init
This commit is contained in:
42
raindrop/api.go
Normal file
42
raindrop/api.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package raindrop
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func (c *Client) MakeApiRequest(method string, path string, body io.Reader, params url.Values) ([]byte, error, int) {
|
||||
u, err := url.Parse(c.getApiURL(path))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse url: %v", err), 500
|
||||
}
|
||||
u.RawQuery = params.Encode()
|
||||
|
||||
req, err := http.NewRequest(method, u.String(), body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create request: %v", err), 500
|
||||
}
|
||||
|
||||
if c.token != nil {
|
||||
req.Header.Set("Authorization", c.token.Type+" "+c.token.Value)
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to send request: %v", err), resp.StatusCode
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read data from response: %v", err), 500
|
||||
}
|
||||
|
||||
if resp.StatusCode >= 400 {
|
||||
return data, fmt.Errorf("response failed with status %s", resp.Status), resp.StatusCode
|
||||
}
|
||||
|
||||
return data, nil, resp.StatusCode
|
||||
}
|
||||
Reference in New Issue
Block a user