@@ -20,6 +20,8 @@ type RequestError struct {
2020 Err error
2121}
2222
23+ const defaultContentType string = "application/json"
24+
2325type RequestOptions struct {
2426 ContentType string
2527 RawBody bool
@@ -36,26 +38,28 @@ func (c *Client) Request(url string, reqMethod string, reqBody io.Reader, option
3638 return Response {}, & RequestError {Err : fmt .Errorf ("failed to fetch login status. error: %w" , err ), Url : url }
3739 }
3840 if ! islogged {
39- err := c .refreshToken ()
41+ err := c .login ()
4042 if err != nil {
4143 return Response {}, & RequestError {Err : fmt .Errorf ("failed to refresh login token. error: %w" , err ), Url : url }
4244 }
4345 }
4446
45- resp , err := c .rawRequest (url , reqMethod , reqBody , options ... )
47+ var opts RequestOptions
48+ if len (options ) > 0 {
49+ opts = options [0 ]
50+ }
51+
52+ resp , err := c .rawRequest (url , reqMethod , defaultContentType , reqBody , opts )
4653 if err != nil {
4754 return Response {}, err
4855 }
4956 return resp , nil
5057}
5158
52- func (c * Client ) rawRequest (url string , reqMethod string , reqBody io.Reader , options ... RequestOptions ) (Response , error ) {
53- contentType := "application/json"
54- var rawBody bool
55-
59+ func (c * Client ) rawRequest (url string , reqMethod string , contentType string , reqBody io.Reader , options ... RequestOptions ) (Response , error ) {
60+ var opts RequestOptions
5661 if len (options ) > 0 {
57- contentType = options [0 ].ContentType
58- rawBody = options [0 ].RawBody
62+ opts = options [0 ]
5963 }
6064
6165 req , err := http .NewRequest (reqMethod , url , reqBody )
@@ -69,7 +73,11 @@ func (c *Client) rawRequest(url string, reqMethod string, reqBody io.Reader, opt
6973 resp , err := c .client .Do (req )
7074 if err != nil {
7175 return Response {}, & RequestError {Err : fmt .Errorf ("error while submitting request. error: %w" , err ), Url : url }
72- } else if resp .StatusCode != http .StatusOK {
76+ }
77+ defer resp .Body .Close ()
78+
79+ // Check for unexpected status codes
80+ if resp .StatusCode != http .StatusOK && resp .StatusCode != http .StatusCreated {
7381 return Response {}, & RequestError {Err : fmt .Errorf ("unexpected status code %d" , resp .StatusCode ), Url : url }
7482 }
7583
@@ -78,13 +86,22 @@ func (c *Client) rawRequest(url string, reqMethod string, reqBody io.Reader, opt
7886 if err != nil {
7987 return Response {}, & RequestError {Err : fmt .Errorf ("failed to read response body. error: %w" , err ), Url : url }
8088 }
81- defer resp .Body .Close ()
8289
83- if ! rawBody {
84- err = json .Unmarshal (response .Response , & response )
85- if err != nil {
86- return response , & RequestError {Err : fmt .Errorf ("failed to unmarshal response body. error: %w" , err ), Url : url }
87- }
90+ // If RawBody is true, return the raw response without further processing
91+ if opts .RawBody {
92+ return response , nil
93+ }
94+
95+ // Handle empty response bodies for successful requests
96+ if len (response .Response ) == 0 {
97+ response .Message = "Empty response (success)"
98+ return response , nil
99+ }
100+
101+ // Otherwise, unmarshal the response as JSON
102+ err = json .Unmarshal (response .Response , & response )
103+ if err != nil {
104+ return response , & RequestError {Err : fmt .Errorf ("failed to unmarshal response body. error: %w" , err ), Url : url }
88105 }
89106
90107 return response , nil
0 commit comments