Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sundowndev
GitHub Repository: sundowndev/phoneinfoga
Path: blob/master/lib/remote/suppliers/numverify.go
994 views
1
package suppliers
2
3
import (
4
"encoding/json"
5
"errors"
6
"fmt"
7
"github.com/sirupsen/logrus"
8
"net/http"
9
)
10
11
type NumverifySupplierInterface interface {
12
Request() NumverifySupplierRequestInterface
13
}
14
15
type NumverifySupplierRequestInterface interface {
16
SetApiKey(string) NumverifySupplierRequestInterface
17
ValidateNumber(string) (*NumverifyValidateResponse, error)
18
}
19
20
type NumverifyErrorResponse struct {
21
Message string `json:"message"`
22
}
23
24
// NumverifyValidateResponse REST API response
25
type NumverifyValidateResponse struct {
26
Valid bool `json:"valid"`
27
Number string `json:"number"`
28
LocalFormat string `json:"local_format"`
29
InternationalFormat string `json:"international_format"`
30
CountryPrefix string `json:"country_prefix"`
31
CountryCode string `json:"country_code"`
32
CountryName string `json:"country_name"`
33
Location string `json:"location"`
34
Carrier string `json:"carrier"`
35
LineType string `json:"line_type"`
36
}
37
38
type NumverifySupplier struct {
39
Uri string
40
}
41
42
func NewNumverifySupplier() *NumverifySupplier {
43
return &NumverifySupplier{
44
Uri: "https://api.apilayer.com",
45
}
46
}
47
48
type NumverifyRequest struct {
49
apiKey string
50
uri string
51
}
52
53
func (s *NumverifySupplier) Request() NumverifySupplierRequestInterface {
54
return &NumverifyRequest{uri: s.Uri}
55
}
56
57
func (r *NumverifyRequest) SetApiKey(k string) NumverifySupplierRequestInterface {
58
r.apiKey = k
59
return r
60
}
61
62
func (r *NumverifyRequest) ValidateNumber(internationalNumber string) (res *NumverifyValidateResponse, err error) {
63
logrus.
64
WithField("number", internationalNumber).
65
Debug("Running validate operation through Numverify API")
66
67
url := fmt.Sprintf("%s/number_verification/validate?number=%s", r.uri, internationalNumber)
68
69
// Build the request
70
client := &http.Client{}
71
req, _ := http.NewRequest("GET", url, nil)
72
req.Header.Set("Apikey", r.apiKey)
73
74
response, err := client.Do(req)
75
76
if err != nil {
77
return nil, err
78
}
79
defer response.Body.Close()
80
81
// Fill the response with the data from the JSON
82
var result NumverifyValidateResponse
83
84
if response.StatusCode >= 400 {
85
errorResponse := NumverifyErrorResponse{}
86
if err := json.NewDecoder(response.Body).Decode(&errorResponse); err != nil {
87
return nil, err
88
}
89
return nil, errors.New(errorResponse.Message)
90
}
91
92
// Use json.Decode for reading streams of JSON data
93
if err := json.NewDecoder(response.Body).Decode(&result); err != nil {
94
return nil, err
95
}
96
97
res = &NumverifyValidateResponse{
98
Valid: result.Valid,
99
Number: result.Number,
100
LocalFormat: result.LocalFormat,
101
InternationalFormat: result.InternationalFormat,
102
CountryPrefix: result.CountryPrefix,
103
CountryCode: result.CountryCode,
104
CountryName: result.CountryName,
105
Location: result.Location,
106
Carrier: result.Carrier,
107
LineType: result.LineType,
108
}
109
110
return res, nil
111
}
112
113