Giter VIP home page Giter VIP logo

blanket's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

blanket's Issues

Responses should not always return an array

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.

Support of form-data params?

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

Use Sawyer, instead of HTTParty

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.

v3.0.3

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!

README not accurate

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"

multipart/form-data

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!

Content-Type

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!

XML support?

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.