Path: blob/master/web/v2/api/handlers/numbers.go
1723 views
package handlers12import (3"github.com/gin-gonic/gin"4"github.com/sundowndev/phoneinfoga/v2/lib/number"5"github.com/sundowndev/phoneinfoga/v2/web/v2/api"6"net/http"7)89type AddNumberInput struct {10Number string `json:"number" binding:"number,required"`11}1213type AddNumberResponse struct {14Valid bool `json:"valid"`15RawLocal string `json:"rawLocal"`16Local string `json:"local"`17E164 string `json:"e164"`18International string `json:"international"`19CountryCode int32 `json:"countryCode"`20Country string `json:"country"`21Carrier string `json:"carrier"`22}2324// AddNumber is an HTTP handler25// @ID AddNumber26// @Tags Numbers27// @Summary Add a new number.28// @Description This route returns information about a given phone number.29// @Accept json30// @Produce json31// @Param request body AddNumberInput true "Request body"32// @Success 200 {object} AddNumberResponse33// @Success 500 {object} api.ErrorResponse34// @Router /v2/numbers [post]35func AddNumber(ctx *gin.Context) *api.Response {36var input AddNumberInput37if err := ctx.ShouldBindJSON(&input); err != nil {38return &api.Response{39Code: http.StatusBadRequest,40JSON: true,41Data: api.ErrorResponse{Error: "Invalid phone number: please provide an integer without any special chars"},42}43}4445num, err := number.NewNumber(input.Number)46if err != nil {47return &api.Response{48Code: http.StatusBadRequest,49JSON: true,50Data: api.ErrorResponse{Error: err.Error()},51}52}5354return &api.Response{55Code: http.StatusOK,56JSON: true,57Data: AddNumberResponse{58Valid: num.Valid,59RawLocal: num.RawLocal,60Local: num.Local,61E164: num.E164,62International: num.International,63CountryCode: num.CountryCode,64Country: num.Country,65Carrier: num.Carrier,66},67}68}697071