Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correcting the responder requests to actually match the API #251

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,26 +439,31 @@ type ResponderRequestTargets struct {

// ResponderRequestOptions defines the input options for the Create Responder function.
type ResponderRequestOptions struct {
From string `json:"-"`
Message string `json:"message"`
RequesterID string `json:"requester_id"`
Targets []ResponderRequestTarget `json:"responder_request_targets"`
From string `json:"-"`
Message string `json:"message"`
RequesterID string `json:"requester_id"`
Targets []ResponderRequestTarget `json:"-"`
NestedTargets []ResponderRequestTargets `json:"responder_request_targets"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for trying to keep this backwards compatible! However, if it's never worked (and that's on me because I reviewed the original PR) would it make sense to simply fix the Targets field to be []ResponderRequestTargets, similar to how you did in ResponderRequests?

Copy link
Contributor Author

@michael-bud michael-bud Feb 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, the DX of doing the construction with []ResponderRequestTarget does feel more intuitive and I can easily see people getting confused in the future to be honest if having to build []ResponderRequestTargets themselves. I postulate that's probably why the mistake was made to implement it like that originally.

On the other hand, what you get back is different enough you're not expecting consistency and as the developer isn't the one building it, it doesn't matter so much.

}

// ResponderRequest contains the API structure for an incident responder request.
type ResponderRequest struct {
Incident Incident `json:"incident"`
Requester User `json:"requester,omitempty"`
RequestedAt string `json:"request_at,omitempty"`
Message string `json:"message,omitempty"`
Targets ResponderRequestTargets `json:"responder_request_targets"`
Incident Incident `json:"incident"`
Requester User `json:"requester,omitempty"`
RequestedAt string `json:"request_at,omitempty"`
Message string `json:"message,omitempty"`
Targets []ResponderRequestTargets `json:"responder_request_targets"`
}

// ResponderRequest will submit a request to have a responder join an incident.
func (c *Client) ResponderRequest(id string, o ResponderRequestOptions) (*ResponderRequestResponse, error) {
headers := make(map[string]string)
headers["From"] = o.From

for _, v := range o.Targets {
o.NestedTargets = append(o.NestedTargets, ResponderRequestTargets{Target: v})
}

resp, err := c.post("/incidents/"+id+"/responder_requests", o, &headers)
if err != nil {
return nil, err
Expand Down
42 changes: 22 additions & 20 deletions incident_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,25 +540,27 @@ func TestIncident_ResponderRequest(t *testing.T) {
mux.HandleFunc("/incidents/"+id+"/responder_requests", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
w.Write([]byte(`{
"responder_request": {
"requester": {
"id": "PL1JMK5",
"type": "user_reference"
},
"message": "Help",
"responder_request_targets": {
"responder_request_target": {
"id": "PJ25ZYX",
"type": "user_reference",
"incident_responders": {
"state": "pending",
"user": {
"id": "PJ25ZYX"
}
}
}
}
}
"responder_request": {
"requester": {
"id": "PL1JMK5",
"type": "user_reference"
},
"message": "Help",
"responder_request_targets": [
{
"responder_request_target": {
"id": "PJ25ZYX",
"type": "user_reference",
"incident_responders": {
"state": "pending",
"user": {
"id": "PJ25ZYX"
}
}
}
}
]
}
}`))

})
Expand Down Expand Up @@ -591,7 +593,7 @@ func TestIncident_ResponderRequest(t *testing.T) {
Incident: Incident{},
Requester: user,
Message: "Help",
Targets: ResponderRequestTargets{target},
Targets: []ResponderRequestTargets{{Target: target}},
},
}
res, err := client.ResponderRequest(id, input)
Expand Down