|
1 | 1 | # go-client
|
2 |
| -REST API Client for various services |
| 2 | + |
| 3 | +This repository contains a generic HTTP client which can be adapted to provide: |
| 4 | + |
| 5 | +* General HTTP methods for GET and POST of data |
| 6 | +* Ability to send and receive JSON, plaintext and XML data |
| 7 | +* Ability to send files and data of type `multipart/form-data` |
| 8 | +* Ability to send data of type `application/x-www-form-urlencoded` |
| 9 | +* Debugging capabilities to see the request and response data |
| 10 | + |
| 11 | +## Basic Usage |
| 12 | + |
| 13 | +The following example shows how to decode a response from a GET request |
| 14 | +to a JSON endpoint: |
| 15 | + |
| 16 | +```go |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + client "github.com/mutablelogic/go-client" |
| 21 | +) |
| 22 | + |
| 23 | +func main() { |
| 24 | + // Create a new client |
| 25 | + c := client.New(client.OptEndpoint("https://api.example.com/api/v1")) |
| 26 | + |
| 27 | + // Send a GET request, populating a struct with the response |
| 28 | + var response struct { |
| 29 | + Message string `json:"message"` |
| 30 | + } |
| 31 | + if err := c.Do(nil, &response, OptPath("test")); err != nil { |
| 32 | + // Handle error |
| 33 | + } |
| 34 | + |
| 35 | + // Print the response |
| 36 | + fmt.Println(response.Message) |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +Various options can be passed to the client `New` method to control its behaviour: |
| 41 | + |
| 42 | +* `OptEndpoint(value string)` sets the endpoint for all requests |
| 43 | +* `OptTimeout(value time.Duration)` sets the timeout on any request, which defaults to 10 seconds |
| 44 | +* `OptUserAgent(value string)` sets the user agent string on each API request |
| 45 | +* `OptTrace(w io.Writer, verbose bool)` allows you to debug the request and response data. |
| 46 | + When `verbose` is set to true, it also displays the payloads |
| 47 | +* `OptStrict()` turns on strict content type checking on anything returned from the API |
| 48 | +* `OptRateLimit(value float32)` sets the limit on number of requests per second and the API will sleep to regulate |
| 49 | + the rate limit when exceeded |
| 50 | +* `OptReqToken(value Token)` sets a request token for all client requests. This can be overridden by the client |
| 51 | + for individual requests using `OptToken` |
| 52 | +* `OptSkipVerify()` skips TLS certificate domain verification |
| 53 | +* `OptHeader(key, value string)` appends a custom header to each request |
| 54 | + |
| 55 | +## Usage with a payload |
| 56 | + |
| 57 | +The first argument to the `Do` method is the payload to send to the server, when set. You can create a payload |
| 58 | +using the following methods: |
| 59 | + |
| 60 | +* `client.NewRequest(accept string)` returns a new empty payload which defaults to GET. The accept parameter is the |
| 61 | + accepted mime-type of the response. |
| 62 | +* `client.NewJSONRequest(payload any, accept string)` returns a new request with a JSON payload which defaults to GET. |
| 63 | +* `client.NewMultipartRequest(payload any, accept string)` returns a new request with a Multipart Form data payload which |
| 64 | + defaults to POST. |
| 65 | +* `client.NewFormRequest(payload any, accept string)` returns a new request with a Form data payload which defaults to POST. |
| 66 | + |
| 67 | +For example, |
| 68 | + |
| 69 | +```go |
| 70 | +package main |
| 71 | + |
| 72 | +import ( |
| 73 | + client "github.com/mutablelogic/go-client" |
| 74 | +) |
| 75 | + |
| 76 | +func main() { |
| 77 | + // Create a new client |
| 78 | + c := client.New(client.OptEndpoint("https://api.example.com/api/v1")) |
| 79 | + |
| 80 | + // Send a GET request, populating a struct with the response |
| 81 | + var request struct { |
| 82 | + Prompt string `json:"prompt"` |
| 83 | + } |
| 84 | + var response struct { |
| 85 | + Reply string `json:"reply"` |
| 86 | + } |
| 87 | + request.Prompt = "Hello, world!" |
| 88 | + payload := client.NewJSONRequest(request, "application/json") |
| 89 | + if err := c.Do(payload, &response, OptPath("test")); err != nil { |
| 90 | + // Handle error |
| 91 | + } |
| 92 | + |
| 93 | + // Print the response |
| 94 | + fmt.Println(response.Reply) |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +You can also implement your own payload by implementing the `Payload` interface: |
| 99 | + |
| 100 | +```go |
| 101 | +type Payload interface { |
| 102 | + io.Reader |
| 103 | + |
| 104 | + // The method to use to send the payload |
| 105 | + Method() string |
| 106 | + |
| 107 | + // The content type of the payload |
| 108 | + Type() string |
| 109 | + |
| 110 | + // The content type which is accepted as a response, or empty string if any |
| 111 | + Accept() string |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +### Request options |
| 116 | + |
| 117 | +The signature of the `Do` method is: |
| 118 | + |
| 119 | +```go |
| 120 | +type Client interface { |
| 121 | + // Perform request and wait for response |
| 122 | + Do(in Payload, out any, opts ...RequestOpt) error |
| 123 | + |
| 124 | + // Perform request and wait for response, with context for cancellation |
| 125 | + DoWithContext(ctx context.Context, in Payload, out any, opts ...RequestOpt) error |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +Various options can be passed to modify each individual request when using the `Do` method: |
| 130 | + |
| 131 | +* `OptReqEndpoint(value string)` sets the endpoint for the request |
| 132 | +* `OptPath(value ...string)` appends path elements onto a request endpoint |
| 133 | +* `OptToken(value Token)` adds an authorization header (overrides the client OptReqToken option) |
| 134 | +* `OptQuery(value url.Values)` sets the query parameters to a request |
| 135 | +* `OptHeader(key, value string)` appends a custom header to the request |
| 136 | + |
| 137 | + |
| 138 | +### Authentication |
| 139 | + |
| 140 | +The authentication token can be set as follows: |
| 141 | + |
| 142 | +```go |
| 143 | +package main |
| 144 | + |
| 145 | +import ( |
| 146 | + client "github.com/mutablelogic/go-client" |
| 147 | +) |
| 148 | + |
| 149 | +func main() { |
| 150 | + // Create a new client |
| 151 | + c := client.New( |
| 152 | + client.OptEndpoint("https://api.example.com/api/v1"), |
| 153 | + client.OptReqToken(client.Token{ |
| 154 | + Scheme: "Bearer", |
| 155 | + Value: os.GetEnv("API_TOKEN"), |
| 156 | + }), |
| 157 | + ) |
| 158 | + |
| 159 | + // ... |
| 160 | +} |
| 161 | +``` |
0 commit comments