Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Delivery Reports #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[[constraint]]
name = "github.com/shopspring/decimal"
version = "1.1.0"

[prune]
go-tests = true
unused-packages = true
54 changes: 50 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package infobip
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"time"
)

const (
Expand All @@ -12,6 +15,9 @@ const (

//AdvancedMessagePath for sending advanced messages
AdvancedMessagePath = "sms/1/text/advanced"

// ReportsPath for getting SMS reports.
ReportsPath = "/sms/1/reports"
)

// HTTPInterface helps Infobip tests
Expand All @@ -30,10 +36,12 @@ type Client struct {
// ClientWithBasicAuth returns a pointer to infobip.Client with Infobip funcs
func ClientWithBasicAuth(username, password string) *Client {
return &Client{
BaseURL: "https://api.infobip.com/",
Username: username,
Password: password,
HTTPClient: &http.Client{},
BaseURL: "https://api.infobip.com/",
Username: username,
Password: password,
HTTPClient: &http.Client{
Timeout: 5 * time.Second,
},
}
}

Expand Down Expand Up @@ -63,6 +71,16 @@ func (c Client) AdvancedMessage(m BulkMessage) (r Response, err error) {
return
}

func (c Client) GetDeliveryReport(smsID string) (*SmsReportResponse, error) {
res := SmsReportResponse{}
err := c.doRequest("GET", c.BaseURL+ReportsPath+"?messageId="+smsID, nil, &res)
if err != nil {
return nil, err
}

return &res, nil
}

func (c Client) defaultRequest(b []byte, path string) (r Response, err error) {
req, err := http.NewRequest(http.MethodPost, c.BaseURL+path, bytes.NewBuffer(b))
if err != nil {
Expand All @@ -79,3 +97,31 @@ func (c Client) defaultRequest(b []byte, path string) (r Response, err error) {
err = json.NewDecoder(resp.Body).Decode(&r)
return
}

func (c *Client) doRequest(method string, path string, payload io.Reader, result interface{}) error {
req, err := http.NewRequest(method, path, payload)
if err != nil {
return err
}

req.SetBasicAuth(c.Username, c.Password)

req.Header.Add("Content-Type", "application/json")
req.Header.Add("Cache-Control", "no-cache")
req.Header.Add("User-Agent", "go-infobip/0.1")

resp, err := c.HTTPClient.Do(req)
if err != nil {
resp.Body.Close()
return err
}
defer resp.Body.Close()

if resp.StatusCode > 299 {
return errors.New(resp.Status)
}

err = json.NewDecoder(resp.Body).Decode(result)

return err
}
66 changes: 66 additions & 0 deletions delivery_reports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package infobip

import (
"github.com/shopspring/decimal"
)

// Amount is a thin wrapper around decimal to support json unmarshal.
type Amount struct {
decimal.Decimal
}

// UnmarshalJSON is an implementation of Unmarshaler interface for the Amount type.
func (a *Amount) UnmarshalJSON(data []byte) error {
v, err := decimal.NewFromString(string(data))
if err != nil {
return err
}
a.Decimal = v
return nil
}

// SentSmsStatus indicates whether the message is successfully sent,
// not sent, delivered, not delivered, waiting for delivery or any other possible status.
type SentSmsStatus struct {
GroupID int `json:"groupId"`
GroupName string `json:"groupName"`
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Action string `json:"action"`
}

// SentSmsPrice is a Sent SMS price info: amount and currency.
type SentSmsPrice struct {
PricePerMessage Amount `json:"pricePerMessage"`
Currency string `json:"currency"`
}

// SentSmsError indicates whether the error occurred during the query execution.
type SentSmsError struct {
GroupID int `json:"groupId"`
GroupName string `json:"groupName"`
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Permanent bool `json:"permanent"`
}

// SentSmsReport is a message-specific delivery report.
type SentSmsReport struct {
BulkID string `json:"bulkId"`
To string `json:"to"`
SentAt string `json:"sentAt"`
DoneAt string `json:"doneAt"`
Status SentSmsStatus `json:"status"`
SmsCount int `json:"smsCount"`
MessageID string `json:"messageId"`
MccMnc string `json:"mccMnc"`
Price SentSmsPrice `json:"price"`
Error SentSmsError `json:"error"`
}

// SmsReportResponse contains a collection of reports, one per every message.
type SmsReportResponse struct {
Results []SentSmsReport `json:"results"`
}
15 changes: 9 additions & 6 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ type BulkMessage struct {

// Message contains the body request
type Message struct {
From string `json:"from,omitempty"`
Destinations []Destination `json:"destinations,omitempty"`
To string `json:"to,omitempty"`
Text string `json:"text"`
Transliteration string `json:"transliteration,omitempty"`
LanguageCode string `json:"languageCode,omitempty"`
From string `json:"from,omitempty"`
Destinations []Destination `json:"destinations,omitempty"`
To string `json:"to,omitempty"`
Text string `json:"text"`
Transliteration string `json:"transliteration,omitempty"`
LanguageCode string `json:"languageCode,omitempty"`
NotifyURL string `json:"notifyUrl,omitempty"`
NotifyContentType string `json:"notifyContentType,omitempty"`
CallbackData string `json:"callbackData,omitempty"`
}

// Destination contains the recipient
Expand Down
2 changes: 2 additions & 0 deletions vendor/github.com/shopspring/decimal/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions vendor/github.com/shopspring/decimal/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions vendor/github.com/shopspring/decimal/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading