From 50b06ab176a24d7ee842e3e07d7462ef465b6762 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 14 Jul 2024 23:45:43 +0300 Subject: initial easy http POST functions --- internal/client/tui/compose.go | 2 ++ internal/easyhttp/easyhttp.go | 50 +++++++++++++++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/internal/client/tui/compose.go b/internal/client/tui/compose.go index 3ba53ec..20f74f1 100644 --- a/internal/client/tui/compose.go +++ b/internal/client/tui/compose.go @@ -11,6 +11,7 @@ import ( ) func composeAction(conf config.ClientConfig, queue bool) tea.Cmd { + err := ensureDirectoryExists(conf.DataDir) composeFile := fmt.Sprintf("%s/%s", conf.DataDir, conf.ComposeFile) @@ -18,6 +19,7 @@ func composeAction(conf config.ClientConfig, queue bool) tea.Cmd { if err != nil { return err } + // ye if !queue { return nil } diff --git a/internal/easyhttp/easyhttp.go b/internal/easyhttp/easyhttp.go index 7b72b9e..a28a77e 100644 --- a/internal/easyhttp/easyhttp.go +++ b/internal/easyhttp/easyhttp.go @@ -1,6 +1,7 @@ package easyhttp import ( + "bytes" "encoding/json" "fmt" "io" @@ -11,13 +12,13 @@ import ( func Get(uri, apiKey string) ([]byte, error) { var ( client = &http.Client{} - bytes []byte + bytes []byte ) req, err := http.NewRequest("GET", uri, nil) if err != nil { return bytes, fmt.Errorf("%s: %w", uri, err) - } + } req.Header.Set("X-API-KEY", apiKey) @@ -37,15 +38,39 @@ 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) - if err != nil { - return err - } + bytes, err := Get(uri, apiKey) + if err != nil { + return err + } return json.Unmarshal(bytes, data) } -// Submiut structure as JSON to API +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)) + if err != nil { + return []byte{}, fmt.Errorf("%s: %w", uri, err) + } + + 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) + } + + return body, nil +} + +// Submit structure as JSON to API func PostData[T any](uri, apiKey string, data *T, servers ...string) error { if len(servers) == 0 { return fmt.Errorf("no server configured") @@ -55,7 +80,7 @@ func PostData[T any](uri, apiKey string, data *T, servers ...string) error { for _, server := range servers { wg.Add(1) - go func(server string){ + go func(server string) { defer wg.Done() errs.Append(postData[T](fmt.Sprintf("%s/%s", server, uri), apiKey, data)) }(server) @@ -65,6 +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 { - return fmt.Errorf("not yet implemented to post to: %s", uri) +func postData[T any](uri, apiKey string, data *T) error { + jsonData, err := json.Marshal(data) + if err != nil { + return err + } + _, err = Post(uri, apiKey, jsonData) + return err } -- cgit v1.2.3