cli / go-gh

A Go module for interacting with gh and the GitHub API from the command line.
https://pkg.go.dev/github.com/cli/go-gh/v2
MIT License
322 stars 45 forks source link

docs: Example of reading an octet stream #54

Closed alejandrovelez7 closed 1 year ago

alejandrovelez7 commented 2 years ago

Hello!

Loving the abstraction so far, great job!

Was wondering if we could add an example that reads an octet-stream similar to this in the cli repo using net/http?

I'm trying a couple of different approaches, but I'm not sure what's the best way to define the response to facilitate reading bytes?

Let me know if I can help in any way as well 😄

Edit: I'm using the rest client for reference, and looking at https://github.com/cli/go-gh/blob/c41a1271d97cf42ba0dcf94289da729cdb1554eb/internal/api/rest_client.go#L48 I'm not sure this is possible currently?

Using Get or Do I get:

invalid character '\x1f' looking for beginning of value
samcoe commented 1 year ago

@alejandrovelez7 That is a good question. In the latest release (v0.0.3) there isn't a good way to read an octet-stream with the RESTClient and you would need to use the raw HTTPClient. Having said that, in trunk, there is a new function Request on the RESTClient that will return the http response without doing any reading or parsing. Using that function it should be easy to adapt the code you linked in the cli repo to read an octet-stream.

https://github.com/cli/go-gh/blob/91ca4ef447d4db5075be14acdac2759710983b26/internal/api/rest_client.go#L51

Hopefully that answers your question. Let me know if anything is unclear!

alejandrovelez7 commented 1 year ago

@samcoe Great! Yeah that should work nicely. Not sure if I'll have time to test out soon, but it looks similar to http's NewRequest so I'm sure it will work. Feel free to close this out or I can add an example to the examples page once it lands if you'd like! Thanks again!