Skip to content

Commit d432f52

Browse files
authored
Merge pull request #122 from vallerion/sync-sdk
Update SDK to actual API Reference
2 parents bdf6d9d + 953f5e2 commit d432f52

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+1877
-668
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ MessageBird's REST API for Go
22
=============================
33
This repository contains the open source Go client for MessageBird's REST API. Documentation can be found at: https://developers.messagebird.com.
44

5-
[![Build Status](https://travis-ci.org/messagebird/go-rest-api.svg?branch=master)](https://travis-ci.org/messagebird/go-rest-api) [![PkgGoDev](https://pkg.go.dev/badge/github.com/messagebird/go-rest-api/v8)](https://pkg.go.dev/github.com/messagebird/go-rest-api/v8)
5+
[![Build Status](https://travis-ci.org/messagebird/go-rest-api.svg?branch=master)](https://travis-ci.org/messagebird/go-rest-api) [![PkgGoDev](https://pkg.go.dev/badge/github.com/messagebird/go-rest-api/v9)](https://pkg.go.dev/github.com/messagebird/go-rest-api/v9)
66

77
Requirements
88
------------
@@ -15,15 +15,15 @@ Installation
1515
The easiest way to use the MessageBird API in your Go project is to install it using *go get*:
1616

1717
```
18-
$ go get github.com/messagebird/go-rest-api/v8
18+
$ go get github.com/messagebird/go-rest-api/v9
1919
```
2020

2121
Examples
2222
--------
2323
Here is a quick example on how to get started. Assuming the **go get** installation worked, you can import the messagebird package like this:
2424

2525
```go
26-
import "github.com/messagebird/go-rest-api/v8"
26+
import "github.com/messagebird/go-rest-api/v9"
2727
```
2828

2929
Then, create an instance of **messagebird.Client**. It can be used to access the MessageBird APIs.
@@ -69,8 +69,8 @@ For this reason, errors returned by the `voice` package are of type `voice.Error
6969
An example of "simple" error handling is shown in the example above. Let's look how we can gain more in-depth insight in what exactly went wrong:
7070

7171
```go
72-
import "github.com/messagebird/go-rest-api/v8"
73-
import "github.com/messagebird/go-rest-api/v8/sms"
72+
import "github.com/messagebird/go-rest-api/v9"
73+
import "github.com/messagebird/go-rest-api/v9/sms"
7474

7575
// ...
7676

@@ -91,7 +91,7 @@ if err != nil {
9191
`voice.ErrorResponse` is very similar, except that it holds `voice.Error` structs - those contain only `Code` and `Message` (not description!) fields:
9292

9393
```go
94-
import "github.com/messagebird/go-rest-api/v8/voice"
94+
import "github.com/messagebird/go-rest-api/v9/voice"
9595

9696
// ...
9797

UPGRADING.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,15 @@ As v7 introduces support for using the Verify API with email recipients, the `Ve
109109
Added [missed fields](https://github.com/messagebird/go-rest-api/pull/119/commits/f9331269238f1518dd35d798a0fbf251bb04bb62) in SMS API.
110110
Updated `sms.Delete` method so now in return only error or nil as result.
111111

112-
## `v8.0.0` -> `v8.1.0`
113-
### Update Conversations API
112+
## `v8.0.0` -> `v9.0.0`
113+
### General
114+
* New `PaginationRequest` instead of many in APIs.
115+
* Interface `messagebird.MessageBirdClient` instead of `messagebird.Client` and new `MockClient` for better testing.
116+
* List structures now have a plural ending instead of using word "List". Example: `ContactList` -> `Contacts`.
117+
* `conversations.ConversationStatus` replaced by `conversations.Status`.
118+
* `number.NumberPattern` replaced by `number.SearchPattern`.
119+
* Added [Partner Accounts API](https://developers.messagebird.com/api/partner/).
120+
### Conversations API
114121
* Replaced `conversations.CreateMessage` with `conversations.Reply` which send a new message to an existing conversation.
115122
* Replaced `conversations.ListMessages` with `conversations.ListConversationMessages` which fetch messages in indicated conversation.
116123
* Added `conversations.SendMessage` to send a message to a specific recipient in a specific platform.

api.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package messagebird
2+
3+
import (
4+
"net/url"
5+
"strconv"
6+
)
7+
8+
// PaginationRequest can be used to set pagination options in List().
9+
type PaginationRequest struct {
10+
Limit, Offset int
11+
}
12+
13+
func (cpr *PaginationRequest) QueryParams() string {
14+
if cpr == nil {
15+
return ""
16+
}
17+
18+
query := url.Values{}
19+
if cpr.Limit > 0 {
20+
query.Set("limit", strconv.Itoa(cpr.Limit))
21+
}
22+
if cpr.Offset >= 0 {
23+
query.Set("offset", strconv.Itoa(cpr.Offset))
24+
}
25+
26+
return query.Encode()
27+
}
28+
29+
// DefaultPagination provides reasonable values for List requests.
30+
var DefaultPagination = &PaginationRequest{
31+
Limit: 20,
32+
Offset: 0,
33+
}

balance/balance.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package balance
33
import (
44
"net/http"
55

6-
messagebird "github.com/messagebird/go-rest-api/v8"
6+
messagebird "github.com/messagebird/go-rest-api/v9"
77
)
88

99
// Balance describes your balance information.
@@ -17,7 +17,7 @@ const path = "balance"
1717

1818
// Read returns the balance information for the account that is associated with
1919
// the access key.
20-
func Read(c *messagebird.Client) (*Balance, error) {
20+
func Read(c messagebird.Client) (*Balance, error) {
2121
balance := &Balance{}
2222
if err := c.Request(balance, http.MethodGet, path, nil); err != nil {
2323
return nil, err

balance/balance_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"net/http"
55
"testing"
66

7-
messagebird "github.com/messagebird/go-rest-api/v8"
8-
"github.com/messagebird/go-rest-api/v8/internal/mbtest"
7+
messagebird "github.com/messagebird/go-rest-api/v9"
8+
"github.com/messagebird/go-rest-api/v9/internal/mbtest"
99
"github.com/stretchr/testify/assert"
1010
)
1111

client.go

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,13 @@ import (
2525

2626
const (
2727
// ClientVersion is used in User-Agent request header to provide server with API level.
28-
ClientVersion = "8.1.0"
28+
ClientVersion = "9.0.0"
2929

3030
// Endpoint points you to MessageBird REST API.
3131
Endpoint = "https://rest.messagebird.com"
3232

3333
// httpClientTimeout is used to limit http.Client waiting time.
3434
httpClientTimeout = 15 * time.Second
35-
36-
// voiceHost is the host name for the Voice API.
37-
voiceHost = "voice.messagebird.com"
3835
)
3936

4037
var (
@@ -45,13 +42,13 @@ var (
4542
// A Feature can be enabled
4643
type Feature int
4744

48-
type MessageBirdClient interface {
45+
type Client interface {
4946
Request(v interface{}, method, path string, data interface{}) error
5047
}
5148

52-
// Client is used to access API with a given key.
49+
// DefaultClient is used to access API with a given key.
5350
// Uses standard lib HTTP client internally, so should be reused instead of created as needed and it is safe for concurrent use.
54-
type Client struct {
51+
type DefaultClient struct {
5552
AccessKey string // The API access key.
5653
HTTPClient *http.Client // The HTTP client to send requests on.
5754
DebugLog *log.Logger // Optional logger for debugging purposes.
@@ -68,26 +65,25 @@ const (
6865
// errorReader reads the provided byte slice into an appropriate error.
6966
type errorReader func([]byte) error
7067

71-
var voiceErrorReader errorReader
68+
var customErrorReader errorReader
69+
70+
// SetErrorReader takes an errorReader that must parse raw JSON errors
71+
func SetErrorReader(r errorReader) {
72+
customErrorReader = r
73+
}
7274

7375
// New creates a new MessageBird client object.
74-
func New(accessKey string) *Client {
75-
return &Client{
76+
func New(accessKey string) *DefaultClient {
77+
return &DefaultClient{
7678
AccessKey: accessKey,
7779
HTTPClient: &http.Client{
7880
Timeout: httpClientTimeout,
7981
},
8082
}
8183
}
8284

83-
// SetVoiceErrorReader takes an errorReader that must parse raw JSON errors
84-
// returned from the Voice API.
85-
func SetVoiceErrorReader(r errorReader) {
86-
voiceErrorReader = r
87-
}
88-
8985
// Request is for internal use only and unstable.
90-
func (c *Client) Request(v interface{}, method, path string, data interface{}) error {
86+
func (c *DefaultClient) Request(v interface{}, method, path string, data interface{}) error {
9187
if !strings.HasPrefix(path, "https://") && !strings.HasPrefix(path, "http://") {
9288
path = fmt.Sprintf("%s/%s", Endpoint, path)
9389
}
@@ -156,17 +152,22 @@ func (c *Client) Request(v interface{}, method, path string, data interface{}) e
156152
return ErrUnexpectedResponse
157153
default:
158154
// Anything else than a 200/201/204/500 should be a JSON error.
159-
if uri.Host == voiceHost && voiceErrorReader != nil {
160-
return voiceErrorReader(responseBody)
155+
if customErrorReader != nil {
156+
return customErrorReader(responseBody)
161157
}
162158

163-
var errorResponse ErrorResponse
164-
if err := json.Unmarshal(responseBody, &errorResponse); err != nil {
165-
return err
166-
}
159+
return defaultErrorReader(responseBody)
160+
}
161+
}
167162

168-
return errorResponse
163+
func defaultErrorReader(b []byte) error {
164+
var errorResponse ErrorResponse
165+
166+
if err := json.Unmarshal(b, &errorResponse); err != nil {
167+
return fmt.Errorf("failed to unmarshal response json %s, error: %v", string(b), err)
169168
}
169+
170+
return errorResponse
170171
}
171172

172173
// prepareRequestBody takes untyped data and attempts constructing a meaningful

contact/contact.go

Lines changed: 18 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package contact
22

33
import (
4-
"errors"
5-
"fmt"
64
"net/http"
7-
"net/url"
8-
"strconv"
95
"time"
106

11-
messagebird "github.com/messagebird/go-rest-api/v8"
7+
messagebird "github.com/messagebird/go-rest-api/v9"
128
)
139

10+
// path represents the path to the Contacts resource.
11+
const path = "contacts"
12+
1413
// Contact gets returned by the API.
1514
type Contact struct {
1615
ID string
@@ -36,20 +35,15 @@ type Contact struct {
3635
UpdatedDatetime *time.Time
3736
}
3837

39-
type ContactList struct {
38+
type Contacts struct {
4039
Limit, Offset int
4140
Count, TotalCount int
4241
Items []Contact
4342
}
4443

45-
// ListOptions can be used to set pagination options in List().
46-
type ListOptions struct {
47-
Limit, Offset int
48-
}
49-
50-
// Request represents a contact for write operations, e.g. for creating a new
44+
// CreateRequest represents a contact for write operations, e.g. for creating a new
5145
// contact or updating an existing one.
52-
type Request struct {
46+
type CreateRequest struct {
5347
MSISDN string `json:"msisdn,omitempty"`
5448
FirstName string `json:"firstName,omitempty"`
5549
LastName string `json:"lastName,omitempty"`
@@ -59,20 +53,12 @@ type Request struct {
5953
Custom4 string `json:"custom4,omitempty"`
6054
}
6155

62-
// path represents the path to the Contacts resource.
63-
const path = "contacts"
64-
65-
// DefaultListOptions provides reasonable values for List().
66-
var DefaultListOptions = &ListOptions{
67-
Limit: 20,
68-
Offset: 0,
56+
type ViewRequest struct {
57+
MSISDN string `json:"msisdn,omitempty"`
58+
Name string `json:"firstName,omitempty"`
6959
}
7060

71-
func Create(c *messagebird.Client, contactRequest *Request) (*Contact, error) {
72-
if err := validateCreate(contactRequest); err != nil {
73-
return nil, err
74-
}
75-
61+
func Create(c messagebird.Client, contactRequest *CreateRequest) (*Contact, error) {
7662
contact := &Contact{}
7763
if err := c.Request(contact, http.MethodPost, path, contactRequest); err != nil {
7864
return nil, err
@@ -81,61 +67,27 @@ func Create(c *messagebird.Client, contactRequest *Request) (*Contact, error) {
8167
return contact, nil
8268
}
8369

84-
func validateCreate(contactRequest *Request) error {
85-
if contactRequest.MSISDN == "" {
86-
return errors.New("msisdn is required")
87-
}
88-
89-
return nil
90-
}
91-
9270
// Delete attempts deleting the contact with the provided ID. If nil is returned,
9371
// the resource was deleted successfully.
94-
func Delete(c *messagebird.Client, id string) error {
95-
if id == "" {
96-
return errors.New("id is required")
97-
}
98-
72+
func Delete(c messagebird.Client, id string) error {
9973
return c.Request(nil, http.MethodDelete, path+"/"+id, nil)
10074
}
10175

10276
// List retrieves a paginated list of contacts, based on the options provided.
10377
// It's worth noting DefaultListOptions.
104-
func List(c *messagebird.Client, options *ListOptions) (*ContactList, error) {
105-
query, err := listQuery(options)
106-
if err != nil {
107-
return nil, err
108-
}
109-
110-
contactList := &ContactList{}
111-
if err = c.Request(contactList, http.MethodGet, path+"?"+query, nil); err != nil {
78+
func List(c messagebird.Client, options *messagebird.PaginationRequest) (*Contacts, error) {
79+
contactList := &Contacts{}
80+
if err := c.Request(contactList, http.MethodGet, path+"?"+options.QueryParams(), nil); err != nil {
11281
return nil, err
11382
}
11483

11584
return contactList, nil
11685
}
11786

118-
func listQuery(options *ListOptions) (string, error) {
119-
if options.Limit < 10 {
120-
return "", fmt.Errorf("minimum limit is 10, got %d", options.Limit)
121-
}
122-
123-
if options.Offset < 0 {
124-
return "", fmt.Errorf("offset can not be negative")
125-
}
126-
127-
values := &url.Values{}
128-
129-
values.Set("limit", strconv.Itoa(options.Limit))
130-
values.Set("offset", strconv.Itoa(options.Offset))
131-
132-
return values.Encode(), nil
133-
}
134-
13587
// Read retrieves the information of an existing contact.
136-
func Read(c *messagebird.Client, id string) (*Contact, error) {
88+
func Read(c messagebird.Client, id string, req *ViewRequest) (*Contact, error) {
13789
contact := &Contact{}
138-
if err := c.Request(contact, http.MethodGet, path+"/"+id, nil); err != nil {
90+
if err := c.Request(contact, http.MethodGet, path+"/"+id, req); err != nil {
13991
return nil, err
14092
}
14193

@@ -144,7 +96,7 @@ func Read(c *messagebird.Client, id string) (*Contact, error) {
14496

14597
// Update updates the record referenced by id with any values set in contactRequest.
14698
// Do not set any values that should not be updated.
147-
func Update(c *messagebird.Client, id string, contactRequest *Request) (*Contact, error) {
99+
func Update(c messagebird.Client, id string, contactRequest *CreateRequest) (*Contact, error) {
148100
contact := &Contact{}
149101
if err := c.Request(contact, http.MethodPatch, path+"/"+id, contactRequest); err != nil {
150102
return nil, err

0 commit comments

Comments
 (0)