summaryrefslogtreecommitdiff
path: root/internal/easyhttp
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-05-15 21:01:14 +0300
committerPaul Buetow <paul@buetow.org>2024-05-15 21:01:14 +0300
commit51ebb1e131818f9b23dd25dea5be841a90476d50 (patch)
tree83d7d81fb6528a37177f7f17ebb112beaee9325c /internal/easyhttp
parentb97854c8ec2665887121d773ab994a09ca768adb (diff)
initial merge operation
Diffstat (limited to 'internal/easyhttp')
-rw-r--r--internal/easyhttp/easyhttp.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/internal/easyhttp/easyhttp.go b/internal/easyhttp/easyhttp.go
new file mode 100644
index 0000000..aa405e6
--- /dev/null
+++ b/internal/easyhttp/easyhttp.go
@@ -0,0 +1,46 @@
+package easyhttp
+
+import (
+ "io"
+ "net/http"
+ "encoding/json"
+)
+
+func Get(uri, apiKey string) ([]byte, error) {
+ var (
+ client = &http.Client{}
+ bytes []byte
+ )
+
+ req, err := http.NewRequest("GET", uri, nil)
+ if err != nil {
+ return bytes, err
+ }
+
+ req.Header.Set("X-API-KEY", apiKey)
+
+ resp, err := client.Do(req)
+ if err != nil {
+ return bytes, err
+ }
+ defer resp.Body.Close()
+
+ bytes, err = io.ReadAll(resp.Body)
+ if err != nil {
+ return bytes, err
+ }
+
+ return bytes, nil
+}
+
+func GetFromJson[T any](uri, apiKey string) (T, error) {
+ var data T
+
+ bytes, err := Get(uri, apiKey)
+ if err != nil {
+ return data, err
+ }
+
+ err = json.Unmarshal(bytes, &data)
+ return data, err
+}