summaryrefslogtreecommitdiff
path: root/internal/easyhttp
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-07-14 23:55:53 +0300
committerPaul Buetow <paul@buetow.org>2024-07-14 23:55:53 +0300
commit95b055a9f40787d15c42927041c9e65c49b05ea5 (patch)
tree62a3361cf520077c88a4c672810e9f31c7549954 /internal/easyhttp
parent50b06ab176a24d7ee842e3e07d7462ef465b6762 (diff)
make it all context.Context aware
Diffstat (limited to 'internal/easyhttp')
-rw-r--r--internal/easyhttp/easyhttp.go22
1 files changed, 11 insertions, 11 deletions
diff --git a/internal/easyhttp/easyhttp.go b/internal/easyhttp/easyhttp.go
index a28a77e..32a3069 100644
--- a/internal/easyhttp/easyhttp.go
+++ b/internal/easyhttp/easyhttp.go
@@ -2,6 +2,7 @@ package easyhttp
import (
"bytes"
+ "context"
"encoding/json"
"fmt"
"io"
@@ -9,13 +10,13 @@ import (
"sync"
)
-func Get(uri, apiKey string) ([]byte, error) {
+func Get(ctx context.Context, uri, apiKey string) ([]byte, error) {
var (
client = &http.Client{}
bytes []byte
)
- req, err := http.NewRequest("GET", uri, nil)
+ req, err := http.NewRequestWithContext(ctx, "GET", uri, nil)
if err != nil {
return bytes, fmt.Errorf("%s: %w", uri, err)
}
@@ -37,8 +38,8 @@ func Get(uri, apiKey string) ([]byte, error) {
}
// Get data from JSON
-func GetData[T any](uri, apiKey string, data *T) error {
- bytes, err := Get(uri, apiKey)
+func GetData[T any](ctx context.Context, uri, apiKey string, data *T) error {
+ bytes, err := Get(ctx, uri, apiKey)
if err != nil {
return err
}
@@ -46,9 +47,8 @@ func GetData[T any](uri, apiKey string, data *T) error {
return json.Unmarshal(bytes, data)
}
-func Post(uri, apiKey string, data []byte) ([]byte, error) {
- // TODO: Use contexts in Post and Get requests, e.g. NewRequestWithContext
- req, err := http.NewRequest("POST", uri, bytes.NewBuffer(data))
+func Post(ctx context.Context, uri, apiKey string, data []byte) ([]byte, error) {
+ req, err := http.NewRequestWithContext(ctx, "POST", uri, bytes.NewBuffer(data))
if err != nil {
return []byte{}, fmt.Errorf("%s: %w", uri, err)
}
@@ -71,7 +71,7 @@ func Post(uri, apiKey string, data []byte) ([]byte, error) {
}
// Submit structure as JSON to API
-func PostData[T any](uri, apiKey string, data *T, servers ...string) error {
+func PostData[T any](ctx context.Context, uri, apiKey string, data *T, servers ...string) error {
if len(servers) == 0 {
return fmt.Errorf("no server configured")
}
@@ -82,7 +82,7 @@ func PostData[T any](uri, apiKey string, data *T, servers ...string) error {
wg.Add(1)
go func(server string) {
defer wg.Done()
- errs.Append(postData[T](fmt.Sprintf("%s/%s", server, uri), apiKey, data))
+ errs.Append(postData[T](ctx, fmt.Sprintf("%s/%s", server, uri), apiKey, data))
}(server)
}
@@ -90,11 +90,11 @@ func PostData[T any](uri, apiKey string, data *T, servers ...string) error {
return errs.Join()
}
-func postData[T any](uri, apiKey string, data *T) error {
+func postData[T any](ctx context.Context, uri, apiKey string, data *T) error {
jsonData, err := json.Marshal(data)
if err != nil {
return err
}
- _, err = Post(uri, apiKey, jsonData)
+ _, err = Post(ctx, uri, apiKey, jsonData)
return err
}