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

RESTClient not accepting query string #20

Closed
gennaro-tedesco opened this issue Jan 27, 2022 · 4 comments
Closed

RESTClient not accepting query string #20

gennaro-tedesco opened this issue Jan 27, 2022 · 4 comments

Comments

@gennaro-tedesco
Copy link

gennaro-tedesco commented Jan 27, 2022

I have a use case where I need to search for repositories containing certain query criteria (say name or language): essentially reproducing the standard repository search. Using the gh CLI this can be achieved via:

gh api -X GET /search/repositories -f q='<name> in:name language: <lang>'

I am trying to reproduce the above with the RESTClient call, passing the query search somehow as header string; however, according to the docs this grammar is not accepted (namely there is no way to pass a query string to the RESTClient object). Practically speaking I am looking to do something along the lines of

opts := api.ClientOptions{
	...
	Headers:  <insert grammar for query string>  <---- this bit must be filled correctly
	Log:       os.Stdout,
}
...
...
client, err := RESTClient(&opts) <--- the query string is passed to the client
...
err = client.Get("search/repositories", &response) <--- notice the endpoint /search/repositories

Question

What is the correct way to achieve the above?

@mislav
Copy link
Contributor

mislav commented Jan 27, 2022

You can add query parameters to the "search/repositories" string by constructing a url.Values object and serializing it to string using its Encode() method.

The argument to client.Get(path) is the path component of the URL with optional query parameters separated with ?.

@mislav mislav closed this as completed Jan 27, 2022
@gennaro-tedesco
Copy link
Author

gennaro-tedesco commented Jan 27, 2022

Thank you for your quick reply. However, when you say:

The argument to client.Get(path) is the path component of the URL with optional query parameters separated with ?.

would this mean that at the end of the day one must pass a whole string of the form

search/repositories?name=<name>?language=lang

to client.Get()? Because this looks different from what the query string for gh API /search/repositories -f q='...' is (so it is a little hard to understand what the right grammar is).

@mislav
Copy link
Contributor

mislav commented Jan 27, 2022

When you do

gh api -X GET search/repositories -f q1=foo -f q2=bar

that's equivalent to

gh api search/repositories?q1=foo&q2=bar

with the added bonus that you don't have to worry about encoding all the special characters in the query string properly, since that's handled by gh api.

Similarly, you could do:

client.Get("path?q1=foo&q2=bar")

and that will work, but I wouldn't recommend it because, again, you'd have to worry about properly encoding query values. Instead, you should do:

searchTerm := "<name>"
repoLanguage := "go"
myQuery := url.Values{}
myQuery.Add("q", fmt.Sprintf("%s in:name language:%s", searchTerm, repoLanguage))
myQuery.Add("per_page", "30")
client.Get("path?"+myQuery.Encode())

@gennaro-tedesco
Copy link
Author

Thank you very much for your exhaustive reply, it works fine!

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