inf0rmer / blanket Goto Github PK
View Code? Open in Web Editor NEWA dead simple API wrapper
License: MIT License
A dead simple API wrapper
License: MIT License
Any chance you can do an official release for 3.0.3? The last release 3.0.2 doesn't include support for payload_json.
Thanks!
referencing #15
@inf0rmer Let me start by saying this gem is awesome and has given me some really great ideas for things to build recently!
In my opinion Blanket should not force responses into an array. If I make a request to http://www.example.com/users/1
, I am expecting a single hash (which most closely resembles a JSON object) at the root of the response describing this resource. Having Blanket force this single object into an array (which most closely resembles a JSON array) seems a bit odd.
If you are following common JSON API conventions (http://jsonapi.org/format/#document-structure-top-level), it is standard to have a JSON object (which would map to a hash in our case) at the root of every JSON API response. Taking all responses (which are more often than not a JSON object) and wrapping them in an array doesn't seem like a good approach to bake into this awesome gem.
If some users of Blanket need all their responses in an array, they can of course, do so with little effort. Blanket, in my opinion, shouldn't change the response conventions chosen by services; this should be left to the user of the gem and be done with coercion after the fact to fit their use case.
Hello!
I really like this little gem, I am using it since a long time now. However, today, I need to query an endpoint and to pass a form-data parameter.
Is that supported or will it be supported in the future?
Thank you
Hi all,
Blanket looks great.
Any chance to use Blanket for APIs, which only return XML data?
The docs regarding XML seem a bit ambiguous to me. To quote the Readme:
"At the moment Blanket only accepts JSON responses"
and
"Some APIs require you to append an extension to your requests, such as .json or .xml. Blanket supports this use case, letting you define an extension for all your requests or override it for a single one"
Thanks and best regards
Christian
Instead of creating a PR here, I thought I'd implement something similar to Blanket but using Sawyer instead of HTTParty. Sawyer handles a lot of the stuff you do here a lot better and with greater configurability.
I created Crib which mimics the syntax of Blanket but, as mentioned previously, uses Sawyer. So instead of having to handle headers by hand, you can do advanced things like this:
dribbble = Crib.api('https://api.dribbble.com/v1') do |http|
http.headers[:user_agent] = 'crib'
http.authorization 'Bearer', '1aea05cfdbb92294be2fcf63ee11b412fd88c65051bd3144302c30ae8ba18896'
http.response :logger # take note of this, it logs all requests in the following examples
end
dribbble.users('simplebits')._get.id
# I, [2015-01-02T14:59:20.183319 #6990] INFO -- : get https://api.dribbble.com/v1/users/simplebits
# D, [2015-01-02T14:59:20.183436 #6990] DEBUG -- request: User-Agent: "crib"
# Authorization: "Bearer 1aea05cfdbb92294be2fcf63ee11b412fd88c65051bd3144302c30ae8ba18896"
# I, [2015-01-02T14:59:20.183742 #6990] INFO -- Status: 200
# D, [2015-01-02T14:59:20.183890 #6990] DEBUG -- response: server: "nginx"
# date: "Fri, 02 Jan 2015 14:59:20 GMT"
# content-type: "application/json; charset=utf-8"
# (...)
#
# => 1
The above example from Crib, defines an API, sets the User-Agent
header for each response, defines token authentication, and also uses middleware that logs requests and their responses. All this in five lines of code.
Now, this isn't all. Sawyer is hypermedia-enabled which means that resources returned by the underscore-prefixed HTTP methods (this is #_get
, etc. so the namespace isn't polluted and we can do /get
paths) contain not only data but hypermedia link relations:
me = dribbble.users('rafalchmiel')._get
me.rels
# => {:html_url=>"https://dribbble.com/RafalChmiel",
# :avatar_url=>
# "https://d13yacurqjgara.cloudfront.net/users/97203/avatars/normal/profile-icon-margin-transparent.png?1385898916",
# :buckets_url=>"https://api.dribbble.com/v1/users/97203/buckets",
# :followers_url=>"https://api.dribbble.com/v1/users/97203/followers",
# :following_url=>"https://api.dribbble.com/v1/users/97203/following",
# :likes_url=>"https://api.dribbble.com/v1/users/97203/likes",
# :projects_url=>"https://api.dribbble.com/v1/users/97203/projects",
# :shots_url=>"https://api.dribbble.com/v1/users/97203/shots",
# :teams_url=>"https://api.dribbble.com/v1/users/97203/teams"}
# Get the followers 'rel', returned from the API as 'followers_url'
me.rels[:followers].href
# => "https://api.dribbble.com/v1/users/97203/followers"
followers = me.rels[:followers].get.data
followers.first.follower.name
# => "Kevin Halley"
So my question is, would you consider using Sawyer in Blanket? A massive GitHub API wrapper project called Octokit uses Sawyer so there's no need to worry about stability. There's also some other additions in Crib that I'd love to see in Blanket, so tell me what you think and also have a look at the README.
Hi, I think the Content-Type header is not being used because I am getting malformed syntax from an API with Blanket, and doing with Net::HTTP it's working. Can you verify that?
Thanks!
Provide data as RecursiveOpenStruct preferably. (or fall back to hash)
I am coming up with a few issues going through the README:
require 'blanket'
=> true
user = github.users('inf0rmer').get
#<Blanket::Response:0x007fe46ea64d70 @payload=[#<RecursiveOpenStruct login="inf0rmer", bla..bla.. >]>
user.login
NoMethodError: undefined method `login' for #<Blanket::Response:0x007fe46ea64d70>
user.payload.first.login
=> "inf0rmer"
I.e. change https://github.com/inf0rmer/blanket/blob/master/lib/blanket/wrapper.rb#L80 from
if response.code <= 400
to if response.code < 400
.
I think this is a typo since Blanket::STATUSES
already includes 400 => 'Bad Request'
(https://github.com/inf0rmer/blanket/blob/master/lib/blanket/exception.rb#L5).
I am trying to add in form-data into blanket through the put method. I seem to have hit a snag with my code.
Here is the offender:
module Knackhq
class Client
attr_accessor :base_uri, :x_knack_application_id, :x_knack_rest_api_key
def initialize(base_uri, x_knack_application_id, x_knack_rest_api_key)
@base_uri = base_uri
@x_knack_application_id = x_knack_application_id
@x_knack_rest_api_key = x_knack_rest_api_key
end
def update_record(object, knackhq_id, json)
hash_request = request
.objects(object)
.records(knackhq_id)
.put(:body => json)
.to_h
!hash_request.empty?
end
private
def request
headers = { 'x-knack-application-id' => @x_knack_application_id.dup,
'Content-Type' => 'application/json',
'x-knack-rest-api-key' => @x_knack_rest_api_key.dup }
Blanket.wrap(@base_uri.dup,
:headers => headers)
end
end
Which will always return the object, but not the changed object. I drilled down and, while I can make Postman/curl update the object, I can't seem to make Blanket update the object. I think it has to do with using multipart/form-data.
Thanks!
Some API use header to return the current page offset, for example Shopify Rest API
Blanket doesn't provide access to header, it'd be easy to add the headers
on the Blanket::Response object here without breaking change
https://github.com/inf0rmer/blanket/blob/master/lib/blanket/wrapper.rb#L97
Would you support such improvement ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.