Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
v3.0.0 - complete overhaul for v2 API (#6)
Browse files Browse the repository at this point in the history
* v3.0.0 - complete overhaul for v2 API

* fix: swap io for ioutil

* Added unit tests and VCR

* Full test suite

* Fix lint

* Adds code coverage and test to build

* Adds README badges

* Fix code coverage commands

* fix: change device string

* Update CHANGELOG date
  • Loading branch information
Justintime50 authored Jun 22, 2021
1 parent 2ab0038 commit 08409b8
Show file tree
Hide file tree
Showing 47 changed files with 1,603 additions and 279 deletions.
29 changes: 26 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,35 @@ jobs:
uses: actions/checkout@v2
- name: Run Golangci Linter
uses: golangci/golangci-lint-action@v2
test:
name: Test with Coverage
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: "1.16"
- name: Checkout Repository
uses: actions/checkout@v2
- name: Run unit tests
run: |
go test -race -covermode atomic -coverprofile=covprofile -coverpkg=./... ./test
- name: Install goveralls
if: github.ref == 'refs/heads/main'
env:
GO111MODULE: off
run: go get github.com/mattn/goveralls
- name: Send coverage
if: github.ref == 'refs/heads/main'
env:
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: goveralls -coverprofile=covprofile -service=github
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, ubuntu-latest]
go: [1.14, 1.15]
os: [macos-latest, ubuntu-latest, windows-latest]
go: [1.14, 1.15, 1.16]
steps:
- name: Set up Go
uses: actions/setup-go@v2
Expand All @@ -24,4 +47,4 @@ jobs:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Build
run: go build
run: go build -o dist/tuneuptechnology-go
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
.idea
covprofile
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# CHANGELOG

## v3.0.0 (2021-06-21)

