Working auth and photo json endpoint
Signed-off-by: Kris Nóva <kris@nivenly.com>
This commit is contained in:
parent
ef275f97f4
commit
e4323b6047
2032 changed files with 821464 additions and 52 deletions
|
@ -1,14 +1,32 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
// DefaultContentType is the content type header the API expects
|
||||
DefaultContentType string = "application/json; charset=utf-8"
|
||||
)
|
||||
|
||||
type V1Client struct {
|
||||
token string
|
||||
connectionString string
|
||||
client http.Client
|
||||
}
|
||||
|
||||
func New(connection, token string) *V1Client {
|
||||
c := http.Client{}
|
||||
return &V1Client{
|
||||
client: c,
|
||||
connectionString: connection,
|
||||
token: token,
|
||||
}
|
||||
}
|
||||
|
||||
func (v1 *V1Client) SetConnectionString(connection string) {
|
||||
|
@ -19,14 +37,48 @@ func (v1 *V1Client) SetToken(token string) {
|
|||
v1.token = token
|
||||
}
|
||||
|
||||
// GET is the V1 GET function. By design it will check globally for all non 200
|
||||
// responses and return an error if a non 200 is encountered.
|
||||
func (v1 *V1Client) GET(format string, a ...interface{}) (*http.Response, error) {
|
||||
url := v1.Endpoint(fmt.Sprintf(format, a))
|
||||
return http.Get(url)
|
||||
str := fmt.Sprintf(format, a...)
|
||||
//logger.Debug("GET [%s]", str)
|
||||
url := v1.EndpointStr(str)
|
||||
buffer := &bytes.Buffer{}
|
||||
req, err := http.NewRequest("GET", url, buffer)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to generate new request: %v", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", DefaultContentType)
|
||||
req.Header.Set("X-Session-Id", v1.token)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return resp, fmt.Errorf("[%d]: unable to read body: %v", err)
|
||||
}
|
||||
return resp, fmt.Errorf("[%d]: %s", resp.StatusCode, body)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (v1 *V1Client) Endpoint(str string) string {
|
||||
func (v1 *V1Client) EndpointStr(str string) string {
|
||||
if strings.HasPrefix("/", str) {
|
||||
return fmt.Sprintf("%s%s", v1.connectionString, str)
|
||||
str = fmt.Sprintf("%s%s", v1.connectionString, str)
|
||||
} else {
|
||||
str = fmt.Sprintf("%s/%s", v1.connectionString, str)
|
||||
}
|
||||
return fmt.Sprintf("%s/%s", v1.connectionString, str)
|
||||
return str
|
||||
}
|
||||
|
||||
func (v1 *V1Client) EndpointURL(str string) (*url.URL, error) {
|
||||
if strings.HasPrefix("/", str) {
|
||||
str = fmt.Sprintf("%s%s", v1.connectionString, str)
|
||||
} else {
|
||||
str = fmt.Sprintf("%s/%s", v1.connectionString, str)
|
||||
}
|
||||
return url.Parse(str)
|
||||
}
|
||||
|
|
|
@ -89,12 +89,6 @@ func (v1 *V1Client) GetPhoto(uuid string) (*Photo, error) {
|
|||
return nil, fmt.Errorf("unable to parse body: %v", err)
|
||||
}
|
||||
|
||||
// The API returns HTML so we have to hack this shit up
|
||||
// TODO @kris-nova This is where we left off
|
||||
// TODO It looks like the API is returning HTML SMDH...
|
||||
//fmt.Println(string(bytes))
|
||||
//return nil, nil
|
||||
|
||||
err = json.Unmarshal(bytes, &photo)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to JSON unmarshal response body: %v", err)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue