Giter Site home page Giter Site logo

slumber's Introduction

Slumber Build status Test coverage percentage Documentation

Slumber is a Python library that provides a convenient yet powerful object-oriented interface to ReSTful APIs. It acts as a wrapper around the excellent requests library and abstracts away the handling of URLs, serialization, and request processing.

Getting Help

Visit IRC channel (#slumber on irc.freenode.net) to get help, bounce ideas or generally shoot the breeze.

QuickStart

  1. Install Slumber:

    $ pip install slumber
  2. Install Optional Requirement:

    pip install pyyaml
  3. Use Slumber!

Requirements

Slumber requires the following modules.

  • Python 2.6+
  • requests
  • pyyaml (If you are using the optional YAML serialization)

slumber's People

Contributors

andymccurdy avatar collinwat avatar demux avatar dgaus avatar dstufft avatar faulkner avatar forever-young avatar jhermann avatar kdeldycke avatar koobs avatar mativs avatar matthewlmcclure avatar maxnoel avatar merwok avatar mngmntt avatar msabramo avatar philipn avatar randalldegges-okta-2 avatar samgiles avatar singingwolfboy avatar tobych avatar toffer avatar

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

slumber's Issues

Circular import problem with slumber.exceptions

slumber.init imports exceptions from slumber, most probably to allow people to do “import slumber” and get immediate access to “slumber.exceptions”, i.e. without needing the regular submodule import (“import slumber.exceptions”). This breaks if you try to do “from slumber.exceptions import HttpClientError”.

I’d recommend removing that import. Additionally, the exception classes could be imported in init to keep convenient imports.

Requests arguments

Hi Sam,

what do you think about passing requests parameters through slumber such as timeout, proxies, stream, etc. At this moment the only way is to overwrite slumber's _request method.

I will create PR if you support this decision.

Cheers,
Alex

Deduplicate README and docs/index

A number of PRs improve wording in one of the other, but don’t always catch both.

README should contain: short desc, install steps, link to doc.

docs/index: desc, install steps, etc

Why all the header clobbering?

In the get, put, post, patch methods, the only thing that is returned is the content. What about the headers?

Some APIs respond with info in the headers rather than the body, so this is all lost.

Display response body in tracebacks

It’s frustrating to debug stuff and only get the error code when you work with servers that include detailed error info in the response. If tracebacks could display the response headers and body, that would be swell.

Python 3.4 support?

ImportError: No module named 'urlparse'

I guess slumber does not support Python 3.4 yet?

http://slumber.in returning 502 Bad Gateway

evdb@custard ~ $ curl -D - http://slumber.in/
HTTP/1.1 502 Bad Gateway
Server: gunicorn/0.17.2
Content-Type: text/html; charset=UTF-8
Date: Tue, 26 Mar 2013 10:03:15 GMT
Transfer-Encoding: chunked
Connection: close

<!DOCTYPE html>
<html>
    <head>
        <title>Bad Gateway</title>
        <style>
            body {
                font-family: sans-serif;
            }
            .footer {
                color: #aaa;
                border-top: 1px solid #aaa;
                padding: 5px 0 0 0;
                margin: 20px 0 0 0;
                font-size: 70%;
            }
        </style>
    </head>
    <body>
        <h1>502 Bad Gateway</h1>
        <p>Unable to contact backend server. Please try again later.</p>
        <div class="footer">Powered by Gondor[router]</div>
    </body>
</html>

SSL Verify

Hi,

How can I pass verify=false to ignore SSL Verify check.

Thanks,

Add support for custom headers

Hi there, first thanks for the great lib! Second, I'd like to have support for adding custom headers to a request. The requests-Lib already supports that, but the slumber code only adds 2 static header values in the current version without option to alter the headers.
Thanks!

Cleanup the Meta Class

Cleanup the Meta subclass that holds settings.

Some thoughts:

Is a full class even required? Can we just use a Dict?
Should each Resource hold it's own meta settings?

slumber.Resource._process_response

I am just checking out your code and stumbled upon slumber.Resource._process_response which seems to throw away and ignore responses =! 200..299.

    def _process_response(self, resp):
        # TODO: something to expose headers and status

        if 200 <= resp.status_code <= 299:
            return self._try_to_serialize_response(resp)
        else:
            return  # @@@ We should probably do some sort of error here? (Is this even possible?)

I think there are a lot of REST APIs which supply meaningful information beyond 2xx and the API wrapper should never try to make assumptions about the API it is wrapping.

PUT should return content and not a boolean

Hi.

Both PUT and POST can be used to create something! Often, an ID is then returned. However, only POST currently returns any returned content. In the current implementation, PUT only returns a boolean value.

Thanks

Support API Key Authentication

Hey dstufft!

I noticed that you don't have support for API key authentication. I was wondering if I rolled a pull request to support it, if it would get merged? Any thoughts on this? I'm working on a project that'll be using API key auth so I figured I'd send a patch next week sometime if you'd like it.

Implement __str__ method to get the URI

Sometimes you need access to the URI for logging or using with pure requests function or whatever reason. A neat way to enable that would be to implement __str__ on the API and Resource classes and document str(api.whatever(42)) as the way to get the URI.

Mixed trailing slashes

Consider this API:
site.com/api/collection/item to get an item
site.com/api/collection/ to get the whole collection

Currently, either everything ends with a / or nothing does. Typically, if it ends with a / it means it's a collection.

debug mode to troublshoot APIs problems

Hi,
First, thank you for your work, it's grea
is there any debug mode to see the full stacktrace and the query sent (ideally as a curl request :)) to be able to debug any REST call ?

