Create API client
parent
1c8368704c
commit
20f67edd28
@ -0,0 +1,105 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/rs/zerolog/log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
const contentTypeJSON = "application/json"
|
||||
|
||||
// ResourceDto represent a resource as given by the API
|
||||
type ResourceDto struct {
|
||||
URL string `json:"url"`
|
||||
Body string `json:"body"`
|
||||
Title string `json:"title"`
|
||||
Time time.Time `json:"time"`
|
||||
}
|
||||
|
||||
type Client interface {
|
||||
SearchResources(url string) ([]ResourceDto, error)
|
||||
AddResource(res ResourceDto) (ResourceDto, error)
|
||||
AddURL(url string) error
|
||||
}
|
||||
|
||||
type client struct {
|
||||
httpClient *http.Client
|
||||
baseURL string
|
||||
}
|
||||
|
||||
func (c *client) SearchResources(url string) ([]ResourceDto, error) {
|
||||
targetEndpoint := fmt.Sprintf("%s/v1/resources", c.baseURL)
|
||||
|
||||
if url != "" {
|
||||
targetEndpoint += "?url=" + url
|
||||
}
|
||||
|
||||
var resources []ResourceDto
|
||||
_, err := jsonGet(c.httpClient, targetEndpoint, &resources)
|
||||
return resources, err
|
||||
}
|
||||
|
||||
func (c *client) AddResource(res ResourceDto) (ResourceDto, error) {
|
||||
targetEndpoint := fmt.Sprintf("%s/v1/resources", c.baseURL)
|
||||
|
||||
var resourceDto ResourceDto
|
||||
_, err := jsonPost(c.httpClient, targetEndpoint, res, &resourceDto)
|
||||
return resourceDto, err
|
||||
}
|
||||
|
||||
func (c *client) AddURL(url string) error {
|
||||
targetEndpoint := fmt.Sprintf("%s/v1/urls", c.baseURL)
|
||||
_, err := jsonPost(c.httpClient, targetEndpoint, url, nil)
|
||||
return err
|
||||
}
|
||||
|
||||
func NewClient(baseURL string) Client {
|
||||
return &client{
|
||||
httpClient: &http.Client{},
|
||||
baseURL: baseURL,
|
||||
}
|
||||
}
|
||||
|
||||
func jsonGet(httpClient *http.Client, url string, response interface{}) (*http.Response, error) {
|
||||
log.Trace().Str("verb", "GET").Str("url", url).Msg("")
|
||||
|
||||
r, err := httpClient.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&response); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func jsonPost(httpClient *http.Client, url string, request, response interface{}) (*http.Response, error) {
|
||||
log.Trace().Str("verb", "POST").Str("url", url).Msg("")
|
||||
|
||||
var err error
|
||||
var b []byte
|
||||
if request != nil {
|
||||
b, err = json.Marshal(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
r, err := httpClient.Post(url, contentTypeJSON, bytes.NewBuffer(b))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if response != nil {
|
||||
if err := json.NewDecoder(r.Body).Decode(&response); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package api
|
||||
|
||||
import "time"
|
||||
|
||||
// ResourceDto represent a resource as given by the API
|
||||
type ResourceDto struct {
|
||||
URL string `json:"url"`
|
||||
Body string `json:"body"`
|
||||
Title string `json:"title"`
|
||||
Time time.Time `json:"time"`
|
||||
}
|
@ -1,61 +0,0 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/rs/zerolog/log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// StatusCreated HTTP 201
|
||||
const StatusCreated = 201
|
||||
|
||||
const contentTypeJSON = "application/json"
|
||||
|
||||
// Client an http client with built-in JSON (de)serialization
|
||||
type Client struct {
|
||||
client http.Client
|
||||
}
|
||||
|
||||
// JSONGet perform a GET request and serialize response body into given interface if any
|
||||
func (c *Client) JSONGet(url string, response interface{}) (*http.Response, error) {
|
||||
log.Trace().Str("verb", "GET").Str("url", url).Msg("")
|
||||
|
||||
r, err := c.client.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&response); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
|
||||
// JSONPost perform a POST request and serialize request/response body into given interface if any
|
||||
func (c *Client) JSONPost(url string, request, response interface{}) (*http.Response, error) {
|
||||
log.Trace().Str("verb", "POST").Str("url", url).Msg("")
|
||||
|
||||
var err error
|
||||
var b []byte
|
||||
if request != nil {
|
||||
b, err = json.Marshal(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
r, err := c.client.Post(url, contentTypeJSON, bytes.NewBuffer(b))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if response != nil {
|
||||
if err := json.NewDecoder(r.Body).Decode(&response); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
Loading…
Reference in New Issue