Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support for webhook event documentation endpoint
- Loading branch information
Showing
2 changed files
with
123 additions
and
13 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 |
---|---|---|
@@ -1,23 +1,73 @@ | ||
package gosparkpost | ||
|
||
var eventDocumentationFormat = "/api/v%d/webhooks/events/documentation" | ||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
type EventDocumentationResponse struct { | ||
Results map[string]*EventGroup `json:"results,omitempty"` | ||
} | ||
"github.com/pkg/errors" | ||
) | ||
|
||
var eventDocumentationFormat = "/api/v%d/webhooks/events/documentation" | ||
|
||
type EventGroup struct { | ||
Name string | ||
Events map[string]EventField | ||
Events map[string]EventMeta | ||
Description string `json:"description"` | ||
DisplayName string `json:"display_name"` | ||
} | ||
|
||
type EventMeta struct { | ||
Name string | ||
Fields map[string]EventField `json:"event"` | ||
Description string `json:"description"` | ||
DisplayName string `json:"display_name"` | ||
} | ||
|
||
type EventField struct { | ||
Description string `json:"description"` | ||
SampleValue string `json:"sampleValue"` | ||
Description string `json:"description"` | ||
SampleValue interface{} `json:"sampleValue"` | ||
} | ||
|
||
func (c *Client) EventDocumentation() (g *EventGroup, res *Response, err error) { | ||
return nil, nil, nil | ||
func (c *Client) EventDocumentation() (g map[string]*EventGroup, res *Response, err error) { | ||
path := fmt.Sprintf(eventDocumentationFormat, c.Config.ApiVersion) | ||
res, err = c.HttpGet(c.Config.BaseUrl + path) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
if err = res.AssertJson(); err != nil { | ||
return nil, res, err | ||
} | ||
|
||
if res.HTTP.StatusCode == 200 { | ||
var body []byte | ||
var ok bool | ||
body, err = res.ReadBody() | ||
if err != nil { | ||
return nil, res, err | ||
} | ||
|
||
var results map[string]map[string]*EventGroup | ||
var groups map[string]*EventGroup | ||
if err = json.Unmarshal(body, &results); err != nil { | ||
return nil, res, err | ||
} else if groups, ok = results["results"]; ok { | ||
return groups, res, err | ||
} | ||
return nil, res, errors.New("Unexpected response format") | ||
} else { | ||
err = res.ParseResponse() | ||
if err != nil { | ||
return nil, res, err | ||
} | ||
if len(res.Errors) > 0 { | ||
err = res.PrettyError("EventDocumentation", "retrieve") | ||
if err != nil { | ||
return nil, res, err | ||
} | ||
} | ||
return nil, res, errors.Errorf("%d: %s", res.HTTP.StatusCode, string(res.Body)) | ||
} | ||
|
||
return nil, res, err | ||
} |
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