This repository has been archived on 2022-08-17. You can view files and clone it, but cannot push or open issues or pull requests.
dex/vendor/github.com/mailgun/mailgun-go/rest_shim.go

154 lines
5.1 KiB
Go
Raw Normal View History

2015-08-18 05:57:27 +05:30
package mailgun
import (
"fmt"
)
// The MailgunGoUserAgent identifies the client to the server, for logging purposes.
// In the event of problems requiring a human administrator's assistance,
// this user agent allows them to identify the client from human-generated activity.
const MailgunGoUserAgent = "mailgun-go/1.0.0"
// This error will be returned whenever a Mailgun API returns an error response.
// Your application can check the Actual field to see the actual HTTP response code returned.
// URL contains the base URL accessed, sans any query parameters.
type UnexpectedResponseError struct {
Expected []int
Actual int
URL string
2016-06-28 22:48:18 +05:30
Data []byte
2015-08-18 05:57:27 +05:30
}
// String() converts the error into a human-readable, logfmt-compliant string.
// See http://godoc.org/github.com/kr/logfmt for details on logfmt formatting.
func (e *UnexpectedResponseError) String() string {
2016-06-28 22:48:18 +05:30
return fmt.Sprintf("UnexpectedResponseError URL=%s ExpectedOneOf=%#v Got=%d Error: %s", e.URL, e.Expected, e.Actual, string(e.Data))
2015-08-18 05:57:27 +05:30
}
// Error() performs as String().
func (e *UnexpectedResponseError) Error() string {
return e.String()
}
// newError creates a new error condition to be returned.
2016-06-28 22:48:18 +05:30
func newError(url string, expected []int, got *httpResponse) error {
2015-08-18 05:57:27 +05:30
return &UnexpectedResponseError{
URL: url,
Expected: expected,
2016-06-28 22:48:18 +05:30
Actual: got.Code,
Data: got.Data,
2015-08-18 05:57:27 +05:30
}
}
// notGood searches a list of response codes (the haystack) for a matching entry (the needle).
// If found, the response code is considered good, and thus false is returned.
// Otherwise true is returned.
func notGood(needle int, haystack []int) bool {
for _, i := range haystack {
if needle == i {
return false
}
}
return true
}
// expected denotes the expected list of known-good HTTP response codes possible from the Mailgun API.
var expected = []int{200, 202, 204}
// makeRequest shim performs a generic request, checking for a positive outcome.
// See simplehttp.MakeRequest for more details.
2016-06-28 22:48:18 +05:30
func makeRequest(r *httpRequest, kind string, p payload) (*httpResponse, error) {
r.addHeader("User-Agent", MailgunGoUserAgent)
rsp, err := r.makeRequest(kind, p)
2015-08-18 05:57:27 +05:30
if (err == nil) && notGood(rsp.Code, expected) {
2016-06-28 22:48:18 +05:30
return rsp, newError(r.URL, expected, rsp)
2015-08-18 05:57:27 +05:30
}
return rsp, err
}
// getResponseFromJSON shim performs a GET request, checking for a positive outcome.
// See simplehttp.GetResponseFromJSON for more details.
2016-06-28 22:48:18 +05:30
func getResponseFromJSON(r *httpRequest, v interface{}) error {
r.addHeader("User-Agent", MailgunGoUserAgent)
response, err := r.makeGetRequest()
2015-08-18 05:57:27 +05:30
if err != nil {
return err
}
if notGood(response.Code, expected) {
2016-06-28 22:48:18 +05:30
return newError(r.URL, expected, response)
2015-08-18 05:57:27 +05:30
}
2016-06-28 22:48:18 +05:30
return response.parseFromJSON(v)
2015-08-18 05:57:27 +05:30
}
// postResponseFromJSON shim performs a POST request, checking for a positive outcome.
// See simplehttp.PostResponseFromJSON for more details.
2016-06-28 22:48:18 +05:30
func postResponseFromJSON(r *httpRequest, p payload, v interface{}) error {
r.addHeader("User-Agent", MailgunGoUserAgent)
response, err := r.makePostRequest(p)
2015-08-18 05:57:27 +05:30
if err != nil {
return err
}
if notGood(response.Code, expected) {
2016-06-28 22:48:18 +05:30
return newError(r.URL, expected, response)
2015-08-18 05:57:27 +05:30
}
2016-06-28 22:48:18 +05:30
return response.parseFromJSON(v)
2015-08-18 05:57:27 +05:30
}
// putResponseFromJSON shim performs a PUT request, checking for a positive outcome.
// See simplehttp.PutResponseFromJSON for more details.
2016-06-28 22:48:18 +05:30
func putResponseFromJSON(r *httpRequest, p payload, v interface{}) error {
r.addHeader("User-Agent", MailgunGoUserAgent)
response, err := r.makePutRequest(p)
2015-08-18 05:57:27 +05:30
if err != nil {
return err
}
if notGood(response.Code, expected) {
2016-06-28 22:48:18 +05:30
return newError(r.URL, expected, response)
2015-08-18 05:57:27 +05:30
}
2016-06-28 22:48:18 +05:30
return response.parseFromJSON(v)
2015-08-18 05:57:27 +05:30
}
// makeGetRequest shim performs a GET request, checking for a positive outcome.
// See simplehttp.MakeGetRequest for more details.
2016-06-28 22:48:18 +05:30
func makeGetRequest(r *httpRequest) (*httpResponse, error) {
r.addHeader("User-Agent", MailgunGoUserAgent)
rsp, err := r.makeGetRequest()
2015-08-18 05:57:27 +05:30
if (err == nil) && notGood(rsp.Code, expected) {
2016-06-28 22:48:18 +05:30
return rsp, newError(r.URL, expected, rsp)
2015-08-18 05:57:27 +05:30
}
return rsp, err
}
// makePostRequest shim performs a POST request, checking for a positive outcome.
// See simplehttp.MakePostRequest for more details.
2016-06-28 22:48:18 +05:30
func makePostRequest(r *httpRequest, p payload) (*httpResponse, error) {
r.addHeader("User-Agent", MailgunGoUserAgent)
rsp, err := r.makePostRequest(p)
2015-08-18 05:57:27 +05:30
if (err == nil) && notGood(rsp.Code, expected) {
2016-06-28 22:48:18 +05:30
return rsp, newError(r.URL, expected, rsp)
2015-08-18 05:57:27 +05:30
}
return rsp, err
}
// makePutRequest shim performs a PUT request, checking for a positive outcome.
// See simplehttp.MakePutRequest for more details.
2016-06-28 22:48:18 +05:30
func makePutRequest(r *httpRequest, p payload) (*httpResponse, error) {
r.addHeader("User-Agent", MailgunGoUserAgent)
rsp, err := r.makePutRequest(p)
2015-08-18 05:57:27 +05:30
if (err == nil) && notGood(rsp.Code, expected) {
2016-06-28 22:48:18 +05:30
return rsp, newError(r.URL, expected, rsp)
2015-08-18 05:57:27 +05:30
}
return rsp, err
}
// makeDeleteRequest shim performs a DELETE request, checking for a positive outcome.
// See simplehttp.MakeDeleteRequest for more details.
2016-06-28 22:48:18 +05:30
func makeDeleteRequest(r *httpRequest) (*httpResponse, error) {
r.addHeader("User-Agent", MailgunGoUserAgent)
rsp, err := r.makeDeleteRequest()
2015-08-18 05:57:27 +05:30
if (err == nil) && notGood(rsp.Code, expected) {
2016-06-28 22:48:18 +05:30
return rsp, newError(r.URL, expected, rsp)
2015-08-18 05:57:27 +05:30
}
return rsp, err
}