Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sundowndev
GitHub Repository: sundowndev/phoneinfoga
Path: blob/master/web/v2/api/handlers/numbers.go
994 views
1
package handlers
2
3
import (
4
"github.com/gin-gonic/gin"
5
"github.com/sundowndev/phoneinfoga/v2/lib/number"
6
"github.com/sundowndev/phoneinfoga/v2/web/v2/api"
7
"net/http"
8
)
9
10
type AddNumberInput struct {
11
Number string `json:"number" binding:"number,required"`
12
}
13
14
type AddNumberResponse struct {
15
Valid bool `json:"valid"`
16
RawLocal string `json:"rawLocal"`
17
Local string `json:"local"`
18
E164 string `json:"e164"`
19
International string `json:"international"`
20
CountryCode int32 `json:"countryCode"`
21
Country string `json:"country"`
22
Carrier string `json:"carrier"`
23
}
24
25
// AddNumber is an HTTP handler
26
// @ID AddNumber
27
// @Tags Numbers
28
// @Summary Add a new number.
29
// @Description This route returns information about a given phone number.
30
// @Accept json
31
// @Produce json
32
// @Param request body AddNumberInput true "Request body"
33
// @Success 200 {object} AddNumberResponse
34
// @Success 500 {object} api.ErrorResponse
35
// @Router /v2/numbers [post]
36
func AddNumber(ctx *gin.Context) *api.Response {
37
var input AddNumberInput
38
if err := ctx.ShouldBindJSON(&input); err != nil {
39
return &api.Response{
40
Code: http.StatusBadRequest,
41
JSON: true,
42
Data: api.ErrorResponse{Error: "Invalid phone number: please provide an integer without any special chars"},
43
}
44
}
45
46
num, err := number.NewNumber(input.Number)
47
if err != nil {
48
return &api.Response{
49
Code: http.StatusBadRequest,
50
JSON: true,
51
Data: api.ErrorResponse{Error: err.Error()},
52
}
53
}
54
55
return &api.Response{
56
Code: http.StatusOK,
57
JSON: true,
58
Data: AddNumberResponse{
59
Valid: num.Valid,
60
RawLocal: num.RawLocal,
61
Local: num.Local,
62
E164: num.E164,
63
International: num.International,
64
CountryCode: num.CountryCode,
65
Country: num.Country,
66
Carrier: num.Carrier,
67
},
68
}
69
}
70
71