weather

package
v0.28.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 21, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package weather shows how to build an API client.

Request data

We may need to send request data as a JSON-encoded body, rather than embedded in the URL. In this case it’s probably a good idea to define some APIRequest adapter struct, as we did with OWMResponse. To make sure we’re marshalling the request data correctly, the httptest handler can check it by unmarshalling it and comparing it with the original value. For APIs with multiple endpoints with different types of requests and responses, we may need multiple adapter structs.

CRUD methods

One common pattern for APIs that manage some kind of external resource is the set of methods known as CRUD: Create, Read, Update, and Delete. It makes sense to map each of these to a corresponding Go method on the client object. There’s usually some unique ID associated with the resource, so that we can specify the one we want. Usually the API assigns an ID, so Create should return it, while Read, Update, and Delete should take it as a parameter. You can often write a single test for all the CRUD methods at once. It should create a resource, read it to check it was created properly, update it, and read it again “to make sure the update worked. The test can then delete the resource, and make sure that a final read fails because it no longer exists.

Level: advanced Topics: api, net/http/httptest, tpg-tools

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Main

func Main() int

Types

type Client

type Client struct {
	BaseURL    string
	HTTPClient *http.Client
	// contains filtered or unexported fields
}

func NewClient

func NewClient(key string) Client

func (Client) FormatURL

func (c Client) FormatURL(location string) string

func (Client) GetWeather

func (c Client) GetWeather(location string) (Conditions, error)

type Conditions

type Conditions struct {
	Summary string
	Temperature
}

func Get

func Get(location, key string) (Conditions, error)

func ParseResponse

func ParseResponse(data []byte) (Conditions, error)

type Temperature

type Temperature float64

func (Temperature) Celsius

func (t Temperature) Celsius() float64

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL