Skip to content

Commit

Permalink
Add debug
Browse files Browse the repository at this point in the history
  • Loading branch information
alex27riva committed Oct 20, 2024
1 parent 4162ee0 commit 4dcb2a8
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions internal/util/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,21 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
)

var debug bool

func init() {
// Check if SOC_DEBUG is set and enable debug mode if it is
if val, exists := os.LookupEnv("SOC_DEBUG"); exists {
debug, _ = strconv.ParseBool(val)
}
}

func MakeAPIRequest(url string, headers map[string]string, target interface{}) error {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
Expand All @@ -23,6 +35,14 @@ func MakeAPIRequest(url string, headers map[string]string, target interface{}) e
req.Header.Set(key, value)
}

// Log request details if debug is enabled
if debug {
log.Printf("Making API request to URL: %s", url)
for key, value := range headers {
log.Printf("Header: %s = %s", key, value)
}
}

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
Expand All @@ -35,6 +55,10 @@ func MakeAPIRequest(url string, headers map[string]string, target interface{}) e
return fmt.Errorf("error reading response body: %w", err)
}

if debug {
log.Printf("Response body: %s", string(body))
}

err = json.Unmarshal(body, target)
if err != nil {
return fmt.Errorf("error unmarshalling JSON response: %w", err)
Expand Down

0 comments on commit 4dcb2a8

Please sign in to comment.