Skip to content

Commit 9afc5df

Browse files
committed
Added some documentation
1 parent 78c4a59 commit 9afc5df

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

README.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This repository contains a generic HTTP client which can be adapted to provide:
77
* Ability to send files and data of type `multipart/form-data`
88
* Ability to send data of type `application/x-www-form-urlencoded`
99
* Debugging capabilities to see the request and response data
10-
* Streaming text events
10+
* Streaming text and JSON events
1111

1212
API Documentation: https://pkg.go.dev/github.com/mutablelogic/go-client
1313

@@ -159,6 +159,9 @@ modify each individual request when using the `Do` method:
159159
* `OptTextStreamCallback(func(TextStreamCallback) error)` allows you to set a callback
160160
function to process a streaming text response of type `text/event-stream`. See below for
161161
more details.
162+
* `OptJsonStreamCallback(func(any) error)` allows you to set a callback for JSON streaming
163+
responses. The callback should have the signature `func(any) error`. See below for
164+
more details.
162165

163166
## Authentication
164167

@@ -191,9 +194,9 @@ You can also set the token on a per-request basis using the `OptToken` option in
191194

192195
You can create a payload with form data:
193196

194-
* `client.NewFormRequest(payload any, accept string)` returns a new request with a Form
197+
* `client.NewFormRequest(payload any, accept string)` returns a new request with a Form
195198
data payload which defaults to POST.
196-
* `client.NewMultipartRequest(payload any, accept string)` returns a new request with
199+
* `client.NewMultipartRequest(payload any, accept string)` returns a new request with
197200
a Multipart Form data payload which defaults to POST. This is useful for file uploads.
198201

199202
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,
@@ -241,9 +244,10 @@ type Unmarshaler interface {
241244
}
242245
```
243246

244-
## Streaming Responses
247+
## Text Streaming Responses
245248

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).
249+
The client implements a streaming text event callback which can be used to process a stream of text events,
250+
as per the [Mozilla specification](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events).
247251

248252
In order to process streamed events, pass the `OptTextStreamCallback()` option to the request
249253
with a callback function, which should have the following signature:
@@ -272,3 +276,12 @@ If you return an error of type `io.EOF` from the callback, then the stream will
272276
Similarly, if you return any other error the stream will be closed and the error returned.
273277

274278
Usually, you would pair this option with `OptNoTimeout` to prevent the request from timing out.
279+
280+
## JSON Streaming Responses
281+
282+
The client decodes JSON streaming responses by passing a callback function to the `OptJsonStreamCallback()` option.
283+
The callback with signature `func(any) error` is called for each JSON object in the stream, where the argument
284+
is the same type as the object in the request.
285+
286+
You can return an error from the callback to stop the stream and return the error, or return `io.EOF` to stop the stream
287+
immediately and return success.

0 commit comments

Comments
 (0)