Skip to content

Commit ddf9490

Browse files
committed
Finish documentation
1 parent a39bcba commit ddf9490

14 files changed

+225
-185
lines changed

README.md

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
1-
# go-http-client
1+
# httpsms-go
22

3-
[![Build](https://github.com/NdoleStudio/go-http-client/actions/workflows/main.yml/badge.svg)](https://github.com/NdoleStudio/go-http-client/actions/workflows/main.yml)
4-
[![codecov](https://codecov.io/gh/NdoleStudio/go-http-client/branch/main/graph/badge.svg)](https://codecov.io/gh/NdoleStudio/go-http-client)
5-
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/NdoleStudio/go-http-client/badges/quality-score.png?b=main)](https://scrutinizer-ci.com/g/NdoleStudio/go-http-client/?branch=main)
6-
[![Go Report Card](https://goreportcard.com/badge/github.com/NdoleStudio/go-http-client)](https://goreportcard.com/report/github.com/NdoleStudio/go-http-client)
7-
[![GitHub contributors](https://img.shields.io/github/contributors/NdoleStudio/go-http-client)](https://github.com/NdoleStudio/go-http-client/graphs/contributors)
8-
[![GitHub license](https://img.shields.io/github/license/NdoleStudio/go-http-client?color=brightgreen)](https://github.com/NdoleStudio/go-http-client/blob/master/LICENSE)
9-
[![PkgGoDev](https://pkg.go.dev/badge/github.com/NdoleStudio/go-http-client)](https://pkg.go.dev/github.com/NdoleStudio/go-http-client)
3+
[![Build](https://github.com/NdoleStudio/httpsms-go/actions/workflows/main.yml/badge.svg)](https://github.com/NdoleStudio/httpsms-go/actions/workflows/main.yml)
4+
[![codecov](https://codecov.io/gh/NdoleStudio/httpsms-go/branch/main/graph/badge.svg)](https://codecov.io/gh/NdoleStudio/httpsms-go)
5+
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/NdoleStudio/httpsms-go/badges/quality-score.png?b=main)](https://scrutinizer-ci.com/g/NdoleStudio/httpsms-go/?branch=main)
6+
[![Go Report Card](https://goreportcard.com/badge/github.com/NdoleStudio/httpsms-go)](https://goreportcard.com/report/github.com/NdoleStudio/httpsms-go)
7+
[![GitHub contributors](https://img.shields.io/github/contributors/NdoleStudio/httpsms-go)](https://github.com/NdoleStudio/httpsms-go/graphs/contributors)
8+
[![GitHub license](https://img.shields.io/github/license/NdoleStudio/httpsms-go?color=brightgreen)](https://github.com/NdoleStudio/httpsms-go/blob/master/LICENSE)
9+
[![PkgGoDev](https://pkg.go.dev/badge/github.com/NdoleStudio/httpsms-go)](https://pkg.go.dev/github.com/NdoleStudio/httpsms-go)
1010

1111

12-
This package provides a generic `go` client template for an HTTP API
12+
This package provides a generic `go` client template for the Http SMS Api
1313

1414
## Installation
1515

16-
`go-http-client` is compatible with modern Go releases in module mode, with Go installed:
16+
`httpsms-go` is compatible with modern Go releases in module mode, with Go installed:
1717

1818
```bash
19-
go get github.com/NdoleStudio/go-http-client
19+
go get github.com/NdoleStudio/httpsms-go
2020
```
2121

2222
Alternatively the same can be achieved if you use `import` in a package:
2323

2424
```go
25-
import "github.com/NdoleStudio/go-http-client"
25+
import "github.com/NdoleStudio/httpsms-go"
2626
```
2727

2828

2929
## Implemented
3030

31-
- [Status Codes](#status-codes)
32-
- `GET /200`: OK
31+
- [Messages](#messages)
32+
- `POST /v1/messages/send`: Send a new SMS Message
3333

3434
## Usage
3535

3636
### Initializing the Client
3737

38-
An instance of the client can be created using `New()`.
38+
An instance of the client can be created using `httpsms.New()`.
3939

4040
```go
4141
package main
4242

4343
import (
44-
"github.com/NdoleStudio/go-http-client"
44+
"github.com/NdoleStudio/httpsms-go"
4545
)
4646

4747
func main() {
48-
statusClient := client.New(client.WithDelay(200))
48+
client := htpsms.New(htpsms.WithDelay(200))
4949
}
5050
```
5151

@@ -54,24 +54,28 @@ func main() {
5454
All API calls return an `error` as the last return object. All successful calls will return a `nil` error.
5555

5656
```go
57-
status, response, err := statusClient.Status.Ok(context.Background())
57+
_, response, err := client.Messages.Send(context.Background())
5858
if err != nil {
5959
//handle error
6060
}
6161
```
6262

63-
### Status Codes
63+
### Messages
6464

65-
#### `GET /200`: OK
65+
#### `POST /v1/messages/send`: Send a new SMS Message
6666

6767
```go
68-
status, response, err := statusClient.Status.Ok(context.Background())
68+
message, response, err := client.Messages.Send(context.Background(), &MessageSendParams{
69+
Content: "This is a sample text message",
70+
From: "+18005550199",
71+
To: "+18005550100",
72+
})
6973

7074
if err != nil {
7175
log.Fatal(err)
7276
}
7377

74-
log.Println(status.Description) // OK
78+
log.Println(message.Code) // 202
7579
```
7680

7781
## Testing

client.go

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package client
1+
package httpsms
22

33
import (
44
"bytes"
@@ -8,7 +8,6 @@ import (
88
"io"
99
"io/ioutil"
1010
"net/http"
11-
"strconv"
1211
)
1312

1413
type service struct {
@@ -21,9 +20,9 @@ type Client struct {
2120
httpClient *http.Client
2221
common service
2322
baseURL string
24-
delay int
23+
apiKey string
2524

26-
Status *statusService
25+
Messages *messagesService
2726
}
2827

2928
// New creates and returns a new campay.Client from a slice of campay.ClientOption.
@@ -36,12 +35,12 @@ func New(options ...Option) *Client {
3635

3736
client := &Client{
3837
httpClient: config.httpClient,
39-
delay: config.delay,
38+
apiKey: config.apiKey,
4039
baseURL: config.baseURL,
4140
}
4241

4342
client.common.client = client
44-
client.Status = (*statusService)(&client.common)
43+
client.Messages = (*messagesService)(&client.common)
4544
return client
4645
}
4746

@@ -67,24 +66,11 @@ func (client *Client) newRequest(ctx context.Context, method, uri string, body i
6766

6867
req.Header.Set("Content-Type", "application/json")
6968
req.Header.Set("Accept", "application/json")
70-
71-
if client.delay > 0 {
72-
client.addURLParams(req, map[string]string{"sleep": strconv.Itoa(client.delay)})
73-
}
69+
req.Header.Set("x-api-key", client.apiKey)
7470

7571
return req, nil
7672
}
7773

78-
// addURLParams adds urls parameters to an *http.Request
79-
func (client *Client) addURLParams(request *http.Request, params map[string]string) *http.Request {
80-
q := request.URL.Query()
81-
for key, value := range params {
82-
q.Add(key, value)
83-
}
84-
request.URL.RawQuery = q.Encode()
85-
return request
86-
}
87-
8874
// do carries out an HTTP request and returns a Response
8975
func (client *Client) do(req *http.Request) (*Response, error) {
9076
if req == nil {

client_config.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
package client
1+
package httpsms
22

33
import "net/http"
44

55
type clientConfig struct {
66
httpClient *http.Client
7-
delay int
7+
apiKey string
88
baseURL string
99
}
1010

1111
func defaultClientConfig() *clientConfig {
1212
return &clientConfig{
1313
httpClient: http.DefaultClient,
14-
delay: 0,
15-
baseURL: "https://httpstat.us",
14+
baseURL: "https://api.httpsms.com",
1615
}
1716
}

client_option.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package client
1+
package httpsms
22

33
import (
44
"net/http"
@@ -26,7 +26,7 @@ func WithHTTPClient(httpClient *http.Client) Option {
2626
})
2727
}
2828

29-
// WithBaseURL set's the base url for the flutterwave API
29+
// WithBaseURL sets the base url for the httpsms API
3030
func WithBaseURL(baseURL string) Option {
3131
return clientOptionFunc(func(config *clientConfig) {
3232
if baseURL != "" {
@@ -35,12 +35,9 @@ func WithBaseURL(baseURL string) Option {
3535
})
3636
}
3737

38-
// WithDelay sets the delay in milliseconds before a response is gotten.
39-
// The delay must be > 0 for it to be used.
40-
func WithDelay(delay int) Option {
38+
// WithAPIKey sets the api key for the httpsms API
39+
func WithAPIKey(apiKey string) Option {
4140
return clientOptionFunc(func(config *clientConfig) {
42-
if delay > 0 {
43-
config.delay = delay
44-
}
41+
config.apiKey = apiKey
4542
})
4643
}

client_option_test.go

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package client
1+
package httpsms
22

33
import (
44
"net/http"
@@ -71,34 +71,19 @@ func TestWithBaseURL(t *testing.T) {
7171
})
7272
}
7373

74-
func TestWithDelay(t *testing.T) {
75-
t.Run("delay is set successfully", func(t *testing.T) {
74+
func TestWithAPIKey(t *testing.T) {
75+
t.Run("api key is set successfully", func(t *testing.T) {
7676
// Setup
7777
t.Parallel()
7878

7979
// Arrange
8080
config := defaultClientConfig()
81-
delay := 1
81+
apiKey := "x-api-key"
8282

8383
// Act
84-
WithDelay(delay).apply(config)
84+
WithAPIKey(apiKey).apply(config)
8585

8686
// Assert
87-
assert.Equal(t, delay, config.delay)
88-
})
89-
90-
t.Run("delay is not set when value < 0", func(t *testing.T) {
91-
// Setup
92-
t.Parallel()
93-
94-
// Arrange
95-
config := defaultClientConfig()
96-
delay := -1
97-
98-
// Act
99-
WithDelay(delay).apply(config)
100-
101-
// Assert
102-
assert.Equal(t, 0, config.delay)
87+
assert.Equal(t, apiKey, config.apiKey)
10388
})
10489
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/NdoleStudio/go-http-client
1+
module github.com/NdoleStudio/httpsms-go
22

33
go 1.17
44

http_status.go

Lines changed: 0 additions & 6 deletions
This file was deleted.

internal/stubs/message.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package stubs
2+
3+
// MessagesSendResponse response from the /v1/messages/send endpoint
4+
func MessagesSendResponse() []byte {
5+
return []byte(`
6+
{
7+
"data": {
8+
"contact": "+18005550100",
9+
"content": "This is a sample text message",
10+
"created_at": "2022-06-05T14:26:02.302718+03:00",
11+
"failure_reason": "",
12+
"id": "32343a19-da5e-4b1b-a767-3298a73703cb",
13+
"last_attempted_at": "2022-06-05T14:26:09.527976+03:00",
14+
"order_timestamp": "2022-06-05T14:26:09.527976+03:00",
15+
"owner": "+18005550199",
16+
"received_at": "2022-06-05T14:26:09.527976+03:00",
17+
"request_received_at": "2022-06-05T14:26:01.520828+03:00",
18+
"send_time": 133414,
19+
"sent_at": "2022-06-05T14:26:09.527976+03:00",
20+
"status": "pending",
21+
"type": "mobile-terminated",
22+
"updated_at": "2022-06-05T14:26:10.303278+03:00",
23+
"user_id": "WB7DRDWrJZRGbYrv2CKGkqbzvqdC"
24+
},
25+
"message": "message created successfully",
26+
"status": "success"
27+
}
28+
`)
29+
}
30+
31+
// MessagesSendErrorResponse internal error response
32+
func MessagesSendErrorResponse() []byte {
33+
return []byte(`
34+
{
35+
"message": "We ran into an internal error while handling the request.",
36+
"status": "error"
37+
}
38+
`)
39+
}

messages.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package httpsms
2+
3+
import "time"
4+
5+
// MessageSendParams is the request payload for sending a message
6+
type MessageSendParams struct {
7+
Content string `json:"content"`
8+
From string `json:"from"`
9+
To string `json:"to"`
10+
}
11+
12+
// MessageResponse is the response gotten with a message content
13+
type MessageResponse struct {
14+
Data Message `json:"data"`
15+
Message string `json:"message"`
16+
Status string `json:"status"`
17+
}
18+
19+
// Message represents and incoming or outgoing SMS message
20+
type Message struct {
21+
Contact string `json:"contact"`
22+
Content string `json:"content"`
23+
CreatedAt time.Time `json:"created_at"`
24+
FailureReason string `json:"failure_reason"`
25+
ID string `json:"id"`
26+
LastAttemptedAt time.Time `json:"last_attempted_at"`
27+
OrderTimestamp time.Time `json:"order_timestamp"`
28+
Owner string `json:"owner"`
29+
ReceivedAt time.Time `json:"received_at"`
30+
RequestReceivedAt time.Time `json:"request_received_at"`
31+
SendTime int `json:"send_time"`
32+
SentAt time.Time `json:"sent_at"`
33+
Status string `json:"status"`
34+
Type string `json:"type"`
35+
UpdatedAt time.Time `json:"updated_at"`
36+
UserID string `json:"user_id"`
37+
}

messages_service.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package httpsms
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"net/http"
7+
)
8+
9+
// messagesService is the API client for the `/` endpoint
10+
type messagesService service
11+
12+
// Send adds a new SMS message to be sent by the android phone
13+
//
14+
// API Docs: https://api.httpsms.com/index.html#/Messages/post_messages_send
15+
func (service *messagesService) Send(ctx context.Context, params *MessageSendParams) (*MessageResponse, *Response, error) {
16+
request, err := service.client.newRequest(ctx, http.MethodPost, "/v1/messages/send", params)
17+
if err != nil {
18+
return nil, nil, err
19+
}
20+
21+
response, err := service.client.do(request)
22+
if err != nil {
23+
return nil, response, err
24+
}
25+
26+
message := new(MessageResponse)
27+
if err = json.Unmarshal(*response.Body, message); err != nil {
28+
return nil, response, err
29+
}
30+
31+
return message, response, nil
32+
}

0 commit comments

Comments
 (0)