forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/kavenegar/kavenegar-go/client.go

154 lines
2.0 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package kavenegar
import (
"encoding/json"
"fmt"
"net"
"net/http"
"net/url"
"strings"
)
const (
apiBaseURL = "https://api.kavenegar.com/"
2024-02-18 10:42:21 +00:00
apiVersion = "v1"
apiFormat = "json"
version = "0.1.0"
2024-02-18 10:42:21 +00:00
)
type Return struct {
Status int `json:"status"`
2024-02-18 10:42:21 +00:00
Message string `json:"message"`
}
type ReturnError struct {
*Return `json:"return"`
}
type Client struct {
BaseClient *http.Client
apikey string
BaseURL *url.URL
2024-02-18 10:42:21 +00:00
}
func NewClient(apikey string) *Client {
2024-02-18 10:42:21 +00:00
baseURL, _ := url.Parse(apiBaseURL)
2024-02-18 10:42:21 +00:00
c := &Client{
2024-02-18 10:42:21 +00:00
BaseClient: http.DefaultClient,
BaseURL: baseURL,
apikey: apikey,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return c
2024-02-18 10:42:21 +00:00
}
func (c *Client) EndPoint(parts ...string) *url.URL {
2024-02-18 10:42:21 +00:00
up := []string{apiVersion, c.apikey}
2024-02-18 10:42:21 +00:00
up = append(up, parts...)
2024-02-18 10:42:21 +00:00
u, _ := url.Parse(strings.Join(up, "/"))
2024-02-18 10:42:21 +00:00
u.Path = fmt.Sprintf("/%s.%s", u.Path, apiFormat)
2024-02-18 10:42:21 +00:00
return u
2024-02-18 10:42:21 +00:00
}
func (c *Client) Execute(urlStr string, b url.Values, v interface{}) error {
2024-02-18 10:42:21 +00:00
body := strings.NewReader(b.Encode())
2024-02-18 10:42:21 +00:00
ul, _ := url.Parse(urlStr)
2024-02-18 10:42:21 +00:00
u := c.BaseURL.ResolveReference(ul)
2024-02-18 10:42:21 +00:00
req, _ := http.NewRequest("POST", u.String(), body)
2024-02-18 10:42:21 +00:00
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
2024-02-18 10:42:21 +00:00
req.Header.Add("Accept", "application/json")
2024-02-18 10:42:21 +00:00
req.Header.Add("Accept-Charset", "utf-8")
2024-02-18 10:42:21 +00:00
resp, err := c.BaseClient.Do(req)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
if err, ok := err.(net.Error); ok {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if resp == nil {
2024-02-18 10:42:21 +00:00
return &HTTPError{
Status: http.StatusInternalServerError,
2024-02-18 10:42:21 +00:00
Message: "nil api response",
Err: err,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return &HTTPError{
Status: resp.StatusCode,
2024-02-18 10:42:21 +00:00
Message: resp.Status,
Err: err,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer resp.Body.Close()
2024-02-18 10:42:21 +00:00
if 200 != resp.StatusCode {
2024-02-18 10:42:21 +00:00
re := new(ReturnError)
2024-02-18 10:42:21 +00:00
err = json.NewDecoder(resp.Body).Decode(&re)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return &HTTPError{
Status: resp.StatusCode,
2024-02-18 10:42:21 +00:00
Message: resp.Status,
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return &APIError{
Status: re.Return.Status,
2024-02-18 10:42:21 +00:00
Message: re.Return.Message,
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
_ = json.NewDecoder(resp.Body).Decode(&v)
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}