go-zendesk
go-zendesk copied to clipboard
Add Client.GetAs generic method
I propose to add a GetAs method that wraps Client.Get but adds the json.Unmarshal step. That method will be convenient for users as well as allow to simplify the internal implementation of all the API wrappers.
Here is the full implementation (that I will submit as PR if this is accepted):
func (z *Client) GetAs(ctx context.Context, path string, target any) error {
b, err := z.Get(ctx, path)
if err != nil {
return err
}
return json.Unmarshal(b, target)
}
Here is a usage example:
func GetMe(ctx context.Context, client *zendesk.Client) (*zendesk.User, error) {
var body struct {
User *zendesk.User `json:"user"`
}
err := client.GetAs(ctx, "/users/me.json", &body)
if err != nil {
return nil, err
}
return body.User, nil
}
@nukosuke What do you think? I'm waiting for your feedback before submitting a PR.
If approved, in which file should I put this utility function?