Skip to content

Commit

Permalink
Go fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
thiperson committed Feb 18, 2022
1 parent a19b005 commit 68d865b
Showing 1 changed file with 38 additions and 39 deletions.
77 changes: 38 additions & 39 deletions provider/sfr/sfr.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package sfr

import (
"encoding/json"
"fmt"
"github.com/messagebird/sachet"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
Expand All @@ -12,29 +12,28 @@ import (

// Config is the configuration struct for Sfr provider
type Config struct {
URL string `yaml:"url"`
SPACEID string `yaml:"space_id"`
SERVICEID string `yaml:"service_id"`
URL string `yaml:"url"`
SPACEID string `yaml:"space_id"`
SERVICEID string `yaml:"service_id"`
SERVICEPASSWORD string `yaml:"service_password"`
LANG string `yaml:"lang"`
TPOA string `yaml:"tpoa"`
LANG string `yaml:"lang"`
TPOA string `yaml:"tpoa"`
}

type Authenticate struct {
ServiceId string `json:"serviceId"`
ServicePassword string `json:"servicePassword"`
SpaceId string `json:"spaceId"`
Lang string `json:"lang"`
ServiceId string `json:"serviceId"`
ServicePassword string `json:"servicePassword"`
SpaceId string `json:"spaceId"`
Lang string `json:"lang"`
}

type MessageUnitaire struct {
Media string `json:"media"`
TextMsg string `json:"textMsg"`
To string `json:"to"`
From string `json:"from"`
Media string `json:"media"`
TextMsg string `json:"textMsg"`
To string `json:"to"`
From string `json:"from"`
}


type ResponseBody struct {
Success bool `json:"success"`
ErrorCode string `json:"errorCode"`
Expand Down Expand Up @@ -67,35 +66,35 @@ func (c *Sfr) Send(message sachet.Message) error {
// No \n in Text tolerated
msg := strings.ReplaceAll(message.Text, "\n", " - ")

error := 0
error := 0

for _, dest := range message.To {
for _, dest := range message.To {

request, err := http.NewRequest("GET", c.URL, nil)
if err != nil {
return err
}

authenticate := &Authenticate{
ServiceId: c.SERVICEID,
ServicePassword: c.SERVICEPASSWORD,
SpaceId: c.SPACEID,
Lang: c.LANG,
}
authenticate := &Authenticate{
ServiceId: c.SERVICEID,
ServicePassword: c.SERVICEPASSWORD,
SpaceId: c.SPACEID,
Lang: c.LANG,
}

var params = request.URL.Query()

messageUnitaire:= &MessageUnitaire{
Media: "SMSLong",
TextMsg: msg,
To: dest,
From: c.TPOA,
}
a, _ := json.Marshal(authenticate)
mU, _ := json.Marshal(messageUnitaire)

params.Add("authenticate", string(a) )
params.Add("messageUnitaire", string(mU))
messageUnitaire := &MessageUnitaire{
Media: "SMSLong",
TextMsg: msg,
To: dest,
From: c.TPOA,
}
a, _ := json.Marshal(authenticate)
mU, _ := json.Marshal(messageUnitaire)

params.Add("authenticate", string(a))
params.Add("messageUnitaire", string(mU))
request.URL.RawQuery = params.Encode()

response, err := c.HTTPClient.Do(request)
Expand All @@ -104,22 +103,22 @@ func (c *Sfr) Send(message sachet.Message) error {
fmt.Println(err)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
body, err := ioutil.ReadAll(response.Body)

var responseBody ResponseBody
if err := json.Unmarshal(body, &responseBody); err != nil { // Parse []byte to go struct pointer
fmt.Errorf("Can not unmarshal JSON")
if err := json.Unmarshal(body, &responseBody); err != nil { // Parse []byte to go struct pointer
fmt.Errorf("Can not unmarshal JSON")
error += 1
}

if responseBody.Success != true{
if responseBody.Success != true {
fmt.Println("API error :", responseBody)
error += 1
} else {
fmt.Println("Successfully sent alert to ", dest)
}
}
if error > 0{
if error > 0 {
return fmt.Errorf("Error with %d calls", error)
}
return nil
Expand Down

0 comments on commit 68d865b

Please sign in to comment.