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

Add Client.GetAs generic method #297

Open
dolmen opened this issue Nov 29, 2023 · 1 comment
Open

Add Client.GetAs generic method #297

dolmen opened this issue Nov 29, 2023 · 1 comment

Comments

@dolmen
Copy link

dolmen commented Nov 29, 2023

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
}
@dolmen
Copy link
Author

dolmen commented Jun 10, 2024

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant