Skip to main content

A very simple module for HTTP/1.1 requests.

Project description

yrequests

A very simple module for HTTP/1.1 requests. It is based on python-requests module.

What and Why

yrequest is just wrapper for requests functions, but it handles connection and HTTP errors silently.

How to use

from yrequests import YRequests
req = YRequests()  # you can pass "headers" (dict) or a "timeout" (secs) too
result = req.get('http://url.com/a/b/c', params={'q': 'apple'})
if result['ok']:
    print(result['text'])  # If you except a JSON uses result['json']
    # ...and other stuffs when everything is fine
else:
    # Connections, HTTP and general errors
    print(result['error'])  # or result['status_code']
    # and other stuffs when an error occurs

You can also use post, put, delete, head and options. These (HTTP) methods receive the same parameters of requests module. If you need instructions of how requests module works take a look at: http://docs.python-requests.org/en/master/user/quickstart/

Installing

To install yrequests just use pip.

pip install yrequests

YRequests class

YRequests(timeout=60, headers=None)

  • timeout: Default timeout (seconds) for all requests.
  • headers: Default headers (dict) for all requests.

YRequests.get(url, *args, **kwargs) YRequests.post(url, *args, **kwargs) YRequests.delete(url, *args, **kwargs) YRequests.put(url, *args, **kwargs) YRequests.head(url, *args, **kwargs) YRequests.options(url, *args, **kwargs) Make a request using the respective HTTP method. The args and kwargs are passed to the respective requests.<method>.

  • url: The URL.
  • args, kwargs: passed to requests module (as requests.get(url, *args, **kwargs))
  • Returns a dict with the result. See Result above for more details.

To know how these methods work take a look at requests documentation.

Result

The methods get, post, put, delete, head and options returns a dict object:

result = {
    'ok': <bool>,
    'error': <str|None>,
    'error_type': <str|None>,
    'response': <requests.response|None>,
    'headers': <dict>,
    'status_code': <int|None>,
    'content_type': <str|None>,
    'text': <str|None>,
    'json': <str|None>,
}

Result keys:

  • ok: True if everything is fine. Always check this value.
  • error: Textual error (when ok is False).
  • error_type: A string with the error type:
    • general: General error.
    • connection: DNS problem, connection refused, etc. The only exception is timed out that has its own code (above).
    • timeout: Connection timed out.
    • http: HTTP errors like 404, 403, 500, etc.
    • json: "Content-Type" header is indicated as JSON but the content is not a valid JSON.
  • response: A response object (same of requests module). You can use as fallback to check informations that are not handled by this class.
  • headers: Dictionary with the response headers (same of requests.response.headers module).
  • status_code: Integer of HTTP status code (200, 404, 500, etc).
  • content_type: The Content-Type header value.
  • text: The content of response (if any). It's always unicode.
  • json: A dictionary of the content if the "Content-Type" header is indicated as JSON.

Other example

This example makes a request on Facebook Graph API.

from yrequests import YRequests

def get_facebook_me(req):
    params = {'fields': 'id,name', 'access_token': 'XXXxxx...'}
    # Note that the "get" method uses "user-agent" and "extra-header" headers.
    result = req.get('https://graph.facebook.com/me', params=params, headers={'extra-header': 'other-header-value'})
    if not result['ok']:
        print(result['error'])
        return {}
    data = result['json']
    print('My name is %(name)s, my Facebook ID is %(id)s' % data)
    return data

# YRequests only accepts two optional parameters: headers and timeout
req = YRequests(headers={'user-agent': 'BugBot/1.0'}, timeout=30)
user_dict = get_facebook_me(req)

Contributing

If you found a bug, typo or bad coding you can contribute to this project! Send a pull request or contact yoshiodeveloper@gmail.com.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yrequests-0.1.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

yrequests-0.1.0-py2.py3-none-any.whl (6.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file yrequests-0.1.0.tar.gz.

File metadata

  • Download URL: yrequests-0.1.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for yrequests-0.1.0.tar.gz
Algorithm Hash digest
SHA256 76c15e121b25fc897159f0289c11046f2873c3b68e5078294d1208a680104b0f
MD5 ef16f671839ebfda878c6b5c2686e308
BLAKE2b-256 2122d6d70254ce8462057f77f2f20291c8fb9371b4c8ed93c1a18a9b8c0477fa

See more details on using hashes here.

File details

Details for the file yrequests-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for yrequests-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7085326ad30edbb44c9039aed86ea5dab42967c5c20d7d961382cdee7864f6e6
MD5 8e32cef7cc2e67d1b03a1b6d629db01e
BLAKE2b-256 1aa83bd78d7d2d9306a245deab91cb68fe8a02761e631b06a960f31aa557d2db

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page