Skip to content

proposal: x/net/context: Context-aware HTTP helper functions. #11904

Closed

Description

@broady

Proposal: add a context-aware client that is similar to/mirrors http.Client.

req, _ := http.NewRequest(...)

ctx := context.Background()
c := ctxhttp.NewClient(http.DefaultClient)

// Add the ctx parameter to Do()
resp, err := c.Do(ctx, req)

Should this type go into x/net/context/ctxhttp? The package shouldn't be called http, since something like this would need to be used in conjunction with net/http.

Is Get(), Post(), etc needed, or do we only need Do(ctx, req)?

Activity

  1. added this to the Unreleased milestone on Jul 28, 2015
  2. ianlancetaylor commented on Jul 28, 2015

    @ianlancetaylor
    Contributor
  3. bradfitz commented on Jul 28, 2015

    @bradfitz
    Contributor

    I'd rather not see two types of *http.Client splitting the API ecosystem. In Go 1.5, *http.Request has a Cancel field that's the same as the Done channel's return type. I'd rather just see a helper package if anything that doesn't introduce new types. If it really has to, maybe it could introduce a new RoundTripper (but returning a RoundTripper and not its own *Transport type), but I'd rather see no new types.

  4. broady commented on Jul 28, 2015

    @broady
    ContributorAuthor

    @bradfitz So, I guess just a function like Do(httpClient, ctx, req) then?

    I don't think a RoundTripper works, it goes against the intent of the context package by putting a Context into a struct.

  5. bradfitz commented on Jul 28, 2015

    @bradfitz
    Contributor

    So, I guess just a function like Do(httpClient, ctx, req) then?

    Making a request just for GET is annoying, so probably also GET.

    I don't think a RoundTripper works, it goes against the intent of the context package by putting a Context into a struct.

    Be sure to also file a bug against Google's internal codebase to make sure all our various RoundTrippers support the new Cancel field.

  6. adg commented on Jul 28, 2015

    @adg
    Contributor

    If you're making a package for helpers, might as well do the full set of
    helpers we have in the standard library:

    func Get(url string) (resp *Response, err error)
    func Head(url string) (resp *Response, err error)
    func Post(url string, bodyType string, body io.Reader) (resp *Response, err
    error)
    func PostForm(url string, data url.Values) (resp *Response, err error)

    On 28 July 2015 at 15:56, Brad Fitzpatrick [email protected] wrote:

    So, I guess just a function like Do(httpClient, ctx, req) then?

    Making a request just for GET is annoying, so probably also GET.

    I don't think a RoundTripper works, it goes against the intent of the
    context package by putting a Context into a struct.

    Be sure to also file a bug against Google's internal codebase to make sure
    all our various RoundTrippers support the new Cancel field.

    —
    Reply to this email directly or view it on GitHub
    #11904 (comment).

  7. changed the title [-]proposal: x/net/context: Context-aware HTTP client.[/-] [+]proposal: x/net/context: Context-aware HTTP helper functions.[/+] on Jul 28, 2015
  8. broady commented on Jul 28, 2015

    @broady
    ContributorAuthor

    Not sure why Gerrit didn't link this issue, but the change that implements this is at http://golang.org/cl/12755.

    I added helpers for Do/Get/Head/Post/PostForm.

    The function signatures are X(http.Client, context.Context, ...) where X is Do/Get/...

  9. gopherbot commented on Jul 28, 2015

    @gopherbot
    Contributor
  10. adg commented on Jul 28, 2015

    @adg
    Contributor

    Sometimes the notifications take a little while to come through.

    On 28 July 2015 at 18:01, GopherBot [email protected] wrote:

    CL https://golang.org/cl/12755 mentions this issue.

    —
    Reply to this email directly or view it on GitHub
    #11904 (comment).

  11. locked and limited conversation to collaborators on Sep 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions