2019-02-13 14:05:59 +01:00
|
|
|
package koofrclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2019-08-14 15:56:32 +02:00
|
|
|
"github.com/koofr/go-httpclient"
|
2019-02-13 14:05:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type KoofrClient struct {
|
|
|
|
*httpclient.HTTPClient
|
|
|
|
token string
|
|
|
|
userID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewKoofrClient(baseUrl string, disableSecurity bool) *KoofrClient {
|
|
|
|
var httpClient *httpclient.HTTPClient
|
|
|
|
|
|
|
|
if disableSecurity {
|
|
|
|
httpClient = httpclient.Insecure()
|
|
|
|
} else {
|
|
|
|
httpClient = httpclient.New()
|
|
|
|
}
|
|
|
|
|
2019-08-26 19:00:17 +02:00
|
|
|
return NewKoofrClientWithHTTPClient(baseUrl, httpClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewKoofrClientWithHTTPClient(baseUrl string, httpClient *httpclient.HTTPClient) *KoofrClient {
|
2019-02-13 14:05:59 +01:00
|
|
|
apiBaseUrl, _ := url.Parse(baseUrl)
|
|
|
|
|
|
|
|
httpClient.BaseURL = apiBaseUrl
|
|
|
|
|
2019-08-26 19:00:17 +02:00
|
|
|
client:= &KoofrClient{
|
2019-02-13 14:05:59 +01:00
|
|
|
HTTPClient: httpClient,
|
|
|
|
token: "",
|
|
|
|
userID: "",
|
|
|
|
}
|
2019-08-26 19:00:17 +02:00
|
|
|
|
|
|
|
client.SetUserAgent("go koofrclient")
|
|
|
|
return client
|
2019-02-13 14:05:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *KoofrClient) SetUserAgent(ua string) {
|
|
|
|
c.Headers.Set("User-Agent", ua)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *KoofrClient) SetToken(token string) {
|
|
|
|
c.token = token
|
|
|
|
c.HTTPClient.Headers.Set("Authorization", fmt.Sprintf("Token token=%s", token))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *KoofrClient) GetToken() string {
|
|
|
|
return c.token
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *KoofrClient) SetUserID(userID string) {
|
|
|
|
c.userID = userID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *KoofrClient) GetUserID() string {
|
|
|
|
return c.userID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *KoofrClient) Authenticate(email string, password string) (err error) {
|
|
|
|
var tokenResponse Token
|
|
|
|
|
|
|
|
tokenRequest := TokenRequest{
|
|
|
|
Email: email,
|
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
|
|
|
|
request := httpclient.RequestData{
|
|
|
|
Method: "POST",
|
|
|
|
Path: "/token",
|
|
|
|
Headers: make(http.Header),
|
|
|
|
ExpectedStatus: []int{http.StatusOK},
|
|
|
|
ReqEncoding: httpclient.EncodingJSON,
|
|
|
|
ReqValue: tokenRequest,
|
|
|
|
RespEncoding: httpclient.EncodingJSON,
|
|
|
|
RespValue: &tokenResponse,
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := c.Request(&request)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.SetToken(tokenResponse.Token)
|
|
|
|
c.SetUserID(res.Header.Get("X-User-ID"))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|