Skip to content

Add Client.GetAs generic method #297

@dolmen

Description

@dolmen

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, target any) error {
	b, err := z.Get(ctx, "/users/me.json")
	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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions