forked from abclogin/dadata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdadata.go
79 lines (63 loc) · 2.23 KB
/
dadata.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Golang client library for DaData.ru (https://dadata.ru/).
// Package dadata implemented only cleaning (https://dadata.ru/api/clean/) and suggesting (https://dadata.ru/api/suggest/)
package dadata
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
const baseURL = "https://dadata.ru/api/v2/"
// DaData client for DaData.ru (https://dadata.ru/)
type DaData struct {
apiKey string
secretKey string
httpClient *http.Client
}
//NewDaData Create new client of DaData.
//Api and secret keys see on profile page (https://dadata.ru/profile/).
func NewDaData(apiKey, secretKey string) *DaData {
return NewDaDataCustomClient(apiKey, secretKey, &http.Client{})
}
// NewDaDataCustomClient Create new custom client of DaData. By example, this option should be used to Google AppEngine:
// ctx := appengine.NewContext(request)
// appEngineClient := urlfetch.Client(ctx)
// daData:= NewDaDataCustomClient(apiKey, secretKey, appEngineClient)
func NewDaDataCustomClient(apiKey, secretKey string, httpClient *http.Client) *DaData {
return &DaData{
apiKey: apiKey,
secretKey: secretKey,
httpClient: httpClient,
}
}
func (daData *DaData) sendRequest(lastURLPart string, source interface{}, result interface{}) error {
buffer := &bytes.Buffer{}
if encodeErr := json.NewEncoder(buffer).Encode(source); nil != encodeErr {
fmt.Printf("encodeErr: %v", encodeErr)
return encodeErr
}
request, requestErr := http.NewRequest("POST", baseURL+lastURLPart, buffer)
if nil != requestErr {
fmt.Printf("requestErr: %v", requestErr)
return requestErr
}
request.Header.Add("Authorization", fmt.Sprintf("Token %s", daData.apiKey))
request.Header.Add("X-Secret", daData.secretKey)
request.Header.Add("Content-Type", "application/json")
request.Header.Add("Accept", "application/json")
request.Header.Set("Connection", "close")
response, httpErr := daData.httpClient.Do(request)
if nil != httpErr {
fmt.Printf("httpErr: %v", httpErr)
return httpErr
}
defer response.Body.Close()
if http.StatusOK != response.StatusCode {
return fmt.Errorf("Request error %v", response.Status)
}
if decodeErr := json.NewDecoder(response.Body).Decode(&result); nil != decodeErr {
fmt.Printf("decodeErr: %v", decodeErr)
return decodeErr
}
return nil
}