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

[WIP] Initialization #1

Open
wants to merge 18 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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Bin
target/
target/*
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: go
go:
- 1.10.x
before_install:
- curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
- dep ensure

script: make travis
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.

38 changes: 38 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# 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/davecgh/go-spew"
version = "1.1.1"

[prune]
go-tests = true
unused-packages = true

[[constraint]]
name = "github.com/google/go-querystring"
version = "1.0.0"
54 changes: 54 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# GoWinds Makefile
#///////////////////////#
#/// DEFS ///#
#///////////////////////#
# Don't ask, for to understand it is to look
# into the void and know the void is not only
#looking back but also reading your emails.
SHELL=/bin/bash -e -o pipefail

# ENV Vars defaults
GOOS ?= darwin
GOARCH ?= amd64
VERSION ?= v0.1

#///////////////////////#
#/// OUTPUT ///#
#///////////////////////#

RED=\033[0;31m
GREEN=\033[0;32m
YELLOW=\033[01;33m
BLUE=\033[0;34m
LBLUE=\033[01;34m
ORANGE=\033[0;33m
PURPLE=\033[0;35m
LCYAN=\033[1;36m
NC=\033[0m

#///////////////////////#
#/// TARGETS ///#
#///////////////////////#

.PHONY: help

help: ## Show this help.
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'

.PHONY: test cover build buildall clean

travis: clean test build

test: ## Run test with coverage
go test -v -race -cover -coverprofile=cov.out

cover: ## Open coverage
go tool cover --html=cov.out

build: ## Build for testing
go build -o target/gowinds

clean: ## Clean build
@go clean
@rm -rf target
@rm -rf cov.out
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# GoWinds
[![Build Status](https://travis-ci.org/openwurl/gowinds.svg?branch=master)](https://travis-ci.org/openwurl/gowinds)
A basic Go client to HighWinds CDN API
6 changes: 6 additions & 0 deletions analytics/analytics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package analytics

// Service interface defines the available analytics methods
type Service interface {
//GetAllXFer(reqOpt *RequestOptions, anaOpt *AnalyticsOptions) (*StatusObject, error) // /analytics/transfer
}
163 changes: 163 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package gowinds

import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"

"github.com/google/go-querystring/query"
)

const (
baseURL = "https://striketracker.highwinds.com"
basePath = "api/v1/accounts"
mediaType = "application/json"
applicationID = "gowinds"
)

// RequestOptions specifies global API parameters for every call
type RequestOptions struct {
// AccountHash is required and variable
AccountHash string `url:"account_hash,omitempty"`
}

// createURL concatenates the account hash at request time
func (r *RequestOptions) createURL() string {
url := fmt.Sprintf("%s/%s", basePath, r.AccountHash)
return url
}

// Response is the API call response
type Response struct {
*http.Response
}

// Logger interface is the default logger
type logger interface {
Printf(string, ...interface{})
}

// Client is our core universal API client
type Client struct {
client *http.Client
debug bool
AuthorizationHeaderToken string
BaseURL *url.URL
logger
// Services
}

// SetLogger sets the logger
func (c *Client) SetLogger(l logger) error {
c.logger = l
return nil
}

// SetDebug enables/disables debug logging after creating a client
func (c *Client) SetDebug(toggle bool) {
c.debug = toggle
return
}

// SetBaseURL Sets optional BaseURL just in case - mostly for tests
func (c *Client) SetBaseURL(u string) error {
url, err := url.Parse(u)
if err != nil {
return err
}
c.BaseURL = url
return nil
}

// NewClient returns a copy of the client
func NewClient(authorizationHeaderToken string) (*Client, error) {

// Fetch auth data
if authorizationHeaderToken == "" || len(authorizationHeaderToken) == 0 {
return nil, fmt.Errorf("authorizationHeaderToken required")
}

// URL object parsed
u, err := url.Parse(baseURL)
if err != nil {
return nil, err
}

c := &Client{client: &http.Client{}, AuthorizationHeaderToken: authorizationHeaderToken, BaseURL: u, debug: false}
// Mount services
// c.Analytics
// c.Hosts
return c, nil
}

// NewRequest packgs a new http request
func (c *Client) NewRequest(method, path string, body interface{}) (*http.Request, error) {
// validate and have obj
rel, err := url.Parse(path)
if err != nil {
return nil, err
}

// build url and queries
u := c.BaseURL.ResolveReference(rel)
v, _ := query.Values(body)
u.RawQuery = v.Encode()

// create raw request, body is not used on striketracker
req, err := http.NewRequest(method, u.String(), nil)
if err != nil {
return nil, err
}

if c.debug {
c.logger.Printf("Request: ", req)
}

req.Close = true

// pack headers
req.Header.Add("Authorization", c.AuthorizationHeaderToken)
req.Header.Add("X-Application-Id", applicationID)
req.Header.Add("Content-Type", mediaType)

return req, nil
}

// Do fires the request
func (c *Client) Do(req *http.Request, v interface{}) (*Response, error) {
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}

defer resp.Body.Close()

response := Response{Response: resp}

if v != nil {
// Just in case we use an io.Writer to decode elsewhere
if w, ok := v.(io.Writer); ok {
io.Copy(w, resp.Body)
} else {
// Decode in the struct injected in v
err = json.NewDecoder(resp.Body).Decode(v)
if err != nil {
return &response, err
}
}
}

return &response, err
}

// DoRequest creates and fires a request
func (c *Client) DoRequest(method, path string, body, v interface{}) (*Response, error) {
req, err := c.NewRequest(method, path, body)
if err != nil {
return nil, err
}

return c.Do(req, v)
}
Loading