changes to readme to match new code semantics

This commit is contained in:
Michael 2022-10-04 03:29:58 +01:00
parent 5f9797d00e
commit 043db0446b
Signed by: michael
GPG Key ID: 523BD9EF68BDD44C
1 changed files with 4 additions and 35 deletions

View File

@ -31,12 +31,12 @@ type Topics struct {
} `json:"topic_list"`
}
func topics() (topics *Topics, err error) {
func topics(ctx context.Context) (topics *Topics, err error) {
headers := map[string]string{
"Accept": "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 {
return nil, err
}
@ -57,7 +57,7 @@ headers := map[string]string{
"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 {
log.Println(err)
return
@ -72,37 +72,6 @@ if err != nil {
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:
```go
@ -116,7 +85,7 @@ req := `{
"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 {
log.Println(err)
}