* Updates entire library to be compliant with the new v2 API (endpoints, HTTP methods, etc)
* Build requests via a Client now providing your email and apiKey
* Added optional BaseURL and Timeout options to client
* Module names are now plural
* The Client now checks if an email and apiKey is provided and raises an error if not
* Added unit tests (closes #1)
* Removed `PrettyPrint` helper function
* Adds missing fields to inventory
* Adds missing `created_at`, `updated_at`, `deleted_at` fields to all records
* Corrected various other data types
* Added `omitempty` to all JSON fields in each request

## v2.1.0 (2021-02-20)

* Changes `Response` to `makeHTTPRequest`
Expand Down
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## help - Display help about make targets for this Makefile
help:
@cat Makefile | grep '^## ' --color=never | cut -c4- | sed -e "`printf 's/ - /\t- /;'`" | column -s "`printf '\t'`" -t

## install - Install globally from source
install:
go build -o $(go env GOPATH)/bin/tuneuptechnology-go

## clean - Clean the project
clean:
rm dist
rm $(go env GOPATH)/bin/tuneuptechnology-go

## build - Build the project
build:
go build -o dist/tuneuptechnology-go

## test - Test the project
test:
go test ./test

## coverage - Get test coverage
coverage:
go test ./test -coverprofile=covprofile -coverpkg=./...

## lint - Lint the project
lint:
golangci-lint run

.PHONY: help install clean build test lint
49 changes: 38 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
The Go client library for the Tuneup Technology App.

[![Build Status](https://github.com/tuneuptechnology/tuneuptechnology-go/workflows/build/badge.svg)](https://github.com/tuneuptechnology/tuneuptechnology-go/actions)
[![Coverage Status](https://coveralls.io/repos/github/tuneuptechnology/tuneuptechnology-go/badge.svg?branch=main)](https://coveralls.io/github/tuneuptechnology/tuneuptechnology-go?branch=main)
[![Version](https://img.shields.io/github/v/tag/tuneuptechnology/tuneuptechnology-go)](https://github.com/tuneuptechnology/tuneuptechnology-go/releases)
[![Licence](https://img.shields.io/github/license/tuneuptechnology/tuneuptechnology-go)](LICENSE)

This library allows you to interact with the customers, tickets, inventory, and locations objects without needing to do the hard work of binding your calls and data to endpoints. Simply call an action such as `CreateCustomer` and pass some data and let the library do the rest.
Expand All @@ -19,20 +21,18 @@ go get -u github.com/tuneuptechnology/tuneuptechnology-go
package main

import (
"github.com/tuneuptechnology/tuneuptechnology-go"
"encoding/json"
"fmt"
"os"

"github.com/tuneuptechnology/tuneuptechnology-go"
)

func main() {
// Setup your email and API key
apiEmail := os.Getenv("API_EMAIL")
apiKey := os.Getenv("API_KEY")
client := tuneuptechnology.New(os.Getenv("API_EMAIL"), os.Getenv("API_KEY"))

// Create a customer passing in all required params
customer := tuneuptechnology.CreateCustomer(
customer := client.CreateCustomer(
&tuneuptechnology.Customer{
Auth: apiEmail,
APIKey: apiKey,
Firstname: "Jake",
Lastname: "Peralta",
Email: "[email protected]",
Expand All @@ -43,9 +43,12 @@ func main() {
},
)

tuneuptechnology.PrettyPrint(customer)
prettyJSON, err := json.MarshalIndent(customer, "", " ")
if err != nil {
fmt.Fprintln(os.Stderr, "error creating JSON:", err)
}
fmt.Printf("%s\n", string(prettyJSON))
}

```

Other examples can be found in the `/examples` directory. Alter according to your needs.
Expand All @@ -60,10 +63,34 @@ [email protected] API_KEY=123... go run create_customer.go

Up-to-date API documentation can be [found here](https://app.tuneuptechnology.com/docs/api).

## Development

When re-recording cassettes, comment out the `delete` tests until all other tests have recorded cassettes. This ensures the records that are required to exist will still be active. Once the retrieve and update cassettes have been recorded, uncomment the delete tests and run again to record those cassettes.

```bash
# Build the project
make build

# Install the project globally from source
make install

# Clean the executables
make clean

# Test the project
[email protected] API_KEY=123... make test

## Get test coverage
[email protected] API_KEY=123... make coverage

# Lint the project (requires golangci-lint be installed)
make lint
```

## Releasing

As a separate PR from the feature/bug PR:

1. Update the Version constant in `version.go`
1. Update the Version constant in `client.go`
1. Update `CHANGELOG`
1. Create a GitHub tag with proper Go version semantics (eg: v1.0.0)
86 changes: 68 additions & 18 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,96 @@ import (
"io/ioutil"
"log"
"net/http"
"strings"
"time"
)

// APIBaseURL sets up the HTTP client and response functionality
const APIBaseURL = "https://app.tuneuptechnology.com/api/"
type Client struct {
// APIEmail is the email of the user and is required on every request
APIEmail string
// APIKey is the API key of the user and is required on every request
APIKey string
// BaseURL is the base url where the API resides
BaseURL string
// Timeout is the timeout of each request made to the API
Timeout int
// Version is the version of this client library
Version string
// Client is an HTTP client used to make API requests
Client *http.Client
}

// Version is the client library version
const Version = "3.0.0"

// Timeout is the number of seconds before a request will timeout
const Timeout = 10

// UserAgent sets the user-agent for requests
const UserAgent = "TuneupTechnologyApp/GoClient/" + Version

// makeHTTPRequest requests a response from the API with supplied data
func makeHTTPRequest(data interface{}, endpoint string) map[string]interface{} {
jsonData, _ := json.Marshal(data)
// baseURL sets the base url of a request, if not specified, a default will be used
func (client *Client) baseURL() string {
if client.BaseURL != "" {
return client.BaseURL
}
return "https://app.tuneuptechnology.com/api"
}

client := &http.Client{
Timeout: time.Second * 10,
func (client *Client) httpClient() *http.Client {
if client.Client != nil {
return client.Client
}
return &http.Client{
Timeout: time.Second * time.Duration(client.Timeout),
}
}

request, err := client.Post(
endpoint,
"application/json",
bytes.NewBuffer(jsonData),
)
// New returns a new Client with the given API email and key
func New(apiEmail string, apiKey string) *Client {
if apiEmail == "" || apiKey == "" {
log.Fatal("apiEmail and apiKey are required to create a Client.")
}

return &Client{
APIEmail: apiEmail,
APIKey: apiKey,
}
}

// makeHTTPRequest requests a response from the API with supplied data
func (client *Client) makeHTTPRequest(method string, endpoint string, data interface{}) map[string]interface{} {
// Based on https://christiangiacomi.com/posts/simple-put-patch-request-go/
jsonData, err := json.Marshal(data)
if err != nil {
log.Fatal(err)
}

request, err := http.NewRequest(strings.ToUpper(method), endpoint, bytes.NewBuffer(jsonData))
request.Header.Set("Accept", "application/json")
request.Header.Set("User-Agent", UserAgent)
request.Header.Set("Email", client.APIEmail)
request.Header.Set("Api-Key", client.APIKey)
if err != nil {
log.Fatal(err)
}

response, err := client.httpClient().Do(request)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()

// Read the response data from the API
responseData, err := ioutil.ReadAll(request.Body)
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}

// Convert the response to a map
var response map[string]interface{}
err = json.Unmarshal(responseData, &response)
var responseJson map[string]interface{}
err = json.Unmarshal(responseData, &responseJson)
if err != nil {
log.Fatal(err)
}

return response
return responseJson
}
49 changes: 0 additions & 49 deletions customer.go

This file was deleted.

Loading

0 comments on commit 08409b8

Please sign in to comment.