changes to readme to match new code semantics
This commit is contained in:
parent
5f9797d00e
commit
043db0446b
39
README.md
39
README.md
@ -31,12 +31,12 @@ type Topics struct {
|
|||||||
} `json:"topic_list"`
|
} `json:"topic_list"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func topics() (topics *Topics, err error) {
|
func topics(ctx context.Context) (topics *Topics, err error) {
|
||||||
headers := map[string]string{
|
headers := map[string]string{
|
||||||
"Accept": "application/json",
|
"Accept": "application/json",
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
}
|
}
|
||||||
resp, err := request.API(http.MethodGet, "https://forum.0cd.xyz/t/nixos-vfio-pcie-passthrough/277", headers, nil)
|
resp, err := request.NewRequest(http.MethodGet, "https://forum.0cd.xyz/t/nixos-vfio-pcie-passthrough/277", headers, nil).Do(ctx, &http.Client{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -57,7 +57,7 @@ headers := map[string]string{
|
|||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := API(http.MethodGet, "https://reqres.in/api/users", headers, nil)
|
resp, err := request.Get(ctx, "https://reqres.in/api/users", headers, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
@ -72,37 +72,6 @@ if err != nil {
|
|||||||
fmt.Println(users["data"])
|
fmt.Println(users["data"])
|
||||||
```
|
```
|
||||||
|
|
||||||
Asynchronous API requests:
|
|
||||||
|
|
||||||
```go
|
|
||||||
func topics(urls []string) (topicBody [][]byte, error err) {
|
|
||||||
headers := map[string]string{
|
|
||||||
"Accept": "application/json",
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
|
|
||||||
ch := make(chan *request.Response)
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
|
|
||||||
for _, path := range urls {
|
|
||||||
wg.Add(1)
|
|
||||||
go request.AsyncAPI(http.MethodGet, path, headers, nil, ch, &wg)
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
wg.Wait()
|
|
||||||
close(ch)
|
|
||||||
}()
|
|
||||||
|
|
||||||
for resp := range ch {
|
|
||||||
if resp.Error != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
topicBody = append(topicBody, resp.Body)
|
|
||||||
}
|
|
||||||
return topicBody, nil
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Sending requests with data using post, put, etc:
|
Sending requests with data using post, put, etc:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -116,7 +85,7 @@ req := `{
|
|||||||
"bri": 255
|
"bri": 255
|
||||||
}`
|
}`
|
||||||
|
|
||||||
_, err := request.API(http.MethodPut, "http://10.0.40.2/api/<apikey>/lights/4/state", headers, bytes.NewBuffer([]byte(req)))
|
_, err := request.NewRequest(http.MethodPut, "http://10.0.40.2/api/<apikey>/lights/4/state", headers, bytes.NewBuffer([]byte(req))).Do(ctx, &http.Client{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user