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

Alternative interface #1

Open
izumin5210 opened this issue Nov 5, 2019 · 4 comments
Open

Alternative interface #1

izumin5210 opened this issue Nov 5, 2019 · 4 comments

Comments

@izumin5210
Copy link
Owner

izumin5210 commented Nov 5, 2019

Current(Everything as Option)

err := a.client.Post(ctx, "/api/contents",
	retry.When(hx.Any(hx.IsServerError(), hx.IsNetworkError()), newBackOff),
	hx.JSON(in),
	hx.WhenOK(hx.AsJSON(&out)),
	hx.WhenNotOK(hx.AsError()),
)
@izumin5210
Copy link
Owner Author

izumin5210 commented Nov 5, 2019

Builder pattern

  • pros
    • Completion-friendly about commonly used options
    • can support variadic params for using path parameters
  • cons
    • should call Do() to execute request

like gentleman, gorequest

err := a.client.Post(ctx, "/api/contents").
	JSON(in).
	When(hx.Any(hx.IsServerError(), hx.IsNetworkError()), retry.With(bo)).
	WhenOK(hx.AsJSON(&out)).
	WhenNotOK(hx.AsError()).
	Do()

like resty

err := a.client.Request().
	JSON(in).
	When(hx.Any(hx.IsServerError(), hx.IsNetworkError()), retry.With(bo)).
	WhenOK(hx.AsJSON(&out)).
	WhenNotOK(hx.AsError()).
	Post(ctx, "/api/contents")

Builder pattern + Functional option pattern

err := a.client.Post(ctx, "/api/contents").
	Do(
		hx.JSON(in),
		hx.When(hx.Any(hx.IsServerError(), hx.IsNetworkError()), retry.With(bo)),
		hx.WhenOK(hx.AsJSON(&out)),
		hx.WhenNotOK(hx.AsError()),
	)

@izumin5210
Copy link
Owner Author

izumin5210 commented Nov 5, 2019

Path parameter support

like gentleman/url, resty.Request.SetPathParams

err := a.client.Get(ctx, "/api/contents/{id}",
	hx.Param("id", 1),
	hx.WhenOK(hx.AsJSON(&out)),
	hx.WhenNotOK(hx.AsError()),
)

Builder pattern + Variadic parameter

err := a.client.Get(ctx, "api", "contents", 1).
	WhenOK(hx.AsJSON(&out)).
	WhenNotOK(hx.AsError()).
	Do()

Helper

err := a.client.Get(ctx, hx.Path("api", "contents", 1),
	hx.WhenOK(hx.AsJSON(&out)),
	hx.WhenNotOK(hx.AsError()),
)

@creasty
Copy link

creasty commented Nov 7, 2019

Case study: Conditionally changing configurations

Builder pattern: (In case of resty-like I/F)

client := a.client.Request().
	JSON(in).
	WhenOK(hx.AsJSON(&out)).
	WhenNotOK(hx.AsError())
if appEnv == "production" {
	client = client.When(hx.Any(hx.IsServerError(), hx.IsNetworkError()), retry.With(bo))
}

err := client.Post(ctx, "/api/contents")

Functional options pattern:

options := []hx.Option{
	hx.JSON(in),
	hx.WhenOK(hx.AsJSON(&out)),
	hx.WhenNotOK(hx.AsError())
}
if appEnv == "production" {
	options = append(options, retry.When(hx.Any(hx.IsServerError(), hx.IsNetworkError()), newBackOff))
}

err := a.client.Post(ctx, "/api/contents", options...)

@izumin5210
Copy link
Owner Author

Conditionally changing configurations with (*hx.Client.)With

client := a.client
if appEnv == "production" {
	client = client.With(
		retry.When(hx.Any(hx.IsServerError(), hx.IsNetworkError()), newBackOff),
	)
}

err := client.Post(ctx, "/api/contents",
	hx.JSON(in),
	hx.WhenOK(hx.AsJSON(&out)),
	hx.WhenNotOK(hx.AsError()),
)

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

2 participants