-
Notifications
You must be signed in to change notification settings - Fork 0
/
alpr_client.go
59 lines (45 loc) · 1.18 KB
/
alpr_client.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package toolkit
import (
"encoding/json"
"errors"
"fmt"
"net/http"
)
const (
alprPath = "/api/v1/alpr"
)
type ALPRClient struct {
base *Client
}
func (client *ALPRClient) Recognize(url string) ([]ResultALPR, error) {
query := client.base.uri + alprPath + "/private/recognize?image_url=" + url
req, err := http.NewRequest(http.MethodGet, query, nil)
if err != nil {
return nil, err
}
req.Header.Set(APIKeyHeader, client.base.token)
response, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
switch response.StatusCode {
case http.StatusOK:
results := make([]ResultALPR, 0)
if err := json.NewDecoder(response.Body).Decode(&results); err != nil {
return nil, errors.New("invalid response body")
}
return results, nil
case http.StatusUnauthorized:
var e Error
if err := json.NewDecoder(response.Body).Decode(&e); err != nil {
return nil, ErrUnauthorized
}
return nil, fmt.Errorf("unauthorized: %s", e.Message)
default:
var e Error
if err := json.NewDecoder(response.Body).Decode(&e); err != nil {
return nil, fmt.Errorf("failed with status %d", response.StatusCode)
}
return nil, fmt.Errorf("failed: %s", e.Message)
}
}