forked from sendgrid/rest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9795e6b
commit 9f8dd38
Showing
5 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,4 @@ _testmain.go | |
*.exe | ||
*.test | ||
*.prof | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package rest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package rest |