rubygarage / api_struct

API wrapper builder with response serialization
MIT License
234 stars 21 forks source link

Default query params that are used for most requests #2

Closed brentdodell closed 6 years ago

brentdodell commented 6 years ago

Flexibility. Most of ApiStruct’s competitors work only with REST actions. ApiStruct is more flexible than that: it enables you not only to use RESTful actions but also to describe requests to any URL with any set of parameters and HTTP headers.

I was reading about ApiStruct here and came across this description of it. And I had a question.

I am working on building a gem for an API in which v3 of the API is RESTful (but not fully built out yet, and still in beta), but v2 of the API is not RESTful at all. For the time being we're going with v2 until v3 is complete, but v2 requires an API key to be passed as a query param for ~each~ most requests. Is there a way to setup default params in the config that get merged in, rather than having to specify it in each Client file?

Also, the action must be specified as a query param. How best would I defined that? (i.e. /admin/api.php?api_action=list_add&api_key=heres-my-key&other=query&params=go-here).

Thanks!

kirillshevch commented 6 years ago

We don't provide settings for the case out of the box.

As a simple solution, you can put a bunch of get params to kind of value object:

class BaseParams
  def self.call
    '?api_action=list_add&api_key=heres-my-key&other=query&params=go-here'
  end
end

And merge it with ApiStruct::Client endpoint:

class Root < ApiStruct::Client
  admin_api('api.php' + BaseParams.call)
end