I am trying to pass a parameter to a login method and I want to switch the base uri based on that parameter.
Like so:
class Managementdb
include HTTParty
def self.login(game_name)
case game_name
when "game1"
self.base_uri = "http://game1"
when "game2"
self.base_uri = "http://game2"
when "game3"
self.base_uri = "http://game3"
end
response = self.get("/login")
if response.success?
#authToken = response["authToken"]
else
# this just raises the net/http response that was raised
raise response.response
end
end
...
Base uri does not set when I call it from a method, how do I get that to work?
In HTTParty, base_uri is a class method which sets an internal options hash. To dynamically change it from within your custom class method login you can just call it as a method (not assigning it as if it was a variable).
For example, changing your code above, this should set base_uri as you expect:
...
case game_name
when "game1"
# call it as a method
self.base_uri "http://game1"
...
Hope it helps.
I can’t comment yet, so here’s an extension to Estanislau Trepat’s answer.
To set the base_uri for all your calls, call the according class method:
self.base_uri "http://api.yourdomain.com"
If you want to have a way of sending only a few calls to a different URI and avoid state errors (forgetting to switch back to the original URI) you could use the following helper:
def self.for_uri(uri)
current_uri = self.base_uri
self.base_uri uri
yield
self.base_uri current_uri
end
With the above helper, you can make specific calls to other URIs like the following:
for_uri('https://api.anotheruri.com') do
# your httparty calls to another URI
end
I'm not certain it was implemented when this question was first asked, but if you want to set or override :base_uri on a per-request or per-instance basis, HTTParty request methods (:get, :post, etc) accept options to override class options.
So for OP's example, it could look something like this:
class Managementdb
include HTTParty
# If you wanted a default, class-level base_uri, set it here:
base_uri "http://games"
def self.login(game_name)
base_uri =
case game_name
when "game1" then "http://game1"
when "game2" then "http://game2"
when "game3" then "http://game3"
end
# To override base_uri for an individual request, pass
# it as an option:
response = get "/login", base_uri: base_uri
# ...
end
end
Calling the class method dynamically, as suggested in some of the other answers, changes the base_uri for all requests, which is probably not what you want. It's certainly not thread-safe.
Related
I'm currently working with Httparty to make a GET Seamless.giv API which returns field from a form. In the requests there are Authentication headers that need to be passed in order to access the API. But the request has to be made to a specific form. Thats where the issue lays should this be in the base URI or appended?
Here is the curl example of the request:
curl -X GET -H "AuthDate: 1531236159"\
-H "Authorization: HMAC-SHA256 api_key=XXXXXXXX nonce=12345 Signature=XXXXXXXXXXX"\
-d 'false' https://nycopp.seamlessdocs.com/api/form/:form_id/elements
and this is the approach im currently taking:
class SeamlessGov
include HTTParty
base_uri "https://nycopp.seamlessdocs.com/api"
def initialize(args={})
#api_key = args[:api_key]
#nonce = args[:nonce]
#signature = generate_signature
end
def form(form_id)
#form = form_id
end
def headers(headers={})
#headers = headers
end
def generate_signature
# bash command
end
end
Is the best practice to append it or put it in the base_uri for example:
base_uri "https://nycopp.seamlessdocs.com/api/form/:form_id/elements" or created a method to append to the base_uri for example:
def append_form(form)
"/form/#{form}/elements"
end
What would the best approach be? So that when I call
#form = SeamlessGov.new(params, headers) works.
If I understand what you're asking correctly, you would write a method like:
def create_form
get("/form/#{form_id}/elements", headers)
end
Which you can then call like:
#form = SeamlessGov.new(params, headers).create_form
I'm writing Ruby Gem where I have Connection module for Faraday configuration
module Example
module Connection
private
def connection
Faraday.new(url: 'http://localhost:3000/api') do |conn|
conn.request :url_encoded # form-encode POST params
conn.response :logger # log requests to STDOUT
conn.adapter Faraday.default_adapter # make requests with Net::HTTP
conn.use Faraday::Response::ParseJson
conn.use FaradayMiddleware::RaiseHttpException
end
end
end
end
Second module which makes API requests looks like this:
module Example
module Request
include Connection
def get(uri)
connection.get(uri).body
end
def post(url, attributes)
response = connection.post(url) do |request|
request.body = attributes.to_json
end
end
def self.extended(base)
base.include(InstanceMethods)
end
module InstanceMethods
include Connection
def put(url, attributes)
response = connection.put(url) do |request|
request.body = attributes.to_json
end
end
end
end
end
Class Cusomer where I use Request looks like this:
module Example
class Customer
extend Request
attr_accessor :id, :name, :age
def initialize(attrs)
attrs.each do |key, value|
instance_variable_set("##{key}", value)
end
end
def self.all
customers = get('v1/customer')
customers.map { |cust| new cust }
end
def save
params = {
id: self.id,
age: self.age
name: self.name,
}
put("v1/customers/#{self.id}", params)
end
end
end
So here you see in Customer#all class method I'm calling Request#get method which is available because I extended Request in Customer. then I'm using self.extended method in Request module to be make Request#put available in Customer class, so I have question is this good approach to use mixins like this, or do you have any suggestion?
Mixins are a strange beast. Best practices vary depending on who you talk to. As far as reuse goes, you've achieved that here with mixins, and you have a nice separation of concerns.
However, mixins are a form of inheritance (you can take a peek at #ancestors). I would challenge you saying that you shouldn't use inheritance here because a Customer doesn't have an "is-a" relationship with Connection. I would recommend you use composition instead (e.g. pass in Connection/Request) as it makes more sense to me in this case and has stronger encapsulation.
One guideline for writing mixins is to make everything end in "-able", so you would have Enumerable, Sortable, Runnable, Callable, etc. In this sense, mixins are generic extensions that provide some sort of helpers that are depending on a very specific interface (e.g. Enumerable depends on the class to implement #each).
You could also use mixins for cross-cutting concerns. For example, we've used mixins in the past in our background jobs so that we could add logging for example without having to touch the source code of the class. In this case, if a new job wants logging, then they just mixin the concern which is coupled to the framework and will inject itself properly.
My general rule of thumb is don't use them if you don't have to. They make understanding the code a lot more complicated in most cases
EDIT: Adding an example of composition. In order to maintain the interface you have above you'd need to have some sort of global connection state, so it may not make sense. Here's an alternative that uses composition
class CustomerConnection
# CustomerConnection is composed of a Connection and retains isolation
# of responsibilities. It also uses constructor injection (e.g. takes
# its dependencies in the constructor) which means easy testing.
def initialize(connection)
#connection = connection
end
def all_customers
#connection.get('v1/customers').map { |res| Customer.new(res) }
end
end
connection = Connection.new
CustomerConnection.new(connection).all_customers
I'm make a little program in sinatra and I'm wanted to perfom some dynamic call of post, with diynamic uri, so I make a Connexion class like this:
class Connexion
def initialize(path)
#path = path
end
def sinatraPost
post "/#{#path}" do
# some code
end
end
end
But when I'm launch sinatraPost, I've got this error:
undefined method `post' for #<Connexion:0x000000026206b8> (NoMethodError)
How can I call the sinatra post method in my class ?
EDIT: Okay ! So, I change my strategy, I have this following code:
class Webhook < Sinatra::Base
get '/:name' do
# compare with names array
end
end
Webhook.run!
Thank's to everyone !
It looks like you're going about this the wrong way. If you want to set up your app to receive a POST request, you'll need routing logic in your controller. Sinatra controllers normally look like this:
require 'sinatra'
get '/route1' do
# do stuff
end
post '/route2' do
# do stuff
end
If you're using a modular app, you'll want to have your app inherit from Sinatra::Base. See the Sinatra docs for more.
Making a post request is different, and doesn't rely on Sinatra methods.
require 'net/http'
uri = URI("http://google.com")
headers = {}
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri, headers)
response = http.request(request)
Or something like that. Good luck!
I have what is probably a basic Q, but it appears complex in the setup. I have a module that has some classes. One class contains methods for API calls. Other classes describe a server. Dev for instance has its attributes. The server classes inherit the class that contains all the API calls. I use an instance of the server class to use one of these methods and then apply EventMachine methods to it. Here's a subset of a server class:
class PulseDev < ApiMethods
def base_uri
"http://myserver.com/api"
end
end
And an action in the methods class:
Class ApiMethods
def get_json_api_post_response(url, post_obj={})
http = EM::Synchrony.sync EventMachine::HttpRequest.new(self.base_uri+"#{url}").post(:body => post_obj)
process_response self.class.post(url, :body => post_obj).body
end
def process_response(result)
response = ActiveSupport::JSON.decode(result)
if response["code"].to_i == 200
ToolResult.new(true, response["result"], 200)
else
ToolResult.new(false, response["result"], response["code"])
end
end
end
Class ToolResult < Struct.new(:success, :result, :code)
end
And my invocation of it in the controller:
http = ApiMethods::Dev.new.get_json_api_post_response('/handshake', #j)
OK, my error is undefined method `post' for ApiMethods::PulseDev:Class
and it points to the post in my get_json_api_post_response method.
My question is: I get that it's within the context of the ApiMethods::Dev which is why self.base_uri works but how should I handle the post inside that process_response method so that it's tied to EventMachine? Is there something about method chaining I'm not seeing? In the error output I can verify that http is showing the EventMachine object so the new method seems to be working. Thanks for your time, sam
The answer is to look more carefully at the error msg. The process_response method is the one actually calling the EventMachine method and processing its :body. So it was written with an unneeded call.
I am trying to access a service which uses the url format. www.example.com/api/API_KEY/action
The below code is a small example of what I'm trying to achieve.
require 'httparty'
class MyAPI
include HTTParty
debug_output $stdout
base_uri "example.com/api/#{#api_key}"
def initialize(api_key)
#api_key = api_key
end
def statistics
return self.class.get("/statistics")
end
end
The server request:
MyAPI.new('apikey').statistics
comes out as
GET /api//statistics
I knew it was optimistic but I put the api_key variable in the base_uri. How do I make it so that the url uses the dynamic api_key?
You are missing a reader method for #api_key.
Add the following to your class to allow the setting of #api_key after initialization.
attr_accessor :api_key
Or add to allow it to be read, but not set later.
attr_reader :api_key