Httparty - appending to url and adding headers to get request via Ruby class - ruby

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

Related

Correct way to get response from Slack API

I want to move this code from the console:
curl --data "token=TOKEN_NO&email=USER#EXAMPLE.COM" https://slack.com/api/users.lookupByEmail
to the class method and get response from Slack (user details) exactly like from the console. I was trying to do something like this, but I don't see any results:
require 'net/http'
module Slack
class GetUserID
SLACK_API_ENDPOINT = 'https://slack.com/api/users.lookupByEmail'
def call
escaped_address = URI.decode_www_form_component(SLACK_API_ENDPOINT)
uri = URI.parse(escaped_address)
puts Net::HTTP.post(uri, params)
end
private
def params
{
token: 'TOKEN_NO',
email: 'USER#EXAMPLE.COM'
}
end
end
end
Right now I have an error:
send_request_with_body': undefined methodbytesize' for #Hash:0x00007f9d85093100> (NoMethodError)
Where am I wrong? Should I use HTTParty instead?
The second argument passed to Net::HTTP.post should be a string, read more about the method signature here.
You will need to convert the params hash into a query string format that looks like:
token=TOKEN_NO&email=USER#EXAMPLE.COM

case sensitive headers in get request using httparty in rails

I'm currently getting an error when I make a GET request using httparty. The call works when I use curl. The error is as follows:
\"Authdate\":\"1531403501\"}" }, { "error_code":
"external_auth_error", "error_message": "Date header is missing or
timestamp out of bounds" } ] }
When I make the request via curl this is the header I use.
curl -X GET -H "AuthDate: 1531403501"
However, as you can see, the request changes from AuthDate to Authdate causing the error. Here is how I'm making the call:
require 'openssl'
require 'base64'
module SeamlessGov
class Form
include HTTParty
attr_accessor :form_id
base_uri "https://nycopp.seamlessdocs.com/api"
def initialize(id)
#api_key = ENV['SEAMLESS_GOV_API_KEY']
#signature = generate_signature
#form_id = id
#timestamp = Time.now.to_i
end
def relative_uri
"/form/#{#form_id}/elements"
end
def create_form
self.class.get(relative_uri, headers: generate_headers)
end
private
def generate_signature
OpenSSL::HMAC.hexdigest('sha256', ENV['SEAMLESS_GOV_SECRET'], "GET+#{relative_uri}+#{#timestamp}")
end
def generate_headers
{
"Authorization" => "HMAC-SHA256 api_key='#{#api_key}' signature='#{#signature}'",
"AuthDate" => #timestamp
}
end
end
end
any workaround this?
Headers are case-insensitive per the spec https://stackoverflow.com/a/41169947/1518336, so it seems like the server you're accessing is in the wrong.
Looking at Net::HTTPHeader, on which HTTParty is implemented
Unlike raw hash access, HTTPHeader provides access via case-insensitive keys
It looks like the class downcases the header keys for uniformity.
You'll likely need to look at a different networking library which doesn't rely on the net/http. Perhaps curb?
There is a work around this in the following article
https://github.com/jnunemaker/httparty/issues/406#issuecomment-239542015
I created the file lib/net_http.rb
require 'net/http'
class Net::HTTP::ImmutableHeaderKey
attr_reader :key
def initialize(key)
#key = key
end
def downcase
self
end
def capitalize
self
end
def split(*)
[self]
end
def hash
key.hash
end
def eql?(other)
key.eql? other.key.eql?
end
def to_s
def self.to_s
key
end
self
end
end
Then in the headers
def generate_headers
{
"Authorization" => "HMAC-SHA256 api_key='#{#api_key}' signature='#{#timestamp}'",
Net::HTTP::ImmutableHeaderKey.new('AuthDate') => "#{#timestamp}"
}
end

Ruby/Sinatra - How can I call post in lambda class?

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!

How to switch base_uri with httparty

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.

Use api key in HTTParty

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

Resources