Skip to content

Commit

Permalink
Finish documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
AchoArnold committed Jul 23, 2022
1 parent a39bcba commit ddf9490
Show file tree
Hide file tree
Showing 14 changed files with 225 additions and 185 deletions.
48 changes: 26 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
# go-http-client
# httpsms-go

[![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)
[![codecov](https://codecov.io/gh/NdoleStudio/go-http-client/branch/main/graph/badge.svg)](https://codecov.io/gh/NdoleStudio/go-http-client)
[![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)
[![Go Report Card](https://goreportcard.com/badge/github.com/NdoleStudio/go-http-client)](https://goreportcard.com/report/github.com/NdoleStudio/go-http-client)
[![GitHub contributors](https://img.shields.io/github/contributors/NdoleStudio/go-http-client)](https://github.com/NdoleStudio/go-http-client/graphs/contributors)
[![GitHub license](https://img.shields.io/github/license/NdoleStudio/go-http-client?color=brightgreen)](https://github.com/NdoleStudio/go-http-client/blob/master/LICENSE)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/NdoleStudio/go-http-client)](https://pkg.go.dev/github.com/NdoleStudio/go-http-client)
[![Build](https://github.com/NdoleStudio/httpsms-go/actions/workflows/main.yml/badge.svg)](https://github.com/NdoleStudio/httpsms-go/actions/workflows/main.yml)
[![codecov](https://codecov.io/gh/NdoleStudio/httpsms-go/branch/main/graph/badge.svg)](https://codecov.io/gh/NdoleStudio/httpsms-go)
[![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)
[![Go Report Card](https://goreportcard.com/badge/github.com/NdoleStudio/httpsms-go)](https://goreportcard.com/report/github.com/NdoleStudio/httpsms-go)
[![GitHub contributors](https://img.shields.io/github/contributors/NdoleStudio/httpsms-go)](https://github.com/NdoleStudio/httpsms-go/graphs/contributors)
[![GitHub license](https://img.shields.io/github/license/NdoleStudio/httpsms-go?color=brightgreen)](https://github.com/NdoleStudio/httpsms-go/blob/master/LICENSE)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/NdoleStudio/httpsms-go)](https://pkg.go.dev/github.com/NdoleStudio/httpsms-go)


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

## Installation

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

```bash
go get github.com/NdoleStudio/go-http-client
go get github.com/NdoleStudio/httpsms-go
```

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

```go
import "github.com/NdoleStudio/go-http-client"
import "github.com/NdoleStudio/httpsms-go"
```


## Implemented

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

## Usage

### Initializing the Client

An instance of the client can be created using `New()`.
An instance of the client can be created using `httpsms.New()`.

```go
package main

import (
"github.com/NdoleStudio/go-http-client"
"github.com/NdoleStudio/httpsms-go"
)

func main() {
statusClient := client.New(client.WithDelay(200))
client := htpsms.New(htpsms.WithDelay(200))
}
```

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

```go
status, response, err := statusClient.Status.Ok(context.Background())
_, response, err := client.Messages.Send(context.Background())
if err != nil {
//handle error
}
```

### Status Codes
### Messages

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

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

if err != nil {
log.Fatal(err)
}

log.Println(status.Description) // OK
log.Println(message.Code) // 202
```

## Testing
Expand Down
26 changes: 6 additions & 20 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package httpsms

import (
"bytes"
Expand All @@ -8,7 +8,6 @@ import (
"io"
"io/ioutil"
"net/http"
"strconv"
)

type service struct {
Expand All @@ -21,9 +20,9 @@ type Client struct {
httpClient *http.Client
common service
baseURL string
delay int
apiKey string

Status *statusService
Messages *messagesService
}

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

client := &Client{
httpClient: config.httpClient,
delay: config.delay,
apiKey: config.apiKey,
baseURL: config.baseURL,
}

client.common.client = client
client.Status = (*statusService)(&client.common)
client.Messages = (*messagesService)(&client.common)
return client
}

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

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")

if client.delay > 0 {
client.addURLParams(req, map[string]string{"sleep": strconv.Itoa(client.delay)})
}
req.Header.Set("x-api-key", client.apiKey)

return req, nil
}

// addURLParams adds urls parameters to an *http.Request
func (client *Client) addURLParams(request *http.Request, params map[string]string) *http.Request {
q := request.URL.Query()
for key, value := range params {
q.Add(key, value)
}
request.URL.RawQuery = q.Encode()
return request
}

// do carries out an HTTP request and returns a Response
func (client *Client) do(req *http.Request) (*Response, error) {
if req == nil {
Expand Down
7 changes: 3 additions & 4 deletions client_config.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package client
package httpsms

import "net/http"

type clientConfig struct {
httpClient *http.Client
delay int
apiKey string
baseURL string
}

func defaultClientConfig() *clientConfig {
return &clientConfig{
httpClient: http.DefaultClient,
delay: 0,
baseURL: "https://httpstat.us",
baseURL: "https://api.httpsms.com",
}
}
13 changes: 5 additions & 8 deletions client_option.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package httpsms

import (
"net/http"
Expand Down Expand Up @@ -26,7 +26,7 @@ func WithHTTPClient(httpClient *http.Client) Option {
})
}

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

// WithDelay sets the delay in milliseconds before a response is gotten.
// The delay must be > 0 for it to be used.
func WithDelay(delay int) Option {
// WithAPIKey sets the api key for the httpsms API
func WithAPIKey(apiKey string) Option {
return clientOptionFunc(func(config *clientConfig) {
if delay > 0 {
config.delay = delay
}
config.apiKey = apiKey
})
}
27 changes: 6 additions & 21 deletions client_option_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package httpsms

import (
"net/http"
Expand Down Expand Up @@ -71,34 +71,19 @@ func TestWithBaseURL(t *testing.T) {
})
}

func TestWithDelay(t *testing.T) {
t.Run("delay is set successfully", func(t *testing.T) {
func TestWithAPIKey(t *testing.T) {
t.Run("api key is set successfully", func(t *testing.T) {
// Setup
t.Parallel()

// Arrange
config := defaultClientConfig()
delay := 1
apiKey := "x-api-key"

// Act
WithDelay(delay).apply(config)
WithAPIKey(apiKey).apply(config)

// Assert
assert.Equal(t, delay, config.delay)
})

t.Run("delay is not set when value < 0", func(t *testing.T) {
// Setup
t.Parallel()

// Arrange
config := defaultClientConfig()
delay := -1

// Act
WithDelay(delay).apply(config)

// Assert
assert.Equal(t, 0, config.delay)
assert.Equal(t, apiKey, config.apiKey)
})
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/NdoleStudio/go-http-client
module github.com/NdoleStudio/httpsms-go

go 1.17

Expand Down
6 changes: 0 additions & 6 deletions http_status.go

This file was deleted.

39 changes: 39 additions & 0 deletions internal/stubs/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package stubs

// MessagesSendResponse response from the /v1/messages/send endpoint
func MessagesSendResponse() []byte {
return []byte(`
{
"data": {
"contact": "+18005550100",
"content": "This is a sample text message",
"created_at": "2022-06-05T14:26:02.302718+03:00",
"failure_reason": "",
"id": "32343a19-da5e-4b1b-a767-3298a73703cb",
"last_attempted_at": "2022-06-05T14:26:09.527976+03:00",
"order_timestamp": "2022-06-05T14:26:09.527976+03:00",
"owner": "+18005550199",
"received_at": "2022-06-05T14:26:09.527976+03:00",
"request_received_at": "2022-06-05T14:26:01.520828+03:00",
"send_time": 133414,
"sent_at": "2022-06-05T14:26:09.527976+03:00",
"status": "pending",
"type": "mobile-terminated",
"updated_at": "2022-06-05T14:26:10.303278+03:00",
"user_id": "WB7DRDWrJZRGbYrv2CKGkqbzvqdC"
},
"message": "message created successfully",
"status": "success"
}
`)
}

// MessagesSendErrorResponse internal error response
func MessagesSendErrorResponse() []byte {
return []byte(`
{
"message": "We ran into an internal error while handling the request.",
"status": "error"
}
`)
}
37 changes: 37 additions & 0 deletions messages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package httpsms

import "time"

// MessageSendParams is the request payload for sending a message
type MessageSendParams struct {
Content string `json:"content"`
From string `json:"from"`
To string `json:"to"`
}

// MessageResponse is the response gotten with a message content
type MessageResponse struct {
Data Message `json:"data"`
Message string `json:"message"`
Status string `json:"status"`
}

// Message represents and incoming or outgoing SMS message
type Message struct {
Contact string `json:"contact"`
Content string `json:"content"`
CreatedAt time.Time `json:"created_at"`
FailureReason string `json:"failure_reason"`
ID string `json:"id"`
LastAttemptedAt time.Time `json:"last_attempted_at"`
OrderTimestamp time.Time `json:"order_timestamp"`
Owner string `json:"owner"`
ReceivedAt time.Time `json:"received_at"`
RequestReceivedAt time.Time `json:"request_received_at"`
SendTime int `json:"send_time"`
SentAt time.Time `json:"sent_at"`
Status string `json:"status"`
Type string `json:"type"`
UpdatedAt time.Time `json:"updated_at"`
UserID string `json:"user_id"`
}
32 changes: 32 additions & 0 deletions messages_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package httpsms

import (
"context"
"encoding/json"
"net/http"
)

// messagesService is the API client for the `/` endpoint
type messagesService service

// Send adds a new SMS message to be sent by the android phone
//
// API Docs: https://api.httpsms.com/index.html#/Messages/post_messages_send
func (service *messagesService) Send(ctx context.Context, params *MessageSendParams) (*MessageResponse, *Response, error) {
request, err := service.client.newRequest(ctx, http.MethodPost, "/v1/messages/send", params)
if err != nil {
return nil, nil, err
}

response, err := service.client.do(request)
if err != nil {
return nil, response, err
}

message := new(MessageResponse)
if err = json.Unmarshal(*response.Body, message); err != nil {
return nil, response, err
}

return message, response, nil
}
Loading

0 comments on commit ddf9490

Please sign in to comment.