@@ -7,18 +7,20 @@ This repository contains a generic HTTP client which can be adapted to provide:
7
7
* Ability to send files and data of type ` multipart/form-data `
8
8
* Ability to send data of type ` application/x-www-form-urlencoded `
9
9
* Debugging capabilities to see the request and response data
10
+ * Streaming JSON responses
10
11
11
12
Documentation: https://pkg.go.dev/github.com/mutablelogic/go-client/pkg/client
12
13
13
- There are also some example API clients:
14
+ There are also some example clients which use this library :
14
15
15
- * [ Bitwarden Client] ( https://github.com/mutablelogic/go-client/tree/main/pkg/bitwarden )
16
- * [ Elevenlabs Client] ( https://github.com/mutablelogic/go-client/tree/main/pkg/elevenlabs )
17
- * [ Home Assistant Client] ( https://github.com/mutablelogic/go-client/tree/main/pkg/homeassistant )
16
+ * [ Bitwarden API Client] ( https://github.com/mutablelogic/go-client/tree/main/pkg/bitwarden )
17
+ * [ Elevenlabs API Client] ( https://github.com/mutablelogic/go-client/tree/main/pkg/elevenlabs )
18
+ * [ Home Assistant API Client] ( https://github.com/mutablelogic/go-client/tree/main/pkg/homeassistant )
18
19
* [ IPify Client] ( https://github.com/mutablelogic/go-client/tree/main/pkg/ipify )
19
20
* [ Mistral API Client] ( https://github.com/mutablelogic/go-client/tree/main/pkg/mistral )
20
21
* [ NewsAPI client] ( https://github.com/mutablelogic/go-client/tree/main/pkg/newsapi )
21
- * [ OpenAI client] ( https://github.com/mutablelogic/go-client/tree/main/pkg/openai )
22
+ * [ Ollama API client] ( https://github.com/mutablelogic/go-client/tree/main/pkg/ollama )
23
+ * [ OpenAI API client] ( https://github.com/mutablelogic/go-client/tree/main/pkg/openai )
22
24
23
25
## Basic Usage
24
26
@@ -29,7 +31,7 @@ to a JSON endpoint:
29
31
package main
30
32
31
33
import (
32
- client " github.com/mutablelogic/go-client"
34
+ client " github.com/mutablelogic/go-client/pkg/client "
33
35
)
34
36
35
37
func main () {
@@ -69,9 +71,8 @@ Various options can be passed to the client `New` method to control its behaviou
69
71
The first argument to the ` Do ` method is the payload to send to the server, when set. You can create a payload
70
72
using the following methods:
71
73
72
- * ` client.NewRequest(accept string) ` returns a new empty payload which defaults to GET. The accept parameter is the
73
- accepted mime-type of the response.
74
- * ` client.NewJSONRequest(payload any, accept string) ` returns a new request with a JSON payload which defaults to GET.
74
+ * ` client.NewRequest() ` returns a new empty payload which defaults to GET.
75
+ * ` client.NewJSONRequest(payload any, accept string) ` returns a new request with a JSON payload which defaults to POST.
75
76
* ` client.NewMultipartRequest(payload any, accept string) ` returns a new request with a Multipart Form data payload which
76
77
defaults to POST.
77
78
* ` client.NewFormRequest(payload any, accept string) ` returns a new request with a Form data payload which defaults to POST.
@@ -82,7 +83,7 @@ For example,
82
83
package main
83
84
84
85
import (
85
- client " github.com/mutablelogic/go-client"
86
+ client " github.com/mutablelogic/go-client/pkg/client "
86
87
)
87
88
88
89
func main () {
@@ -97,7 +98,7 @@ func main() {
97
98
Reply string ` json:"reply"`
98
99
}
99
100
request.Prompt = " Hello, world!"
100
- payload := client.NewJSONRequest (request, " application/json " )
101
+ payload := client.NewJSONRequest (request)
101
102
if err := c.Do (payload, &response, OptPath (" test" )); err != nil {
102
103
// Handle error
103
104
}
@@ -145,7 +146,9 @@ Various options can be passed to modify each individual request when using the `
145
146
* ` OptToken(value Token) ` adds an authorization header (overrides the client OptReqToken option)
146
147
* ` OptQuery(value url.Values) ` sets the query parameters to a request
147
148
* ` OptHeader(key, value string) ` appends a custom header to the request
148
-
149
+ * ` OptResponse(func() error) ` allows you to set a callback function to process a streaming response.
150
+ See below for more details.
151
+ * ` OptNoTimeout() ` disables the timeout on the request, which is useful for long running requests
149
152
150
153
## Authentication
151
154
@@ -155,7 +158,7 @@ The authentication token can be set as follows:
155
158
package main
156
159
157
160
import (
158
- client " github.com/mutablelogic/go-client"
161
+ client " github.com/mutablelogic/go-client/pkg/client "
159
162
)
160
163
161
164
func main () {
@@ -182,3 +185,9 @@ You can create a payload with form data:
182
185
* ` 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.
183
186
184
187
The payload should be a ` struct ` where the fields are converted to form tuples. File uploads require a field of type ` multipart.File ` .
188
+
189
+ ## Streaming Responses
190
+
191
+ If the returned content is a stream of JSON responses, then you can use the ` OptResponse(fn func() error) ` option, which
192
+ will be called by the ` Do ` method for each response. The function should return an error if the stream should be terminated.
193
+ Usually, you would pair this option with ` OptNoTimeout ` to prevent the request from timing out.
0 commit comments