You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*`OptHeader(key, value string)` appends a custom header to each request.
72
73
73
74
## Usage with a payload
74
75
75
-
The first argument to the `Do` method is the payload to send to the server, when set. You can create a payload
76
-
using the following methods:
76
+
The first argument to the `Do` method is the payload to send to the server, when set.
77
+
You can create a payload using the following methods:
77
78
78
79
*`client.NewRequest()` returns a new empty payload which defaults to GET.
79
-
*`client.NewJSONRequest(payload any, accept string)` returns a new request with a JSON payload which defaults to POST.
80
-
*`client.NewMultipartRequest(payload any, accept string)` returns a new request with a Multipart Form data payload which
81
-
defaults to POST.
82
-
*`client.NewFormRequest(payload any, accept string)` returns a new request with a Form data payload which defaults to POST.
80
+
*`client.NewJSONRequest(payload any, accept string)` returns a new request with
81
+
a JSON payload which defaults to POST.
82
+
*`client.NewMultipartRequest(payload any, accept string)` returns a new request with
83
+
a Multipart Form data payload which defaults to POST.
84
+
*`client.NewFormRequest(payload any, accept string)` returns a new request with a
85
+
Form data payload which defaults to POST.
83
86
84
87
For example,
85
88
@@ -131,7 +134,7 @@ type Payload interface {
131
134
132
135
## Request options
133
136
134
-
The signature of the `Do` method is:
137
+
The signature of the `Do` method is as follows:
135
138
136
139
```go
137
140
typeClientinterface {
@@ -143,16 +146,19 @@ type Client interface {
143
146
}
144
147
```
145
148
146
-
Various options can be passed to modify each individual request when using the `Do` method:
149
+
If you pass a context to the `DoWithContext` method, then the request can be
150
+
cancelled using the context in addition to the timeout. Various options can be passed to
151
+
modify each individual request when using the `Do` method:
147
152
148
153
*`OptReqEndpoint(value string)` sets the endpoint for the request
149
154
*`OptPath(value ...string)` appends path elements onto a request endpoint
150
155
*`OptToken(value Token)` adds an authorization header (overrides the client OptReqToken option)
151
156
*`OptQuery(value url.Values)` sets the query parameters to a request
152
-
*`OptHeader(key, value string)` appends a custom header to the request
153
-
*`OptResponse(func() error)` allows you to set a callback function to process a streaming response.
154
-
See below for more details.
157
+
*`OptHeader(key, value string)` sets a custom header to the request
155
158
*`OptNoTimeout()` disables the timeout on the request, which is useful for long running requests
159
+
*`OptTextStreamCallback(func(TextStreamCallback) error)` allows you to set a callback
160
+
function to process a streaming text response of type `text/event-stream`. See below for
161
+
more details.
156
162
157
163
## Authentication
158
164
@@ -185,10 +191,45 @@ You can also set the token on a per-request basis using the `OptToken` option in
185
191
186
192
You can create a payload with form data:
187
193
188
-
*`client.NewFormRequest(payload any, accept string)` returns a new request with a Form data payload which defaults to POST.
189
-
*`client.NewMultipartRequest(payload any, accept string)` returns a new request with a Multipart Form data payload which defaults to POST. This is useful for file uploads.
194
+
*`client.NewFormRequest(payload any, accept string)` returns a new request with a Form
195
+
data payload which defaults to POST.
196
+
*`client.NewMultipartRequest(payload any, accept string)` returns a new request with
197
+
a Multipart Form data payload which defaults to POST. This is useful for file uploads.
190
198
191
-
The payload should be a `struct` where the fields are converted to form tuples. File uploads require a field of type `multipart.File`.
199
+
The payload should be a `struct` where the fields are converted to form tuples. File uploads require a field of type `multipart.File`. For example,
If the returned content is a stream of JSON responses, then you can use the `OptResponse(fn func() error)` option, which
206
-
will be called by the `Do` method for each response. The function should return an error if the stream should be terminated.
246
+
The client implements a streaming text event callback which can be used to process a stream of text events, as per the [Mozilla specification](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events).
247
+
248
+
In order to process streamed events, pass the `OptTextStreamCallback()` option to the request
249
+
with a callback function, which should have the following signature:
250
+
251
+
```go
252
+
funcCallback(eventclient.TextStreamEvent) error {
253
+
// Finish processing successfully
254
+
if event.Event == "close" {
255
+
return io.EOF
256
+
}
257
+
258
+
// Decode the data into a JSON object
259
+
vardatamap[string]any
260
+
iferr:= event.Json(data); err != nil {
261
+
return err
262
+
}
263
+
264
+
// Return success - continue streaming
265
+
returnnil
266
+
}
267
+
```
268
+
269
+
The `TextStreamEvent` object has the following
270
+
271
+
If you return an error of type `io.EOF` from the callback, then the stream will be closed.
272
+
Similarly, if you return any other error the stream will be closed and the error returned.
273
+
207
274
Usually, you would pair this option with `OptNoTimeout` to prevent the request from timing out.
0 commit comments