GithubHelp home page GithubHelp logo

Comments (10)

marcosnils avatar marcosnils commented on July 22, 2024

@rexposadasi it's not a bad idea. Can you provide a snippet about how you'd use goreq and third party libraries so I can get an idea how to implement the API?

from goreq.

rexposadas avatar rexposadas commented on July 22, 2024

I'm writing a library which accepts an http.Request, make the call, and does some metrics on it. See this function signature. That function accepts a standard http.Request because, while using something like goreq is great, i don't want to force users to do that in order to use my library. I can see cases where some library creators feels the same way. So I was thinking of something like this.

req := goreq.Request{
Method: "POST"
Uri: "http://localhost:8000/myendpoint"
Body: body,
}

// req.Standard is simply a reference to a standard http.Request
simulate.MakeRequest(req.Standard)

from goreq.

marcosnils avatar marcosnils commented on July 22, 2024

@rexposadas I see where you're going and I have some questions though which may help to clarify the use case.

If your library does all the request handling how do you plan to return the request handler to goreq for all the remaining processing (ie: response, headers, redirects, etc)??.

To give an example:

req := goreq.Request{
Method: "POST"
Uri: "http://localhost:8000/myendpoint"
Body: body,
}

// req.Standard is simply a reference to a standard http.Request
simulate.MakeRequest(req.Standard)

//If we do this, then the request will be triggered two times against the targer URL. Once by your library and then by goreq
res, err := req.Do();

I believe a pluggable/middleware solution is more efficient in this cases where you need to sit between the request and the response to do some processing rather than just delegating the request handling to the library. We've thought to add middleware support to goreq so we can encourage some other projects (like yours) to sit between requests (currently Go default http client doesn't provide this functionality) but we haven't found the necessity or the time to do it so far.

I'm not saying we couldn't or shouldn't do it, I'm just trying to find the best use case for our users whenever they need to use both libraries.

from goreq.

rexposadas avatar rexposadas commented on July 22, 2024

I see what you mean. I guess my usecase is not common. What I'm trying to do is measure the time when a request gets called and when it returns. I didn't want to delegate that work in the middleware since I'm trying to get a very accurate reading. In this piece of code I am trying to guarantee that nothing happens between the time I start my timer, run a request, receive a response, then stop my timer. Using middleware to do that removes that guarantee.

But for all other things, I do agree that using a middle solution is the way to go.

Feel free to close this issue if you see fit. I do not think a change on your side would be beneficial to your target audience. And I can find a workaround.

from goreq.

rexposadas avatar rexposadas commented on July 22, 2024

Here is another situation of what I was thinking about..

http://golang.org/pkg/net/http/httputil/#DumpResponse

That standard package method accepts an http.Reponse, but I'm having difficulty using that library with goreq since goreq.Response cannot be passed to that method.

There are, at least, a couple of ways to tackle this situation:

  1. add a method in goreq.Response which returns an http.Response
  2. have a similar method to DumpResponse added to the goreq.Response object

from goreq.

marcosnils avatar marcosnils commented on July 22, 2024

@rexposadas you're correct. plz check out #85 and let me know your opinion so we can merge.
@xetorthio plz review as well.

from goreq.

rexposadas avatar rexposadas commented on July 22, 2024

@marcosnils thanks for that. I looked at #85. I think it's a step in the right direction. I would do something similar for goreq.Request.

i.e. having the ability to make use of http://golang.org/pkg/net/http/httputil/#DumpRequest

from goreq.

marcosnils avatar marcosnils commented on July 22, 2024

@rexposadas I've updated #85 with the ability to obtain the *http.Request with Goreq. Can you please check the PR and see if it sounds good from the user perspective?

Thanks!.

from goreq.

rexposadas avatar rexposadas commented on July 22, 2024

@marcosnils looks good to me. I can get a handle on the http.Request and http.Response, which was what I was after. Thanks for doing this.

from goreq.

marcosnils avatar marcosnils commented on July 22, 2024

closed by #85

from goreq.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.