-
Notifications
You must be signed in to change notification settings - Fork 9
/
query.go
42 lines (33 loc) · 922 Bytes
/
query.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
package wlc
import (
"context"
"net/http"
"net/url"
)
const (
kQueryURL = "http://api2.wlc.nppa.gov.cn/idcard/authentication/query"
kQueryTestURL = "https://wlc.nppa.gov.cn/test/authentication/query/"
)
func (c *client) Query(ctx context.Context, ai string) (*QueryResult, error) {
return c.query(ctx, kQueryURL, ai)
}
func (c *client) QueryTest(ctx context.Context, code, ai string) (*QueryResult, error) {
return c.query(ctx, kQueryTestURL+code, ai)
}
func (c *client) query(ctx context.Context, api, ai string) (*QueryResult, error) {
var aux = struct {
*Error
Data struct {
Result *QueryResult `json:"result"`
} `json:"data"`
}{}
var values = url.Values{}
values.Set("ai", ai)
if err := c.request(ctx, http.MethodGet, api, values, nil, &aux); err != nil {
return nil, err
}
if aux.Error != nil && aux.Error.ErrCode != 0 {
return nil, aux.Error
}
return aux.Data.Result, nil
}