summaryrefslogtreecommitdiff
path: root/internal/easyhttp
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-09-21 14:03:45 +0300
committerPaul Buetow <paul@buetow.org>2024-09-21 14:03:45 +0300
commitdff4d455e07d639b82a0bed814f41d0656e9b6d0 (patch)
tree56289a6fd80a00a9724992ab9cb8b3d7215ebedf /internal/easyhttp
parent780ade3dc066afb8a43be824373414f3d316ebd6 (diff)
cleanup
Diffstat (limited to 'internal/easyhttp')
-rw-r--r--internal/easyhttp/easyhttp.go110
-rw-r--r--internal/easyhttp/saferrors.go31
2 files changed, 0 insertions, 141 deletions
diff --git a/internal/easyhttp/easyhttp.go b/internal/easyhttp/easyhttp.go
deleted file mode 100644
index 2968454..0000000
--- a/internal/easyhttp/easyhttp.go
+++ /dev/null
@@ -1,110 +0,0 @@
-package easyhttp
-
-import (
- "bytes"
- "context"
- "encoding/json"
- "fmt"
- "io"
- "log"
- "net/http"
- "sync"
-)
-
-func Get(ctx context.Context, uri, apiKey string) ([]byte, error) {
- var (
- client = &http.Client{}
- bytes []byte
- )
-
- req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
- if err != nil {
- return bytes, fmt.Errorf("%s: %w", uri, err)
- }
-
- req.Header.Set("X-API-KEY", apiKey)
-
- resp, err := client.Do(req)
- if err != nil {
- return bytes, fmt.Errorf("%s: %w", uri, err)
- }
- defer resp.Body.Close()
-
- bytes, err = io.ReadAll(resp.Body)
- if err != nil {
- return bytes, fmt.Errorf("%s: %w", uri, err)
- }
-
- return bytes, nil
-}
-
-// Get data from JSON
-func GetData[T any](ctx context.Context, uri, apiKey string, data *T) error {
- bytes, err := Get(ctx, uri, apiKey)
- if err != nil {
- return err
- }
-
- return json.Unmarshal(bytes, data)
-}
-
-func Post(ctx context.Context, uri, apiKey string, data []byte) ([]byte, error) {
- req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, bytes.NewBuffer(data))
- if err != nil {
- return []byte{}, fmt.Errorf("%s: %w", uri, err)
- }
-
- req.Header.Set("Content-Type", "application/json")
- req.Header.Set("X-API-KEY", apiKey)
-
- client := &http.Client{}
- resp, err := client.Do(req)
- if err != nil {
- return []byte{}, fmt.Errorf("%s: %w", uri, err)
- }
- defer resp.Body.Close()
-
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return []byte{}, fmt.Errorf("%s: %w", uri, err)
- }
-
- switch resp.StatusCode {
- case 200:
- return body, nil
- case 401:
- return body, fmt.Errorf("unauthorized, API key configured?")
- default:
- return body, fmt.Errorf("unexpected HTTP response code %d", resp.StatusCode)
- }
-}
-
-// Submit structure as JSON to API
-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")
- }
- var errs safErrors
- var wg sync.WaitGroup
-
- for _, server := range servers {
- log.Println("Submitting data to", server)
- wg.Add(1)
- go func(server string) {
- defer wg.Done()
- errs.Append(postData[T](ctx, fmt.Sprintf("%s/%s", server, uri), apiKey, data))
- }(server)
- }
-
- wg.Wait()
- return errs.Join()
-}
-
-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(ctx, uri, apiKey, jsonData)
- return err
-}
diff --git a/internal/easyhttp/saferrors.go b/internal/easyhttp/saferrors.go
deleted file mode 100644
index 464c90d..0000000
--- a/internal/easyhttp/saferrors.go
+++ /dev/null
@@ -1,31 +0,0 @@
-package easyhttp
-
-import (
- "errors"
- "sync"
-)
-
-// Safe errors
-type safErrors struct {
- errs []error
- mutex sync.Mutex
-}
-
-func (errs *safErrors) Append(err error) {
- if err == nil {
- return
- }
- errs.mutex.Lock()
- defer errs.mutex.Unlock()
- errs.errs = append(errs.errs, err)
-}
-
-func (errs *safErrors) Join() error {
- errs.mutex.Lock()
- defer errs.mutex.Unlock()
- return errors.Join(errs.errs...)
-}
-
-func (errs *safErrors) Error() string {
- return errs.Join().Error()
-}