Regards,

Olivier.

Having a /.../get/.../ URI

Hello,

I didn't find any help on the documentation or in the submitted issues about this.

How can I do a request like:

api = slumber.Api()
api.master.object.get.prop.get()

so the api call the URI /master/object/get/prop/ ?

Thanks

debug mode to troublshoot APIs problems

Hi,
First, thank you for your work, it's great and helping me a lot right now.
But as everything, i'm having trouble with my own APIs so i wanted to know,
is there any debug mode to see the full stacktrace and the query sent (ideally as a curl request :)) to be able to debug any REST call ?

Regards,

Olivier.

add resource name in a string

My resource name contains hyphens (-). I can only use underscore (_) character for a name in Python. I do a hack like this:

PATH = 'http://localhost:8000/api/v1/' # (the real url should be: http://localhost:8000/api/v1/conf/)
response_post = api.conf("pdus-oid2").post(pdu.dict)
response_delete = api.conf("pdus-oid2/" + response_post["_id"]).delete()

whereas "pdus-oid2" is the name of the resource. Thus, I reckon you should add a possibility of adding a resource name in a string.

Tests

Nothing is Tested, this needs fixed ASAP.

Documentation

The documentation currently isn't the greatest in particular a better job could be done explaining behind the scenes what is going on to make it easier to understand.

  • I'm not the greatest at writing, maybe I can find someone to help with this.

Support for "weird" urls

I'm trying to use slumber with the openbravo erp json api, which has some kind of inline query system within the query string.

The url is something like:
https://hostname/org.openbravo.service.json.jsonrest/PricingProductPrice/?_where=product.id='FBBE591D76204756A4E349FEF6E41D6D'

I've been able to call it using requests with the following parameters:
requests.get('https://hostname/org.openbravo.service.json.jsonrest/PricingProductPrice/?_where=product.id=\'FBBE591D76204756A4E349FEF6E41D6D\'')

Slumber instead seems to be escaping the quotes, so the url that I've tried does not work.
api.PricingProductPrice.get(_where='Product.id='FBBE591D76204756A4E349FEF6E41D6D'')

The request sent by slumber to the webserver is this:
org.openbravo.service.json.jsonrest/PricingProductPrice/?_where=Product.id%3D%27FBBE591D76204756A4E349FEF6E41D6D%27

while the correct one is this:
org.openbravo.service.json.jsonrest/PricingProductPrice/?_where=product.id='FBBE591D76204756A4E349FEF6E41D6D'

Do you think that such particular use cases could be supported by slumber? More examples of such queries are available on the openbravo website:

Thanks a lot for this powerful tool.

Allow making requests at the root of an API

Unless I've missed something, it doesn't seem to be possible to make a request to the root URL of an API. Maybe instead of returning an API object, you should just get a fully functional resource from the get go?

If session and auth are provided to API constructor, auth is ignored

If you create an API object with an auth param, slumber creates a Session object and saves the auth on it. If you create with a session object, it is used. If you pass session and auth, the auth argument will be ignored.

Work-around: pass auth to Session, not API.

Fix: move session.auth = auth out of the if block.

Slumber tries to decode responses before passing them to the serializer, making it unusable for binary formats

I've been trying to use Slumber with an API that exposes both JSON and MessagePack; it works fine with the former but doesn't work at all when I try to write a custom serialiser for the latter.

It looks like Slumber is trying to decode the response as text in slumber.Resource._try_to_serialize_response; because MessagePack is a binary format, requests.utils.guess_json_utf is returning None, and resp.content.decode(None) works exactly as well as one would expect, so the serialiser doesn't even get called and the consumer code gets returned a raw bytestring.

I'm using Slumber 0.7.1 on Python 3.4.3. The code for my MessagePack serialiser is as follows:

import msgpack
from slumber.serialize import BaseSerializer


