mirror of
https://github.com/writeas/go-writeas.git
synced 2025-07-27 19:18:35 +00:00
89 lines
1.9 KiB
Go
89 lines
1.9 KiB
Go
package writeas
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/writeas/impart"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
apiURL = "https://write.as/api"
|
|
)
|
|
|
|
type Client struct {
|
|
baseURL string
|
|
|
|
// Access token for the user making requests.
|
|
token string
|
|
// Client making requests to the API
|
|
client *http.Client
|
|
}
|
|
|
|
// defaultHTTPTimeout is the default http.Client timeout.
|
|
const defaultHTTPTimeout = 10 * time.Second
|
|
|
|
func NewClient(token string) *Client {
|
|
return &Client{
|
|
token: token,
|
|
client: &http.Client{Timeout: defaultHTTPTimeout},
|
|
baseURL: apiURL,
|
|
}
|
|
}
|
|
|
|
func (c *Client) get(path string, r interface{}) (*impart.Envelope, error) {
|
|
method := "GET"
|
|
if method != "GET" && method != "HEAD" {
|
|
return nil, errors.New(fmt.Sprintf("Method %s not currently supported by library (only HEAD and GET).\n", method))
|
|
}
|
|
|
|
return c.request(method, path, nil, r)
|
|
}
|
|
|
|
func (c *Client) post(path string, data, r interface{}) (*impart.Envelope, error) {
|
|
b := new(bytes.Buffer)
|
|
json.NewEncoder(b).Encode(data)
|
|
return c.request("POST", path, b, r)
|
|
}
|
|
|
|
func (c *Client) request(method, path string, data io.Reader, result interface{}) (*impart.Envelope, error) {
|
|
url := fmt.Sprintf("%s%s", c.baseURL, path)
|
|
r, err := http.NewRequest(method, url, data)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Create request: %v", err)
|
|
}
|
|
|
|
c.prepareRequest(r)
|
|
resp, err := c.client.Do(r)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Request: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
env := &impart.Envelope{
|
|
Code: resp.StatusCode,
|
|
}
|
|
if result != nil {
|
|
env.Data = result
|
|
}
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&env)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return env, nil
|
|
}
|
|
|
|
func (c *Client) prepareRequest(r *http.Request) {
|
|
r.Header.Add("User-Agent", "go-writeas v1")
|
|
r.Header.Add("Content-Type", "application/json")
|
|
if c.token != "" {
|
|
r.Header.Add("Authorization", "Token "+c.token)
|
|
}
|
|
}
|