Skip to content

Commit

Permalink
First working prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkingserious committed Mar 26, 2016
1 parent 9795e6b commit 9f8dd38
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ _testmain.go
*.exe
*.test
*.prof
.env
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package rest
122 changes: 122 additions & 0 deletions examples/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package main

import (
"fmt"
"github.com/sendgrid/rest"
"os"
"encoding/json"
)

func main() {

// Build the URL
//host := "https://api.sendgrid.com"
host := "https://e9sk3d3bfaikbpdq7.stoplight-proxy.io"
version := "/v3"
endpoint := "/api_keys"
baseURL := host + version + endpoint

// Build the request headers
key := os.Getenv("SENDGRID_API_KEY")
requestHeaders := make(map[string]string)
requestHeaders["Content-Type"] = "application/json"
requestHeaders["Authorization"] = "Bearer " + key

// GET Collection
method := "GET"

// Build the query parameters
queryParams := make(map[string]string)
queryParams["limit"] = "100"
queryParams["offset"] = "0"

// Make the API call
request := rest.Request{
Method: method,
BaseURL: baseURL,
RequestHeaders: requestHeaders,
QueryParams: queryParams,
}
response, e := rest.API(request)
if e != nil {
fmt.Println(e)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.ResponseBody)
fmt.Println(response.ResponseHeaders)
}

// POST
method = "POST"

var requestBody = []byte(` {
"name": "My API Key",
"scopes": [
"mail.send",
"alerts.create",
"alerts.read"
]
}`)
request = rest.Request{
Method: method,
BaseURL: baseURL,
RequestHeaders: requestHeaders,
QueryParams: queryParams,
RequestBody: requestBody,
}
response, e = rest.API(request)
if e != nil {
fmt.Println(e)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.ResponseBody)
fmt.Println(response.ResponseHeaders)
}

// Get a perticular return value
b := []byte(response.ResponseBody)
var f interface{}
err := json.Unmarshal(b, &f)
if err != nil {
fmt.Println(err)
}
m := f.(map[string]interface{})
apiKey := m["api_key_id"].(string)

// GET Single
method = "GET"

// Make the API call
request = rest.Request{
Method: method,
BaseURL: baseURL+"/"+apiKey,
RequestHeaders: requestHeaders,
QueryParams: queryParams,
}
response, e = rest.API(request)
if e != nil {
fmt.Println(e)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.ResponseBody)
fmt.Println(response.ResponseHeaders)
}

// DELETE
method = "DELETE"

request = rest.Request{
Method: method,
BaseURL: baseURL+"/"+apiKey,
RequestHeaders: requestHeaders,
QueryParams: queryParams,
RequestBody: requestBody,
}
response, e = rest.API(request)
if e != nil {
fmt.Println(e)
} else {
fmt.Println(response.StatusCode)
fmt.Println(response.ResponseHeaders)
}
}
83 changes: 83 additions & 0 deletions rest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package rest

import (
"bytes"
"io/ioutil"
"net/http"
"net/url"
)

// Request holds the request to an API Call
type Request struct {
Method string
BaseURL string
RequestHeaders map[string]string
QueryParams map[string]string
RequestBody []byte
}

// Response holds the response from an API call
type Response struct {
StatusCode int // e.g. 200
ResponseBody string // e.g. {"result: success"}
ResponseHeaders map[string][]string // e.g. map[X-Ratelimit-Limit:[600]]
}

// BuildURL adds query paramaters to the URL
func BuildURL(baseURL string, queryParams map[string]string) (string) {
baseURL += "?"
params := url.Values{}
for key, value := range queryParams {
params.Add(key, value)
}
return baseURL + params.Encode()
}

// BuildRequest creates the HTTP request object
func BuildRequest(request Request) (*http.Request, error) {
req, e := http.NewRequest(request.Method, request.BaseURL, bytes.NewBuffer(request.RequestBody))
for key, value := range request.RequestHeaders {
req.Header.Set(key, value)
}
return req, e
}

// MakeRequest makes the API call
func MakeRequest(req *http.Request) (*http.Response, error) {
var Client = &http.Client{
Transport: http.DefaultTransport,
}
res, e := Client.Do(req)
return res, e
}

// BuildResponse builds the response struct
func BuildResponse(res *http.Response) (Response, error) {
var response Response
response.StatusCode = res.StatusCode
body, e := ioutil.ReadAll(res.Body)
defer res.Body.Close()
response.ResponseBody = string(body)
response.ResponseHeaders = res.Header
return response, e
}

// API allows for quick and easy access any REST or REST-like API.
func API(request Request) (Response, error) {

// Build the final URL
if len(request.QueryParams) != 0 {
request.BaseURL = BuildURL(request.BaseURL, request.QueryParams)
}

// Build the http request object
req, e := BuildRequest(request)

// Build the HTTP client and make the request
res, e := MakeRequest(req)

// Build Response object
response, e := BuildResponse(res)

return response, e
}
1 change: 1 addition & 0 deletions rest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package rest

0 comments on commit 9f8dd38

Please sign in to comment.