Commit 53d7acc

Karn Wong <[email protected]>
2024-07-30 05:57:26
replace net/http with requests
1 parent 88e49bd
cmd/get/get_ip.go
@@ -1,13 +1,12 @@
 package get
 
 import (
-	"encoding/json"
+	"context"
 	"fmt"
-	"io"
 	"net"
-	"net/http"
 	"strings"
 
+	"github.com/carlmjohnson/requests"
 	"github.com/kahnwong/swissknife/color"
 	"github.com/spf13/cobra"
 )
@@ -39,26 +38,16 @@ type PublicIPResponse struct {
 }
 
 func getPublicIP() (PublicIPResponse, error) {
-	// make request
-	resp, err := http.Get("https://api.country.is")
-	if err != nil {
-		return PublicIPResponse{}, err
-	}
+	var response PublicIPResponse
+	err := requests.
+		URL("https://api.country.is").
+		ToJSON(&response).
+		Fetch(context.Background())
 
-	body, err := io.ReadAll(resp.Body)
 	if err != nil {
-		return PublicIPResponse{}, err
+		fmt.Println("Error getting public ip:", err)
 	}
-
-	// parse
-
-	var jsonResponse PublicIPResponse
-	err = json.Unmarshal(body, &jsonResponse)
-	if err != nil {
-		return PublicIPResponse{}, err
-	}
-
-	return jsonResponse, nil
+	return response, nil
 }
 
 var getIPCmd = &cobra.Command{
cmd/shouldideploytoday.go
@@ -1,13 +1,12 @@
 package cmd
 
 import (
-	"encoding/json"
 	"fmt"
-	"io"
-	"log/slog"
-	"net/http"
 	"time"
 
+	"context"
+
+	"github.com/carlmjohnson/requests"
 	"github.com/kahnwong/swissknife/color"
 	"github.com/spf13/cobra"
 )
@@ -19,37 +18,20 @@ type ShouldIDeploy struct {
 	Message       string    `json:"message"`
 }
 
-func ShouldIDeployToday() (ShouldIDeploy, error) {
+func ShouldIDeployToday() ShouldIDeploy {
 	url := "https://shouldideploy.today/api?tz=Asia%2FBangkok"
-	method := "GET"
-
-	client := &http.Client{}
-	req, err := http.NewRequest(method, url, nil)
-	if err != nil {
-		return ShouldIDeploy{}, err
-	}
 
-	if err != nil {
-		return ShouldIDeploy{}, err
-	}
-	res, err := client.Do(req)
-	if err != nil {
-		return ShouldIDeploy{}, err
-	}
-	defer res.Body.Close()
+	var response ShouldIDeploy
+	err := requests.
+		URL(url).
+		ToJSON(&response).
+		Fetch(context.Background())
 
-	body, err := io.ReadAll(res.Body)
 	if err != nil {
-		return ShouldIDeploy{}, err
-	}
-
-	// decode
-	var response ShouldIDeploy
-	if err := json.Unmarshal(body, &response); err != nil {
-		slog.Error("Can not unmarshal JSON")
+		fmt.Println("Error calling ShouldIDeploy API:", err)
 	}
 
-	return response, nil
+	return response
 }
 
 var ShouldIDeployTodayCmd = &cobra.Command{
@@ -57,10 +39,7 @@ var ShouldIDeployTodayCmd = &cobra.Command{
 	Short: "Should I deploy today?",
 	Long:  `Should I deploy today?`,
 	Run: func(cmd *cobra.Command, args []string) {
-		response, err := ShouldIDeployToday()
-		if err != nil {
-			fmt.Println(err)
-		}
+		response := ShouldIDeployToday()
 
 		if response.ShouldIDeploy {
 			fmt.Printf("%s\n", color.Green(response.Message))
cmd/shouldideploytoday_test.go
@@ -6,11 +6,9 @@ import (
 )
 
 func TestShouldIDeployToday(t *testing.T) {
-	response, err := ShouldIDeployToday()
-	if err != nil {
-		t.Errorf("ShouldIDeployToday() error = %v", err)
+	response := ShouldIDeployToday()
 
-	} else if reflect.TypeOf(response.Message).Kind() != reflect.String {
+	if reflect.TypeOf(response.Message).Kind() != reflect.String {
 		t.Errorf("ShouldIDeployToday() does not return a string")
 	}
 }
go.mod
@@ -3,6 +3,7 @@ module github.com/kahnwong/swissknife
 go 1.22
 
 require (
+	github.com/carlmjohnson/requests v0.24.1
 	github.com/charmbracelet/bubbles v0.18.0
 	github.com/charmbracelet/bubbletea v0.26.6
 	github.com/charmbracelet/lipgloss v0.12.1
@@ -50,7 +51,7 @@ require (
 	golang.org/x/exp/shiny v0.0.0-20240719175910-8a7402abbf56 // indirect
 	golang.org/x/image v0.18.0 // indirect
 	golang.org/x/mobile v0.0.0-20231127183840-76ac6878050a // indirect
-	golang.org/x/net v0.23.0 // indirect
+	golang.org/x/net v0.27.0 // indirect
 	golang.org/x/sync v0.7.0 // indirect
 	golang.org/x/sys v0.22.0 // indirect
 	golang.org/x/text v0.16.0 // indirect
go.sum
@@ -1,5 +1,7 @@
 github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
 github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
+github.com/carlmjohnson/requests v0.24.1 h1:M8hmzyJr3A9D3u96MjCNuUVLd7Z3hQb7UjP5DsBp3lE=
+github.com/carlmjohnson/requests v0.24.1/go.mod h1:duYA/jDnyZ6f3xbcF5PpZ9N8clgopubP2nK5i6MVMhU=
 github.com/charmbracelet/bubbles v0.18.0 h1:PYv1A036luoBGroX6VWjQIE9Syf2Wby2oOl/39KLfy0=
 github.com/charmbracelet/bubbles v0.18.0/go.mod h1:08qhZhtIwzgrtBjAcJnij1t1H0ZRjwHyGsy6AL11PSw=
 github.com/charmbracelet/bubbletea v0.26.6 h1:zTCWSuST+3yZYZnVSvbXwKOPRSNZceVeqpzOLN2zq1s=
@@ -111,8 +113,8 @@ golang.org/x/mobile v0.0.0-20231127183840-76ac6878050a/go.mod h1:Ede7gF0KGoHlj82
 golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
 golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
 golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
-golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
-golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
+golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
+golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
 golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
 golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=