Skip to main content

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.

Release files for yrequests 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for yrequests 0.1.0
File Size Uploaded
yrequests-0.1.0.tar.gz 6.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for yrequests 0.1.0
File Interpreter ABI Platform
yrequests-0.1.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 12.4 kB

Release files / yrequests-0.1.0.tar.gz

Download URL yrequests-0.1.0.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
76c15e121b25fc897159f0289c11046f2873c3b68e5078294d1208a680104b0f
BLAKE2b-256 checksum
How to use checksums
2122d6d70254ce8462057f77f2f20291c8fb9371b4c8ed93c1a18a9b8c0477fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / yrequests-0.1.0-py2.py3-none-any.whl

Download URL yrequests-0.1.0-py2.py3-none-any.whl
Size 6.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
7085326ad30edbb44c9039aed86ea5dab42967c5c20d7d961382cdee7864f6e6
BLAKE2b-256 checksum
How to use checksums
1aa83bd78d7d2d9306a245deab91cb68fe8a02761e631b06a960f31aa557d2db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page