Skip to content

Commit

Permalink
fix whitespace in query
Browse files Browse the repository at this point in the history
  • Loading branch information
martinhny committed Oct 18, 2023
1 parent 51b24ce commit 31dcdbb
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 11 deletions.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func init() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelInfo,
Level: slog.LevelDebug,
}))
slog.SetDefault(logger)
}
Expand All @@ -25,6 +25,7 @@ func main() {
if cfg.Debug {
logLevel := &slog.LevelVar{}
logLevel.Set(slog.LevelDebug)
slog.Debug("Debug mode on")
}

smseagle := smseagle.NewSMSEagle(cfg)
Expand Down
2 changes: 1 addition & 1 deletion pkg/alerter/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (g *Grafana) handleRequest(w http.ResponseWriter, r *http.Request, c Contac

message := SMSEagleMessage{
Receiver: receiver,
Message: fmt.Sprintf("%s", webhook.Title),
Message: fmt.Sprintf("test: %s", webhook.Title),
ContactType: c,
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/smseagle/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ import (
)

func sendSMS(cfg *config.ProxyConfig, phoneNumber string, message string) error {
requestUrl := fmt.Sprintf("%s/http_api/send_sms?access_token=%s&to=%s&message=%s", cfg.SMS.Url, cfg.SMS.AccessToken, phoneNumber, message)
requestUrl := fmt.Sprintf("http://%s/http_api/send_sms?access_token=%s&to=%s&message=%s", cfg.SMS.Url, cfg.SMS.AccessToken, phoneNumber, message)
slog.Debug("Sending sms", "url", requestUrl)
res, err := http.Get(requestUrl)
if err != nil {
return err
}
slog.Debug("sms request succesfull", "response", res)
slog.Debug("sms request succesful", "response code", res.StatusCode, "response text", res.Body)
return nil
}

func call(cfg *config.ProxyConfig, phoneNumber string) error {
requestUrl := fmt.Sprintf("%s/http_api/call_with_termination?access_token=%s&to=%s", cfg.Call.Url, cfg.Call.AccessToken, phoneNumber)
requestUrl := fmt.Sprintf("http://%s/http_api/call_with_termination?access_token=%s&to=%s", cfg.Call.Url, cfg.Call.AccessToken, phoneNumber)
slog.Debug("Sending call request", "url", requestUrl)
res, err := http.Get(requestUrl)
if err != nil {
return err
}
slog.Debug("Call request successful", "response", res)
slog.Debug("Call request successful", "response", res.Body)
return nil
}
4 changes: 3 additions & 1 deletion pkg/smseagle/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package smseagle
import (
"kartverket.no/smseagle-proxy/pkg/config"
"log/slog"
"strings"
)

type SMSEagleMessage struct {
Expand Down Expand Up @@ -46,7 +47,8 @@ func (s *SMSEagle) Notify(message *SMSEagleMessage) error {
}

if message.ContactType == SMS {
err := sendSMS(s.cfg, phoneNumber, message.Message)
msg := strings.ReplaceAll(message.Message, " ", "+")
err := sendSMS(s.cfg, phoneNumber, msg)
if err != nil {
slog.Error("Error sending sms", "error", err)
return err
Expand Down
9 changes: 5 additions & 4 deletions pkg/smseagle/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ var _ = Describe("Handler", func() {
AppdriftPhoneNumber: "123",
InfraPhoneNumber: "456",
Call: config.SMSEagleConfig{
Url: server.URL(),
Url: server.Addr(),
AccessToken: "calltoken",
},
SMS: config.SMSEagleConfig{
Url: server.URL(),
Url: server.Addr(),
AccessToken: "smstoken",
},
}
Expand All @@ -39,8 +39,9 @@ var _ = Describe("Handler", func() {

Context("appdrift alert", func() {
It("should make get requests with correct queries to sms and call", func() {
msg := SMSEagleMessage{Message: "hei", Receiver: Appdrift}
expectedSMSQuery := fmt.Sprintf("access_token=%s&to=%s&message=%s", cfg.SMS.AccessToken, cfg.AppdriftPhoneNumber, msg.Message)
msg := SMSEagleMessage{Message: "hei pa deg", Receiver: Appdrift}
exptectedSMSMsg := "hei+pa+deg"
expectedSMSQuery := fmt.Sprintf("access_token=%s&to=%s&message=%s", cfg.SMS.AccessToken, cfg.AppdriftPhoneNumber, exptectedSMSMsg)
expectedCallQuery := fmt.Sprintf("access_token=%s&to=%s", cfg.Call.AccessToken, cfg.AppdriftPhoneNumber)
server.AppendHandlers(
ghttp.CombineHandlers(
Expand Down

0 comments on commit 31dcdbb

Please sign in to comment.