This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
numberinsight.go
175 lines (143 loc) · 5.44 KB
/
numberinsight.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package vonage
import (
"context"
"github.com/antihax/optional"
"github.com/vonage/vonage-go-sdk/internal/numberinsight"
)
// NumberInsightClient for working with the NumberInsight API
type NumberInsightClient struct {
Config *numberinsight.Configuration
apiKey string
apiSecret string
}
// NewNumberInsightClient Creates a new NumberInsight Client, supplying an Auth to work with
func NewNumberInsightClient(Auth Auth) *NumberInsightClient {
client := new(NumberInsightClient)
creds := Auth.GetCreds()
client.apiKey = creds[0]
client.apiSecret = creds[1]
client.Config = numberinsight.NewConfiguration()
client.Config.UserAgent = GetUserAgent()
return client
}
type NiErrorResponse struct {
Status int32
StatusMessage string
}
type NiOpts struct {
Country string
}
type NiResponseJsonBasic struct {
Status numberinsight.NiBasicStatus
StatusMessage string
RequestId string
InternationalFormatNumber string
NationalFormatNumber string
CountryCode string
CountryCodeIso3 string
CountryName string
CountryPrefix string
}
// Basic does a basic-level lookup for data about a number
func (client *NumberInsightClient) Basic(number string, opts NiOpts) (NiResponseJsonBasic, NiErrorResponse, error) {
// create the client
numberinsightClient := numberinsight.NewAPIClient(client.Config)
niOpts := numberinsight.GetNumberInsightBasicOpts{}
if opts.Country != "" {
niOpts.Country = optional.NewString(opts.Country)
}
// we need context for the API key
ctx := context.Background()
ctx = context.WithValue(ctx, numberinsight.ContextAPIKey, numberinsight.APIKey{Key: client.apiKey})
ctx = context.WithValue(ctx, numberinsight.ContextAPISecret, numberinsight.APIKey{Key: client.apiSecret})
result, _, err := numberinsightClient.DefaultApi.GetNumberInsightBasic(ctx, "json", number, &niOpts)
// catch HTTP errors
if err != nil {
return NiResponseJsonBasic{}, NiErrorResponse{}, err
}
if result.Status != 0 {
errResp := NiErrorResponse{
Status: int32(result.Status),
StatusMessage: result.StatusMessage,
}
return NiResponseJsonBasic(result), errResp, nil
}
return NiResponseJsonBasic(result), NiErrorResponse{}, nil
}
type NiResponseJsonStandard struct {
Status numberinsight.NiBasicStatus
StatusMessage string
RequestId string
InternationalFormatNumber string
NationalFormatNumber string
CountryCode string
CountryCodeIso3 string
CountryName string
CountryPrefix string
RequestPrice string
RefundPrice string
RemainingBalance string
CurrentCarrier numberinsight.NiCurrentCarrierProperties
OriginalCarrier numberinsight.NiInitialCarrierProperties
Ported string
Roaming numberinsight.NiRoaming
CallerIdentity numberinsight.NiCallerIdentity
CallerName string
LastName string
FirstName string
CallerType string
}
// Standard does a Standard-level lookup for data about a number
func (client *NumberInsightClient) Standard(number string, opts NiOpts) (NiResponseJsonStandard, NiErrorResponse, error) {
// create the client
numberinsightClient := numberinsight.NewAPIClient(client.Config)
niOpts := numberinsight.GetNumberInsightStandardOpts{}
// we need context for the API key
ctx := context.Background()
ctx = context.WithValue(ctx, numberinsight.ContextAPIKey, numberinsight.APIKey{Key: client.apiKey})
ctx = context.WithValue(ctx, numberinsight.ContextAPISecret, numberinsight.APIKey{Key: client.apiSecret})
result, _, err := numberinsightClient.DefaultApi.GetNumberInsightStandard(ctx, "json", number, &niOpts)
// catch HTTP errors
if err != nil {
return NiResponseJsonStandard{}, NiErrorResponse{}, err
}
if result.Status != 0 {
errResp := NiErrorResponse{
Status: int32(result.Status),
StatusMessage: result.StatusMessage,
}
return NiResponseJsonStandard(result), errResp, nil
}
return NiResponseJsonStandard(result), NiErrorResponse{}, nil
}
type NiResponseAsync struct {
RequestId string
Number string
RemainingBalance string
RequestPrice string
Status numberinsight.NiStandardAdvancedStatus
StatusMessage string
}
// AdvancedAsync requests a callback with advanced-level information about a number
func (client *NumberInsightClient) AdvancedAsync(number string, callback string, opts NiOpts) (NiResponseAsync, NiErrorResponse, error) {
// create the client
numberinsightClient := numberinsight.NewAPIClient(client.Config)
niOpts := numberinsight.GetNumberInsightAsyncOpts{}
// we need context for the API key
ctx := context.Background()
ctx = context.WithValue(ctx, numberinsight.ContextAPIKey, numberinsight.APIKey{Key: client.apiKey})
ctx = context.WithValue(ctx, numberinsight.ContextAPISecret, numberinsight.APIKey{Key: client.apiSecret})
result, _, err := numberinsightClient.DefaultApi.GetNumberInsightAsync(ctx, "json", callback, number, &niOpts)
// catch HTTP errors
if err != nil {
return NiResponseAsync{}, NiErrorResponse{}, err
}
if result.Status != 0 {
errResp := NiErrorResponse{
Status: int32(result.Status),
StatusMessage: result.StatusMessage,
}
return NiResponseAsync(result), errResp, nil
}
return NiResponseAsync(result), NiErrorResponse{}, nil
}