From 043db0446b804f96061772bc284b1b9813416332 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 4 Oct 2022 03:29:58 +0100 Subject: [PATCH] changes to readme to match new code semantics --- README.md | 39 ++++----------------------------------- 1 file changed, 4 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 3c62a3b..f622956 100644 --- a/README.md +++ b/README.md @@ -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//lights/4/state", headers, bytes.NewBuffer([]byte(req))) +_, err := request.NewRequest(http.MethodPut, "http://10.0.40.2/api//lights/4/state", headers, bytes.NewBuffer([]byte(req))).Do(ctx, &http.Client{}) if err != nil { log.Println(err) }