@@ -8,11 +8,9 @@ import (
88 "fmt"
99 "io"
1010 "net/http"
11- "strings"
1211 "time"
1312
1413 "github.com/sirupsen/logrus"
15- "github.com/vmihailenco/msgpack/v5"
1614 "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
1715)
1816
@@ -24,102 +22,10 @@ type HTTPRequestOptions[T any, R any] struct {
2422 Path string
2523 Payload T
2624 Response * R
27- ContentType string // Optional, defaults to "application/msgpack "
25+ ContentType string // Optional, defaults to "application/json "
2826 Timeout time.Duration // Optional, defaults to 10 seconds
2927}
3028
31- // SendHTTPRequest is a legacy HTTP request function that uses msgpack encoding
32- // Deprecated: Use SendHTTPRequestJSON for new implementations
33- func SendHTTPRequest [T any , R any ](opts HTTPRequestOptions [T , R ]) error {
34- ctx , span := modelTracer .Start (opts .Context , "http.send" )
35- defer span .End ()
36-
37- jsonData , err := msgpack .Marshal (opts .Payload )
38- if err != nil {
39- logrus .Errorln (err )
40- return err
41- }
42-
43- timeout := time .Second * 10
44- if opts .Timeout > 0 {
45- timeout = opts .Timeout
46- }
47-
48- client := & http.Client {
49- Timeout : timeout ,
50- Transport : otelhttp .NewTransport (http .DefaultTransport ),
51- }
52-
53- req , err := http .NewRequestWithContext (ctx , opts .Method , opts .Endpoint .APIEndpoint + opts .Path , bytes .NewBuffer (jsonData ))
54- if err != nil {
55- logrus .Errorln (err )
56- return err
57- }
58-
59- contentType := "application/msgpack"
60- if opts .ContentType != "" {
61- contentType = opts .ContentType
62- }
63-
64- req .Header .Set ("Content-Type" , contentType )
65- req .Header .Set ("User-Agent" , fmt .Sprintf ("shelltimeCLI@%s" , commitID ))
66- req .Header .Set ("Authorization" , "CLI " + opts .Endpoint .Token )
67-
68- logrus .Traceln ("http: " , req .URL .String ())
69-
70- resp , err := client .Do (req )
71- if err != nil {
72- logrus .Errorln (err )
73- return err
74- }
75- defer resp .Body .Close ()
76-
77- logrus .Traceln ("http: " , resp .Status )
78-
79- if resp .StatusCode == http .StatusNoContent {
80- return nil
81- }
82-
83- buf , err := io .ReadAll (resp .Body )
84- if err != nil {
85- logrus .Errorln (err )
86- return err
87- }
88-
89- if resp .StatusCode != http .StatusOK {
90- var msg errorResponse
91- err = json .Unmarshal (buf , & msg )
92- if err != nil {
93- logrus .Errorln ("Failed to parse error response:" , err )
94- return fmt .Errorf ("HTTP error: %d" , resp .StatusCode )
95- }
96- logrus .Errorln ("Error response:" , msg .ErrorMessage )
97- return errors .New (msg .ErrorMessage )
98- }
99-
100- // Only try to unmarshal if we have a response struct
101- if opts .Response != nil {
102- contentType := resp .Header .Get ("Content-Type" )
103- if strings .Contains (contentType , "json" ) {
104- err = json .Unmarshal (buf , opts .Response )
105- if err != nil {
106- logrus .Errorln ("Failed to unmarshal JSON response:" , err )
107- return err
108- }
109- return nil
110- }
111- if strings .Contains (contentType , "msgpack" ) {
112- err = msgpack .Unmarshal (buf , opts .Response )
113- if err != nil {
114- logrus .Errorln ("Failed to unmarshal response:" , err )
115- return err
116- }
117- }
118- }
119-
120- return nil
121- }
122-
12329// SendHTTPRequestJSON is a generic HTTP request function that sends JSON data and unmarshals the response
12430func SendHTTPRequestJSON [T any , R any ](opts HTTPRequestOptions [T , R ]) error {
12531 ctx , span := modelTracer .Start (opts .Context , "http.send.json" )
0 commit comments