Finalize proper error management + fix

pull/30/head
Aloïs Micard 4 years ago
parent 8750830a62
commit b2728e4f62
No known key found for this signature in database
GPG Key ID: 1A0EB82F071F5EFE

@ -21,9 +21,6 @@ const (
PaginationPageQueryParam = "pagination-page"
// PaginationSizeQueryParam is the query parameter used to set page size in paginated endpoint
PaginationSizeQueryParam = "pagination-size"
contentTypeJSON = "application/json"
authorizationHeader = "Authorization"
)
// ResourceDto represent a resource as given by the API
@ -60,6 +57,7 @@ func (c *client) SearchResources(url, keyword string,
targetEndpoint := fmt.Sprintf("%s/v1/resources?", c.baseURL)
req := c.httpClient.R()
req.SetAuthToken(c.token)
if url != "" {
b64URL := base64.URLEncoding.EncodeToString([]byte(url))
@ -78,8 +76,6 @@ func (c *client) SearchResources(url, keyword string,
req.SetQueryParam("end-date", endDate.Format(time.RFC3339))
}
req.Header.Set(authorizationHeader, fmt.Sprintf("Bearer %s", c.token))
if paginationPage != 0 {
req.Header.Set(PaginationPageHeader, strconv.Itoa(paginationPage))
}
@ -107,10 +103,9 @@ func (c *client) AddResource(res ResourceDto) (ResourceDto, error) {
targetEndpoint := fmt.Sprintf("%s/v1/resources", c.baseURL)
req := c.httpClient.R()
req.SetAuthToken(c.token)
req.SetBody(res)
req.Header.Set(authorizationHeader, fmt.Sprintf("Bearer %s", c.token))
var resourceDto ResourceDto
req.SetResult(&resourceDto)
@ -122,9 +117,9 @@ func (c *client) ScheduleURL(url string) error {
targetEndpoint := fmt.Sprintf("%s/v1/urls", c.baseURL)
req := c.httpClient.R()
req.SetBody(url)
req.Header.Set(authorizationHeader, fmt.Sprintf("Bearer %s", c.token))
req.SetAuthToken(c.token)
req.SetHeader("Content-Type", "application/json")
req.SetBody(fmt.Sprintf("\"%s\"", url))
_, err := req.Post(targetEndpoint)
return err
@ -145,8 +140,17 @@ func (c *client) Authenticate(credentials CredentialsDto) (string, error) {
// NewAuthenticatedClient create a new Client & authenticate it against the API
func NewAuthenticatedClient(baseURL string, credentials CredentialsDto) (Client, error) {
httpClient := resty.New()
httpClient.SetAuthScheme("Bearer")
httpClient.OnAfterResponse(func(c *resty.Client, r *resty.Response) error {
if r.StatusCode() > 302 {
return fmt.Errorf("error when making HTTP request: %s", r.Status())
}
return nil
})
client := &client{
httpClient: resty.New(),
httpClient: httpClient,
baseURL: baseURL,
}

@ -15,7 +15,6 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumC
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/go-resty/resty v1.12.0 h1:L1P5qymrXL5H/doXe2pKUr1wxovAI5ilm2LdVLbwThc=
github.com/go-resty/resty/v2 v2.3.0 h1:JOOeAvjSlapTT92p8xiS19Zxev1neGikoHsXJeOq8So=
github.com/go-resty/resty/v2 v2.3.0/go.mod h1:UpN9CgLZNsv4e9XG50UU8xdI0F43UQ4HmxLBDwaroHU=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=

Loading…
Cancel
Save