File size: 1,409 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package mocktwilio

import (
	"encoding/json"
	"net/http"
	"path"
	"strconv"

	"github.com/nyaruka/phonenumbers"
	"github.com/target/goalert/notification/twilio"
)

func (s *Server) serveLookup(w http.ResponseWriter, req *http.Request) {
	number := path.Base(req.URL.Path)
	inclCarrier := req.URL.Query().Get("Type") == "carrier"

	var info struct {
		CallerName *struct{}           `json:"caller_name"`
		Carrier    *twilio.CarrierInfo `json:"carrier"`
		CC         string              `json:"country_code"`
		Fmt        string              `json:"national_format"`
		Number     string              `json:"phone_number"`
		AddOns     *struct{}           `json:"add_ons"`
		URL        string              `json:"url"`
	}
	n, err := phonenumbers.Parse(number, "")
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	info.CC = strconv.Itoa(int(n.GetCountryCode()))
	info.Fmt = phonenumbers.Format(n, phonenumbers.NATIONAL)
	info.Number = phonenumbers.Format(n, phonenumbers.E164)
	req.URL.Host = req.Host
	info.URL = req.URL.String()

	if inclCarrier {
		s.carrierInfoMx.Lock()
		c, ok := s.carrierInfo[info.Number]
		s.carrierInfoMx.Unlock()
		if ok {
			info.Carrier = &c
		}
	}

	data, err := json.Marshal(info)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	w.Header().Set("Content-Type", "application/json")
	_, _ = w.Write(data)
}