class MessagePackSerializer(BaseSerializer):
    key = "msgpack"
    content_types = ("application/msgpack",)

    def loads(self, data):
        return msgpack.loads(data)

    def dumps(self, data):
        return msgpack.dumps(data)

Allow passing custom headers on REST APIs

Currently _request method does not allow passing custom headers from any of HTTP method.

This is useful for REST requests in a lot of cases.

I have submitted a pull request for this.

Second way to get help?

The README says:

There are two primary ways of getting help. I have an IRC channel (#slumber on irc.freenode.net) to get help, want to bounce idea or generally shoot the breeze.

What's the second way to get help? ;-)

Easier way to pass query string parameters

I’m writing a service with GET filters like from=user_id and payload.received=true. When using slumber on the client side, because of Python rules for identifiers I have to write things.get(**{'from': user_id, 'payload.received': 'true'}). It would be nice to have a parameter (like query) that accepts a dict (and is merged with the varkwargs for compat). What do you think?

Python 3 compatibility

I've been trying to run slumber in Python 3.4 after fixing the references to "urlparse" and "iteritems" but now there's one of those bytes-vs-str things happening. Is there a recommended way to run slumber in Python 3?

Add convenience method to check for existence of a resource

We’re replacing an internal lib with slumber at work and so far we like it; thanks!

In addition to the HTTP methods, our lib had an exists function, which does a GET and returns True if the response is 2xx or 3xx, False for 404 (should also handle 410), and raises other HTTP errors.

Are you open to adding such a method to slumber?

JSON serializer breaks with charset in content-type header

The JSON serializer (I guess any serializer?) should handle content-type headers with charsets. Currently, adding a charset to the header breaks the serializer lookup. Example:

>>> from slumber.serialize import Serializer
>>> s = Serializer()
>>> s.get_serializer(content_type='application/json')
<slumber.serialize.JsonSerializer object at 0x1003a3290>
>>> s.get_serializer(content_type='application/json; charset=utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/kumar/.virtualenvs/webpay/lib/python2.6/site-packages/slumber/serialize.py", line 93, in get_serializer
    raise exceptions.SerializerNotAvailable("%s is not an available serializer" % name)
slumber.exceptions.SerializerNotAvailable: None is not an available serializer

JSON decoding should create Decimals, not floats

By default, json.load[s] will convert JSON floating-point numbers to Python floats, which is fine if you just display or print them, but causes the well-known accuracy issues if you do arithmetic or comparisons. Luckily, load and loads have a parse_float parameter that takes a callable like decimal.Decimal and uses that to convert the JSON bytes to an object: http://docs.python.org/2/library/json#json.load

I propose to always convert to Decimal. If you don’t want that, I propose a setting on the API class to control this. I’ll wait for your feedback and work up a PR.

URL parameter ?pass=secret

Request Tracker only support authentication like:

https://rt.mydomain.com/REST/1.0/?user=joe&pass=123

Unfortunately pass is a reserved keyword in Python, so I can't call:

api.ticket(42).get(user='joe', pass='123')

...without python throwing an error.

I don't see any way around this in the docs.

Update PyPi package

Hello,

is there any plans to update PyPi package since slumber works with Python 3 now?
If there is something blocking I'd be happy to help.

Best,
Alex.

Nested Resources

Nested Resources are not currently available, This needs fixed, we should be able to nest resources infinitely.

An example using the github api::

api.users("dstufft").repos.get()

Create a FormData serializer

Hi,

What do you think about adding the following serializer?

class FormDataSerializer(BaseSerializer):

    key = "formdata"
    content_types = ['application/x-www-form-urlencoded']

    def get_content_type(self):
        return self.content_types[0]

    def loads(self, data):
        return data

    def dumps(self, data):
        return data

I have to deal with a REST API which has one specific endpoint that doesn't expect JSON.

If you think adding such a serializer is worthwhile, I'll make a PR :-)

content type detection fails when charset is given

>>> api = slumber.API("https://api.github.com/", append_slash=False)
>>> resp = api.orgs('github')._request("GET")
>>> resp.headers.get('content-type')
'application/json; charset=utf-8'
>>> s = api.orgs('github')._store['serializer']
>>> s.get_serializer(content_type=resp.headers.get('content-type'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/slumber/serialize.py", line 93, in get_serializer
    raise exceptions.SerializerNotAvailable("%s is not an available serializer" % name)
slumber.exceptions.SerializerNotAvailable: None is not an available serializer
>>> 

if the get_serializer function split at ';' i think that would be sufficient

requests >=1.0 is not compatible with slumber

A new versions of the requests lib was released. This version is backwards incompatible, slumber however doesn't specify a version in install_requires causing the new incompatible version to be installed.

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.