Example #1
0
def make_request(url, **kwargs):
    """ Makes a POST, GET or HEAD request to the passed URL with the passed arguments,
        available arguments are:

        Argument..........Type..........Description
        method............string........Can be either get, post or head, decides
                                        which type of request is made. Default GET.
        data..............dict..........The data to send when using a POST request.
        allow_redir.......bool..........Whether or not the request follows redirects,
                                        default False.
        headers...........dict..........Additional header information for the request,
                                        by default a User-Agent and accepted language
                                        is set, to see the full headers access
                                        'use_requests.default_headers'.
        is_json...........bool..........Whether or not the response contains json data,
                                        this comes in handy when using APIs or the like,
                                        default False.
        timeout...........int...........The amount of time the request will wait until
                                        completion, default 10 seconds.

        The return value of this method is of type dict, it contains:

        Key...........Type..........Description
        success.......bool..........Whether or not the request was successfull.
        status_code...int...........The status code of the made request.
        error_msg.....string........Only exists when the request failed.
        source........string/json...The source code of a website or json data when
                                    the 'is_json' bool was set. This key is missing
                                    when the request failed.
        headers.......dict..........The returned headers of the server, this is a
                                    case in-sensitive dictionary. This key is missing
                                    when the request failed.
        """
    args = {"method": "get", "data": None, "headers": default_headers, "timeout": 5,
            "allow_redir": False, "is_json": False}
    args.update(kwargs)
    try: # Prepare the request
        if args["method"].lower() not in ("get", "post", "head"):
            args["method"] = "get"
        session = requests.Session()
        req = requests.Request(args["method"], url, args["headers"],
                               data=args["data"])
        prepared = req.prepare()
        # Make the request
        req = session.send(prepared, allow_redirects=args["allow_redir"],
                           timeout=args["timeout"], verify=False)
        # Start populating the response object
        response = Response(url)
        response.status_code = req.status_code
        if(req.status_code not in (200, 301, 302, 307)):
            raise requests.exceptions.RequestException("Status code was not OK")
    except(requests.exceptions.RequestException) as e:
        response.error_msg = e.message
    else:
        response.success = True
        response.headers = req.headers
        response.source = req.content
        # Act upon the is_json argument
        if(args["is_json"]):
            response.source = req.json()
    return response