forked from keys-pub/keys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
29 lines (26 loc) · 796 Bytes
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package services
import (
"context"
"github.com/burlingtonbertie99/mykeys/http"
"github.com/burlingtonbertie99/mykeys/user"
"github.com/pkg/errors"
)
// Request resource.
func Request(ctx context.Context, client http.Client, urs string, headers []http.Header) (user.Status, []byte, error) {
logger.Infof("Requesting %s", urs)
req, err := http.NewRequest("GET", urs, nil)
if err != nil {
return user.StatusFailure, nil, err
}
for _, h := range headers {
req.Header.Set(h.Name, h.Value)
}
b, err := client.Request(ctx, req)
if err != nil {
if errHTTP, ok := errors.Cause(err).(http.Err); ok && errHTTP.Code == 404 {
return user.StatusResourceNotFound, nil, errors.Errorf("resource not found")
}
return user.StatusConnFailure, nil, err
}
return user.StatusOK, b, nil
}