@@ -4,75 +4,7 @@ Package httpc provides functions for simplifying client-side HTTP request handli
4
4
5
5
## Examples
6
6
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
76
8
77
9
## Contributing
78
10
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
0 commit comments