-
Notifications
You must be signed in to change notification settings - Fork 2
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
0 parents
commit 0bcce17
Showing
9 changed files
with
632 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Changelog | ||
|
||
## 0.1.0 | ||
This is the first release. It contains full support for Dead Man's Snitch API. See the [godocs](https://godoc.org/github.com/prometheus/client_golang) for details. |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 Premiere Global Services, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,76 @@ | ||
# go-deadmanssnitch [](http://godoc.org/github.com/PremiereGlobal/go-deadmanssnitch) [](https://goreportcard.com/report/github.com/PremiereGlobal/go-deadmanssnitch) [](https://github.com/PremiereGlobal/go-deadmanssnitch/blob/master/LICENSE) | ||
go-deadmanssnitch is a [Go](http://golang.org/) client library for the [Dead Man's Snitch](https://deadmanssnitch.com/docs/api/v1) API. | ||
|
||
## Installation | ||
Make sure you have a working Go environment. To install, simply run: | ||
|
||
``` | ||
go get github.com/PremiereGlobal/go-deadmanssnitch | ||
``` | ||
|
||
## Usage | ||
|
||
``` | ||
package main | ||
import ( | ||
"github.com/PremiereGlobal/go-deadmansnitch" | ||
) | ||
var apiKey = "" // Set your api key here | ||
func main() { | ||
client := deadmanssnitch.NewClient(apiKey) | ||
... | ||
} | ||
``` | ||
|
||
For more information, read the [godoc package documentation](http://godoc.org/github.com/PremiereGlobal/go-deadmanssnitch). | ||
|
||
## Examples | ||
|
||
### Check-In | ||
|
||
``` | ||
var snitchToken = "" // Set your snitch token here | ||
_, err := client.CheckIn(snitchToken) | ||
if err != nil { | ||
panic(err) | ||
} | ||
``` | ||
|
||
### List All Snitches | ||
|
||
``` | ||
snitches, err := client.ListSnitches([]string{}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
``` | ||
|
||
### Create Snitch | ||
|
||
``` | ||
snitch := deadmanssnitch.Snitch { | ||
Name: "testSnitch", | ||
Interval: "hourly", | ||
AlertType: "basic", | ||
Tags: []string{"test"}, | ||
Notes: "This is an example snitch", | ||
} | ||
snitches, err := client.CreateSnitch(snitch) | ||
if err != nil { | ||
panic(err) | ||
} | ||
``` | ||
|
||
## Testing the Client Library | ||
Tests will validate API calls by creating a test snitch, checking in and updating the snitch using all of the methods. It will then delete the snitch after waiting `wait` seconds (to allow for manual verification). | ||
|
||
``` | ||
go test -v --args -apikey=<apiKey> -wait 30 | ||
``` |
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,29 @@ | ||
package deadmanssnitch | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
apiEndpoint = "https://api.deadmanssnitch.com" | ||
apiVersion = "v1" | ||
) | ||
|
||
// Client is the Dead Man's Snitch API client | ||
type Client struct { | ||
httpClient *http.Client | ||
apiBaseURL string | ||
apiKey string | ||
} | ||
|
||
// NewClient creates a new API client | ||
func NewClient(apiKey string) (*Client, error) { | ||
client := &Client{ | ||
httpClient: &http.Client{}, | ||
apiBaseURL: fmt.Sprintf("%s/%s", apiEndpoint, apiVersion), | ||
apiKey: apiKey, | ||
} | ||
|
||
return client, nil | ||
} |
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,3 @@ | ||
module github.com/PremiereGlobal/go-deadmanssnitch | ||
|
||
go 1.12 |
Empty file.
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,72 @@ | ||
package deadmanssnitch | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
// ErrorResponse represents the structure of a API error | ||
type ErrorResponse struct { | ||
ErrorType string `json:"type"` | ||
ErrorString string `json:"error"` | ||
Validations []Validation `json:"validations"` | ||
} | ||
|
||
// Validation contains the detials of a API field validation error | ||
type Validation struct { | ||
Attribute string `json:"attribute"` | ||
Message string `json:"message"` | ||
} | ||
|
||
func (c *Client) do(method string, path string, body []byte) ([]byte, error) { | ||
request, err := http.NewRequest(method, fmt.Sprintf("%s/%s", c.apiBaseURL, path), bytes.NewBuffer(body)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
request.Header.Set("Content-Type", "application/json") | ||
request.SetBasicAuth(c.apiKey, "") | ||
|
||
resp, err := c.httpClient.Do(request) | ||
return c.checkResponse(resp, err) | ||
} | ||
|
||
func (c *Client) checkResponse(response *http.Response, err error) ([]byte, error) { | ||
if err != nil { | ||
return nil, fmt.Errorf("Error calling the API endpoint: %v", err) | ||
} | ||
|
||
defer response.Body.Close() | ||
|
||
bodyBytes, err := ioutil.ReadAll(response.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("Error reading the API response: %v", err) | ||
} | ||
|
||
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNoContent { | ||
errorResponse := ErrorResponse{} | ||
err = json.Unmarshal(bodyBytes, &errorResponse) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
errorString := "" | ||
|
||
if response.StatusCode == http.StatusUnprocessableEntity { | ||
// Fields invalid | ||
for _, v := range errorResponse.Validations { | ||
errorString = errorString + fmt.Sprintf("%s: %s, ", v.Attribute, v.Message) | ||
} | ||
} else { | ||
// Generic error | ||
errorString = fmt.Sprintf("%s: %s", errorResponse.ErrorType, errorResponse.ErrorString) | ||
} | ||
|
||
return nil, fmt.Errorf("Error requesting %s %s, HTTP %d. %s", response.Request.Method, response.Request.URL, response.StatusCode, errorString) | ||
} | ||
|
||
return bodyBytes, nil | ||
} |
Oops, something went wrong.