From dff4d455e07d639b82a0bed814f41d0656e9b6d0 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 21 Sep 2024 14:03:45 +0300 Subject: cleanup --- internal/easyhttp/easyhttp.go | 110 ----------------------------------------- internal/easyhttp/saferrors.go | 31 ------------ 2 files changed, 141 deletions(-) delete mode 100644 internal/easyhttp/easyhttp.go delete mode 100644 internal/easyhttp/saferrors.go (limited to 'internal/easyhttp') 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() -} -- cgit v1.2.3