telegraph/telegraph.go

38 lines
796 B
Go
Raw Normal View History

2016-12-25 03:10:55 +00:00
// Package telegraph has functions and types used for interacting with the
// Telegraph API.
package telegraph
import (
"errors"
2017-01-10 10:28:23 +00:00
2017-08-24 00:58:37 +00:00
json "github.com/pquerna/ffjson/ffjson"
2017-04-18 16:34:59 +00:00
http "github.com/valyala/fasthttp"
)
const (
// APIEndpoint should be presented in this for all queries to the Telegraph
// API must be served over HTTPS.
APIEndpoint = "https://api.telegra.ph/%s"
// PathEndpoint used if a path parameter is present.
PathEndpoint = "https://api.telegra.ph/%s/%s"
)
2017-04-18 16:34:59 +00:00
func request(url string, args *http.Args) (*Response, error) {
2017-05-11 21:06:32 +00:00
_, body, err := http.Post(nil, url, args)
if err != nil {
return nil, err
}
2017-05-11 21:06:32 +00:00
var resp Response
2017-08-24 00:58:37 +00:00
if err := json.Unmarshal(body, &resp); err != nil {
return nil, err
}
2017-05-11 21:06:32 +00:00
if !resp.Ok {
return nil, errors.New(resp.Error)
}
2017-05-11 21:06:32 +00:00
return &resp, nil
}