Package: yrequests
yrequestsA very simple module for HTTP/1.1 requests. It is based on python-requests module.What and Whyyrequest is just wrapper forrequestsfunctions, but it handles connection and HTTP errors silently.How to usefromyrequestsimportYRequestsreq=YRequests()# you can pass "headers" (dict) or a "timeout" (secs) tooresult=req.get('http://url.com/a/b/c',params={'q':'apple'})ifresult['ok']:print(result['text'])# If you except a JSON uses result['json']# ...and other stuffs when everything is fineelse:# Connections, HTTP and general errorsprint(result['error'])# or result['status_code']# and other stuffs when an error occursYou can also usepost,put,delete,headandoptions. These (HTTP) methods receive the same parameters ofrequestsmodule. If you need instructions of how requests module works take a look at:http://docs.python-requests.org/en/master/user/quickstart/InstallingTo install yrequests just use pip.pip install yrequestsYRequests classYRequests(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. Theargsandkwargsare passed to the respectiverequests.<method>.url: The URL.args, kwargs: passed torequestsmodule (asrequests.get(url, *args, **kwargs))Returns adictwith the result. See Result above for more details.To know how these methods work take a look atrequests documentation.ResultThe methodsget,post,put,delete,headandoptionsreturns adictobject: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 (whenokis 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 ofrequestsmodule). You can use as fallback to check informations that are not handled by this class.headers: Dictionary with the response headers (same ofrequests.response.headersmodule).status_code: Integer of HTTP status code (200, 404, 500, etc).content_type: TheContent-Typeheader 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 exampleThis example makes a request on Facebook Graph API.fromyrequestsimportYRequestsdefget_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'})ifnotresult['ok']:print(result['error'])return{}data=result['json']print('My name is%(name)s, my Facebook ID is%(id)s'%data)returndata# YRequests only accepts two optional parameters: headers and timeoutreq=YRequests(headers={'user-agent':'BugBot/1.0'},timeout=30)user_dict=get_facebook_me(req)ContributingIf you found a bug, typo or bad coding you can contribute to this project!
Send a pull request or contactyoshiodeveloper@gmail.com.