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
|
||||
}
|
||||
68
raindrop/auth.go
Normal file
68
raindrop/auth.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package raindrop
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func (c *Client) OauthRedirect(w http.ResponseWriter, r *http.Request) {
|
||||
authUrl, _ := url.Parse("https://raindrop.io/oauth/authorize")
|
||||
params := url.Values{}
|
||||
params.Set("redirect_uri", c.config.RedirectURI)
|
||||
params.Set("client_id", c.config.ClientId)
|
||||
params.Set("response_type", "code")
|
||||
authUrl.RawQuery = params.Encode()
|
||||
|
||||
http.Redirect(w, r, authUrl.String(), http.StatusFound)
|
||||
}
|
||||
|
||||
type TokensResponse struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
TokenType string `json:"token_type"`
|
||||
// token lifetime in seconds
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
}
|
||||
|
||||
func (c *Client) ExchangeOauthCode(code string) (*TokensResponse, error) {
|
||||
body := struct {
|
||||
GrantType string `json:"grant_type"`
|
||||
Code string `json:"code"`
|
||||
ClientId string `json:"client_id"`
|
||||
ClientSecret string `json:"client_secret"`
|
||||
RedirectURI string `json:"redirect_uri"`
|
||||
}{
|
||||
GrantType: "authorization_code",
|
||||
Code: code,
|
||||
ClientId: c.config.ClientId,
|
||||
ClientSecret: c.config.ClientSecret,
|
||||
RedirectURI: c.config.RedirectURI,
|
||||
}
|
||||
data, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to encode data to send in request: %v", err)
|
||||
}
|
||||
|
||||
resp, err := http.Post("https://raindrop.io/oauth/access_token", "application/json", bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to send request to raindrop api: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
response := &TokensResponse{}
|
||||
if err := json.NewDecoder(resp.Body).Decode(response); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode response from raindrop api: %v", err)
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (c *Client) SetApiToken(tokenType string, value string) {
|
||||
c.token = &ApiToken{
|
||||
Type: tokenType,
|
||||
Value: value,
|
||||
}
|
||||
}
|
||||
36
raindrop/client.go
Normal file
36
raindrop/client.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package raindrop
|
||||
|
||||
import "fmt"
|
||||
|
||||
type ClientConfig struct {
|
||||
ClientId string
|
||||
ClientSecret string
|
||||
RedirectURI string
|
||||
}
|
||||
|
||||
type ApiToken struct {
|
||||
Type string
|
||||
Value string
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
config ClientConfig
|
||||
token *ApiToken
|
||||
}
|
||||
|
||||
func NewClient(config ClientConfig) (*Client, error) {
|
||||
if config.ClientId == "" || config.ClientSecret == "" || config.RedirectURI == "" {
|
||||
return nil, fmt.Errorf("some environment variables missing")
|
||||
}
|
||||
return &Client{
|
||||
config: config,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Client) baseURL() string {
|
||||
return "https://api.raindrop.io/rest/v1"
|
||||
}
|
||||
|
||||
func (c *Client) getApiURL(path string) string {
|
||||
return c.baseURL() + path
|
||||
}
|
||||
Reference in New Issue
Block a user