Skip to content

Commit 5d2eb66

Browse files
committed
WIP
1 parent 8614423 commit 5d2eb66

File tree

5 files changed

+626
-842
lines changed

5 files changed

+626
-842
lines changed

README.md

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,75 +4,7 @@ Package httpc provides functions for simplifying client-side HTTP request handli
44

55
## Examples
66

7-
### Request creation
8-
9-
The following creates a PATCH request to https://example.com/product/1234 with a JSON request body containing a product
10-
name and accepting a JSON response.
11-
12-
```go
13-
var apiURL, _ = url.Parse("https://example.com/")
14-
15-
func main() {
16-
ctx := context.Background()
17-
18-
req, err := httpc.NewRequest(ctx, "PATCH", "/product/:productID",
19-
httpc.WithBaseURL(apiURL),
20-
httpc.WithHeader("Accept", "application/json"),
21-
httpc.WithPathValue("productID", "1234"),
22-
httpc.WithJSON(map[string]any{
23-
"name": "Jeans",
24-
}))
25-
if err != nil {
26-
panic(err)
27-
}
28-
29-
// Normal net/http request handling
30-
resp, err := http.DefaultClient.Do(req)
31-
32-
// ...
33-
}
34-
```
35-
36-
### Using endpoints
37-
38-
Endpoints allow defining different options and logic for handling requests and responses to specific endpoints.
39-
40-
They also support automatically unmarshalling responses into Go types for example by reading the response JSON.
41-
42-
#### Defining an endpoint
43-
44-
An endpoint consists of a HTTP request method and URL as well as zero or more request options, as well as one or more
45-
"Handlers", which are responsible for processing the response.
46-
47-
The following defines an endpoint for fetching products from an API endpoint returning a JSON body:
48-
49-
```go
50-
var productEndpoint = &httpc.Endpoint[Product]{
51-
Method: "GET",
52-
URL: "https://example.com/product/:id",
53-
Options: []httpc.RequestOption{
54-
// Tell the server we want a JSON response
55-
httpc.WithHeader("Accept", "application/json"),
56-
},
57-
Handlers: []httpc.Handler{
58-
// Decode the response JSON, but only if the response code is 200
59-
httpc.StatusHandler(200, httpc.JSONHandler()),
60-
},
61-
}
62-
```
63-
64-
#### Making a request
65-
66-
Once created an endpoint can be used to make requests and return the unmarshalled response.
67-
68-
Using the endpoint from the previous example, this will fetch the product with ID 1234:
69-
70-
```go
71-
product, _, err := productEndpoint.Do(context.Background(), httpc.WithPathValue("id", "1234"))
72-
if err != nil {
73-
panic(err)
74-
}
75-
```
7+
# TODO: Examples
768

779
## Contributing
7810
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ module github.com/nussjustin/httpc
22

33
go 1.24
44

5-
require github.com/go-json-experiment/json v0.0.0-20250223041408-d3c622f1b874
5+
require (
6+
github.com/go-json-experiment/json v0.0.0-20250223041408-d3c622f1b874
7+
github.com/google/go-cmp v0.7.0
8+
github.com/nussjustin/problem v0.0.0-20250415195331-f012dda88dba
9+
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
github.com/go-json-experiment/json v0.0.0-20250223041408-d3c622f1b874 h1:F8d1AJ6M9UQCavhwmO6ZsrYLfG8zVFWfEfMS2MXPkSY=
22
github.com/go-json-experiment/json v0.0.0-20250223041408-d3c622f1b874/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M=
3+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
4+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
5+
github.com/nussjustin/problem v0.0.0-20250415195331-f012dda88dba h1:YDvOg4zHgPRYJ6oidWAhgB129GVDSVm/5JPe5YjML+Y=
6+
github.com/nussjustin/problem v0.0.0-20250415195331-f012dda88dba/go.mod h1:6OO4YOISsZzGBE6GZddfQOOA7lJ+Zcs77gt+meaglK8=

0 commit comments

Comments
 (0)