Python requests.request() Examples

The following are 30 code examples for showing how to use requests.request(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module requests , or try the search function .

Example 1
Project: tvdbsimple   Author: phate89   File: base.py    License: GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, id=0, user=None, key=None, timeout=None):
        """
        Initialize the base class.

        You can provide `id` that is the item id used for url creation. You can also 
        provide `user`, that is the username for login. 
        You can also provide `key`, that is the userkey needed to 
        authenticate with the user, you can find it in the 
        [account info](http://thetvdb.com/?tab=userinfo) under account identifier., 
        the language id you want to use to retrieve the info.
        """
        self._ID = id
        self.USER = user
        """Stores username if available"""
        self.USER_KEY = key
        """Stores user-key if available"""
        self.TIMEOUT = timeout
        """Stores timeout request if available""" 
Example 2
Project: tvdbsimple   Author: phate89   File: base.py    License: GNU General Public License v3.0 6 votes vote down vote up
def _request(self, method, path, params=None, payload=None,
                 forceNewToken=False, cleanJson=True):
        self._set_token_header(forceNewToken)

        url = self._get_complete_url(path)

        response = requests.request(
            method, url, params=params,
            data=json.dumps(payload) if payload else payload,
            headers=self._headers,
            timeout=self.TIMEOUT)

        if response.status_code == 200:
            response.encoding = 'utf-8'
            jsn = response.json()
            if cleanJson and 'data' in jsn:
                return jsn['data']
            return jsn
        elif not forceNewToken:
            return self._request(method=method, path=path, params=params,
                                 payload=payload, forceNewToken=True)
        try:
            raise Exception(response.json()['Error'])
        except (JSONDecodeError, KeyError):
            response.raise_for_status() 
Example 3
Project: harmony-ops   Author: harmony-one   File: get_blockchain.py    License: MIT License 6 votes vote down vote up
def get_block_number(block_num, endpoint, get_tx_info=False):
    url = endpoint
    payload = json.dumps({
        "jsonrpc": "2.0",
        "method": "hmy_getBlockByNumber",
        "params": [
            str(hex(block_num)),
            get_tx_info
        ],
        "id": 1
    })
    headers = {
        'Content-Type': 'application/json'
    }
    response = requests.request('POST', url, headers=headers, data=payload, allow_redirects=False, timeout=30)
    try:
        returned = json.loads(response.content)["result"]
        return returned
    except Exception:  # Catch all to not halt
        num_bad_get_blocks.append({
            'block-num': block_num,
            'reason': f"Failed to json load block {block_num}. Content: {response.content}"
        })
        print(f"\n[!!!] Failed to json load block {block_num}. Content: {response.content}\n") 
Example 4
Project: harmony-ops   Author: harmony-one   File: get_blockchain.py    License: MIT License 6 votes vote down vote up
def get_block_hash(block_hash, endpoint, get_tx_info=False):
    url = endpoint
    payload = json.dumps({
        "jsonrpc": "2.0",
        "method": "hmy_getBlockByNumber",
        "params": [
            block_hash if block_hash.startswith('0x') else '0x' + block_hash,
            get_tx_info
        ],
        "id": 1
    })
    headers = {
        'Content-Type': 'application/json'
    }
    response = requests.request('POST', url, headers=headers, data=payload, allow_redirects=False, timeout=30)
    try:
        returned = json.loads(response.content)["result"]
        return returned
    except Exception:  # Catch all to not halt
        hash_bad_get_blocks.append({
            'block-hash': block_hash,
            'reason': f"Failed to json load block {block_hash}. Content: {response.content}"
        })
        print(f"\n[!!!] Failed to json load block {block_hash}. Content: {response.content}\n") 
Example 5
Project: harmony-ops   Author: harmony-one   File: test.py    License: MIT License 6 votes vote down vote up
def is_after_epoch(n):
    url = args.hmy_endpoint_src
    payload = """{
        "jsonrpc": "2.0",
        "method": "hmy_latestHeader",
        "params": [  ],
        "id": 1
    }"""
    headers = {
        'Content-Type': 'application/json'
    }
    try:
        response = requests.request('POST', url, headers=headers, data=payload, allow_redirects=False, timeout=3)
        body = json.loads(response.content)
        return int(body["result"]["epoch"]) > n
    except (requests.ConnectionError, KeyError):
        return False 
Example 6
Project: Jtyoui   Author: jtyoui   File: interfaces.py    License: MIT License 6 votes vote down vote up
def interface_tests(method, url, data, record_time=True, **kwargs):
    """简单接口测试

    :param method: 请求方法,比如:post、get
    :param url: url地址
    :param data: 传入的参数
    :param record_time: 是否记录消耗时间
    :param kwargs: 参考requests.post **kwargs
    :return: 接口返回值
    """
    start = time.time()
    response = requests.request(method=method.upper(), url=url, data=data, **kwargs)
    end = time.time()
    if record_time:
        return response.text, end - start
    return response.text 
Example 7
Project: python-unsplash   Author: yakupadakli   File: client.py    License: MIT License 6 votes vote down vote up
def _request(self, url, method, params=None, data=None, **kwargs):
        url = "%s%s" % (self.api.base_url, url)
        headers = self.get_auth_header()
        headers.update(self.get_version_header())
        headers.update(kwargs.pop("headers", {}))

        try:
            response = requests.request(method, url, params=params, data=data, headers=headers, **kwargs)
        except Exception as e:
            raise UnsplashError("Connection error: %s" % e)

        try:
            if not self._is_2xx(response.status_code):
                if response.text == self.rate_limit_error:
                    raise UnsplashError(self.rate_limit_error)
                else:
                    errors = response.json().get("errors")
                    raise UnsplashError(errors[0] if errors else None)
            result = response.json()
        except ValueError as e:
            result = None
        return result 
Example 8
Project: thingsboard-gateway   Author: thingsboard   File: request_connector.py    License: Apache License 2.0 6 votes vote down vote up
def on_attributes_update(self, content):
        try:
            for attribute_request in self.__attribute_updates:
                if fullmatch(attribute_request["deviceNameFilter"], content["device"]) and fullmatch(attribute_request["attributeFilter"], list(content["data"].keys())[0]):
                    converted_data = attribute_request["converter"].convert(attribute_request, content)
                    response_queue = Queue(1)
                    request_dict = {"config": {**attribute_request,
                                               **converted_data},
                                    "request": request}
                    attribute_update_request_thread = Thread(target=self.__send_request,
                                                             args=(request_dict, response_queue, log),
                                                             daemon=True,
                                                             name="Attribute request to %s" % (converted_data["url"]))
                    attribute_update_request_thread.start()
                    attribute_update_request_thread.join()
                    if not response_queue.empty():
                        response = response_queue.get_nowait()
                        log.debug(response)
                    del response_queue
        except Exception as e:
            log.exception(e) 
Example 9
Project: thingsboard-gateway   Author: thingsboard   File: request_connector.py    License: Apache License 2.0 6 votes vote down vote up
def server_side_rpc_handler(self, content):
        try:
            for rpc_request in self.__rpc_requests:
                if fullmatch(rpc_request["deviceNameFilter"], content["device"]) and fullmatch(rpc_request["methodFilter"], content["data"]["method"]):
                    converted_data = rpc_request["converter"].convert(rpc_request, content)
                    response_queue = Queue(1)
                    request_dict = {"config": {**rpc_request,
                                               **converted_data},
                                    "request": request}
                    request_dict["config"].get("uplink_converter")
                    rpc_request_thread = Thread(target=self.__send_request,
                                                args=(request_dict, response_queue, log),
                                                daemon=True,
                                                name="RPC request to %s" % (converted_data["url"]))
                    rpc_request_thread.start()
                    rpc_request_thread.join()
                    if not response_queue.empty():
                        response = response_queue.get_nowait()
                        log.debug(response)
                        self.__gateway.send_rpc_reply(device=content["device"], req_id=content["data"]["id"], content=response[2])
                    self.__gateway.send_rpc_reply(success_sent=True)

                    del response_queue
        except Exception as e:
            log.exception(e) 
Example 10
Project: thingsboard-gateway   Author: thingsboard   File: request_connector.py    License: Apache License 2.0 6 votes vote down vote up
def __fill_requests(self):
        log.debug(self.__config["mapping"])
        for endpoint in self.__config["mapping"]:
            try:
                log.debug(endpoint)
                converter = None
                if endpoint["converter"]["type"] == "custom":
                    module = TBUtility.check_and_import(self.__connector_type, endpoint["converter"]["extension"])
                    if module is not None:
                        log.debug('Custom converter for url %s - found!', endpoint["url"])
                        converter = module(endpoint)
                    else:
                        log.error("\n\nCannot find extension module for %s url.\nPlease check your configuration.\n", endpoint["url"])
                else:
                    converter = JsonRequestUplinkConverter(endpoint)
                self.__requests_in_progress.append({"config": endpoint,
                                                    "converter": converter,
                                                    "next_time": time(),
                                                    "request": request})
            except Exception as e:
                log.exception(e) 
Example 11
Project: thingsboard-gateway   Author: thingsboard   File: rest_connector.py    License: Apache License 2.0 6 votes vote down vote up
def on_attributes_update(self, content):
        try:
            for attribute_request in self.__attribute_updates:
                if fullmatch(attribute_request["deviceNameFilter"], content["device"]) and \
                        fullmatch(attribute_request["attributeFilter"], list(content["data"].keys())[0]):
                    converted_data = attribute_request["downlink_converter"].convert(attribute_request, content)
                    response_queue = Queue(1)
                    request_dict = {"config": {**attribute_request,
                                               **converted_data},
                                    "request": regular_request}
                    with self._app.test_request_context():
                        attribute_update_request_thread = Thread(target=self.__send_request,
                                                                 args=(request_dict, response_queue, log),
                                                                 daemon=True,
                                                                 name="Attribute request to %s" % (converted_data["url"]))
                        attribute_update_request_thread.start()
                        attribute_update_request_thread.join()
                    if not response_queue.empty():
                        response = response_queue.get_nowait()
                        log.debug(response)
                    del response_queue
        except Exception as e:
            log.exception(e) 
Example 12
Project: dataiku-contrib   Author: dataiku   File: pipedriveapi.py    License: Apache License 2.0 6 votes vote down vote up
def make_api_call_all_pages(conf, action, params = {}):
    """
    A wrapper of make_api_call() to get all pages on a GET request
    """
    start = 0
    results = []
    looping = True
    params.update({'limit':conf['PAGINATION']})
    while looping:
        params.update({'start':start})
        json = make_api_call(conf, action, params)
        for r in json.get('data'):
            results.append(r)
        is_more = json.get('additional_data').get('pagination').get('more_items_in_collection')
        if is_more:
            start = json.get('additional_data').get('pagination').get('next_start')
        else:
            looping = False
    return results 
Example 13
Project: schemathesis   Author: kiwicom   File: test_models.py    License: MIT License 6 votes vote down vote up
def test_as_requests_kwargs(override, server, base_url, swagger_20, converter):
    base_url = converter(base_url)
    endpoint = Endpoint("/success", "GET", {}, swagger_20)
    kwargs = {"endpoint": endpoint, "cookies": {"TOKEN": "secret"}}
    if override:
        case = Case(**kwargs)
        data = case.as_requests_kwargs(base_url)
    else:
        case = Case(**kwargs)
        endpoint.base_url = base_url
        data = case.as_requests_kwargs()
    assert data == {
        "headers": None,
        "json": None,
        "method": "GET",
        "params": None,
        "cookies": {"TOKEN": "secret"},
        "url": f"http://127.0.0.1:{server['port']}/api/success",
    }
    response = requests.request(**data)
    assert response.status_code == 200
    assert response.json() == {"success": True} 
Example 14
Project: ibllib   Author: int-brain-lab   File: onelight.py    License: MIT License 6 votes vote down vote up
def figshare_request(
        endpoint=None, data=None, method='GET', url=None,
        binary=False, error_level=logging.ERROR):
    """Perform a REST request against the figshare API."""
    headers = {'Authorization': 'token ' + repository().get('token', '')}
    if data is not None and not binary:
        data = json.dumps(data)
    response = requests.request(
        method, url or _FIGSHARE_BASE_URL.format(endpoint=endpoint), headers=headers, data=data)
    try:
        response.raise_for_status()
        try:
            data = json.loads(response.content)
        except ValueError:
            data = response.content
    except HTTPError as error:
        logger.log(error_level, error)
        raise error
    return data 
Example 15
Project: ec2-api   Author: openstack   File: __init__.py    License: Apache License 2.0 6 votes vote down vote up
def log_request_completion(self, response, request, start):
        apireq = request.environ.get('ec2.request', None)
        if apireq:
            action = apireq.action
        else:
            action = None
        ctxt = request.environ.get('ec2api.context', None)
        delta = timeutils.utcnow() - start
        seconds = delta.seconds
        microseconds = delta.microseconds
        LOG.info(
            "%s.%ss %s %s %s %s %s [%s] %s %s",
            seconds,
            microseconds,
            request.remote_addr,
            request.method,
            "%s%s" % (request.script_name, request.path_info),
            action,
            response.status_int,
            request.user_agent,
            request.content_type,
            response.content_type,
            context=ctxt) 
Example 16
Project: ec2-api   Author: openstack   File: __init__.py    License: Apache License 2.0 6 votes vote down vote up
def _get_signature(self, req):
        """Extract the signature from the request.

        This can be a get/post variable or for version 4 also in a header
        called 'Authorization'.
        - params['Signature'] == version 0,1,2,3
        - params['X-Amz-Signature'] == version 4
        - header 'Authorization' == version 4
        """
        sig = req.params.get('Signature') or req.params.get('X-Amz-Signature')
        if sig is not None:
            return sig

        if 'Authorization' not in req.headers:
            return None

        auth_str = req.headers['Authorization']
        if not auth_str.startswith('AWS4-HMAC-SHA256'):
            return None

        return auth_str.partition("Signature=")[2].split(',')[0] 
Example 17
Project: calibre-web   Author: janeczku   File: kobo.py    License: GNU General Public License v3.0 6 votes vote down vote up
def redirect_or_proxy_request():
    if config.config_kobo_proxy:
        if request.method == "GET":
            return redirect(get_store_url_for_current_request(), 307)
        else:
            # The Kobo device turns other request types into GET requests on redirects, so we instead proxy to the Kobo store ourselves.
            store_response = make_request_to_kobo_store()

            response_headers = store_response.headers
            for header_key in CONNECTION_SPECIFIC_HEADERS:
                response_headers.pop(header_key, default=None)

            return make_response(
                store_response.content, store_response.status_code, response_headers.items()
            )
    else:
        return make_response(jsonify({})) 
Example 18
Project: calibre-web   Author: janeczku   File: kobo.py    License: GNU General Public License v3.0 6 votes vote down vote up
def get_download_url_for_book(book, book_format):
    if not current_app.wsgi_app.is_proxied:
        if ':' in request.host and not request.host.endswith(']'):
            host = "".join(request.host.split(':')[:-1])
        else:
            host = request.host
        return "{url_scheme}://{url_base}:{url_port}/download/{book_id}/{book_format}".format(
            url_scheme=request.scheme,
            url_base=host,
            url_port=config.config_port,
            book_id=book.id,
            book_format=book_format.lower()
        )
    return url_for(
        "web.download_link",
        book_id=book.id,
        book_format=book_format.lower(),
        _external=True,
    ) 
Example 19
Project: calibre-web   Author: janeczku   File: kobo.py    License: GNU General Public License v3.0 6 votes vote down vote up
def HandleCoverImageRequest(book_uuid, width, height,Quality, isGreyscale):
    book_cover = helper.get_book_cover_with_uuid(
        book_uuid, use_generic_cover_on_failure=False
    )
    if not book_cover:
        if config.config_kobo_proxy:
            log.debug("Cover for unknown book: %s proxied to kobo" % book_uuid)
            return redirect(KOBO_IMAGEHOST_URL +
                            "/{book_uuid}/{width}/{height}/false/image.jpg".format(book_uuid=book_uuid,
                                                                                   width=width,
                                                                                   height=height), 307)
        else:
            log.debug("Cover for unknown book: %s requested" % book_uuid)
            # additional proxy request make no sense, -> direct return
            return make_response(jsonify({}))
    log.debug("Cover request received for book %s" % book_uuid)
    return book_cover 
Example 20
Project: calibre-web   Author: janeczku   File: kobo.py    License: GNU General Public License v3.0 6 votes vote down vote up
def make_calibre_web_auth_response():
    # As described in kobo_auth.py, CalibreWeb doesn't make use practical use of this auth/device API call for
    # authentation (nor for authorization). We return a dummy response just to keep the device happy.
    content = request.get_json()
    AccessToken = base64.b64encode(os.urandom(24)).decode('utf-8')
    RefreshToken = base64.b64encode(os.urandom(24)).decode('utf-8')
    return  make_response(
        jsonify(
            {
                "AccessToken": AccessToken,
                "RefreshToken": RefreshToken,
                "TokenType": "Bearer",
                "TrackingId": str(uuid.uuid4()),
                "UserKey": content['UserKey'],
            }
        )
    ) 
Example 21
Project: x-proxies   Author: cls1991   File: util.py    License: Apache License 2.0 6 votes vote down vote up
def request_html(url, method='GET', headers=None, proxies=None):
    """
    :param url:
    :param method:
    :param headers:
    :param proxies:
    :return:
    """
    resp = None
    try:
        r = requests.request(method, url, headers=headers, proxies=proxies)
        if r.status_code == 200:
            resp = r.text
    except requests.RequestException as e:
        print(e)

    return resp 
Example 22
Project: python-wordpress-json   Author: stylight   File: __init__.py    License: MIT License 5 votes vote down vote up
def _request(self, method_name, **kw):
        method, endpoint, params, data, json, headers = self._prepare_req(
            method_name, **kw
        )

        http_response = requests.request(
            method,
            self.site + endpoint,
            auth=self.auth,
            params=params,
            data=data,
            json=json,
            headers=headers
        )

        if http_response.status_code not in [200, 201]:
            if 'application/json' in http_response.headers.get('Content-Type'):
                code = http_response.json().get('code')
                message = http_response.json().get('message')
            else:
                code = http_response.status_code
                message = http_response.text
            raise WordpressError(" ".join([
                str(http_response.status_code),
                str(http_response.reason),
                ":",
                '[{code}] {message}'.format(code=code, message=message)
            ]))
        elif 'application/json' in http_response.headers.get('Content-Type'):
            return http_response.json()
        else:
            raise WordpressError(" ".join([
                "Expected JSON response but got",
                http_response.headers.get('Content-Type')])) 
Example 23
Project: tvdbsimple   Author: phate89   File: base.py    License: GNU General Public License v3.0 5 votes vote down vote up
def refresh_token(self):
        """
        Refresh the current token set in the module.

        Returns the new obtained valid token for the API.
        """
        self._set_token_header()

        response = requests.request(
            'GET', self._get_complete_url('refresh_token'),
            headers=self._headers)

        response.raise_for_status()
        jsn = response.json()
        if 'token' in jsn:
            from . import KEYS
            KEYS.API_TOKEN = jsn['token']
            return KEYS.API_TOKEN
        return '' 
Example 24
Project: tvdbsimple   Author: phate89   File: base.py    License: GNU General Public License v3.0 5 votes vote down vote up
def get_token(self, forceNew=False):
        """
        Get the existing token or creates it if it doesn't exist.
        Returns the API token.

        If `forceNew` is true  the function will do a new login to retrieve the token.
        """
        from . import KEYS
        if not KEYS.API_TOKEN or forceNew:
            if not KEYS.API_KEY:
                raise APIKeyError

            if hasattr(self, "USER") and hasattr(self, "USER_KEY"):
                data = {"apikey": KEYS.API_KEY, "username": self.USER, "userkey": self.USER_KEY}
            else:
                data = {"apikey": KEYS.API_KEY}

            response = requests.request(
                'POST', self._get_complete_url('login'),
                data=json.dumps(data),
                headers=self._headers)
            if response.status_code == 200:
                KEYS.API_TOKEN = response.json()['token']
            else:
                error = ("Unknown error while authenticating. "
                         "Check your api key or your user/userkey")
                try:
                    error = response.json()['Error']
                except (JSONDecodeError, KeyError):
                    pass
                raise AuthenticationError(error)
        return KEYS.API_TOKEN 
Example 25
Project: harmony-ops   Author: harmony-one   File: get_blockchain.py    License: MIT License 5 votes vote down vote up
def get_latest_header(endpoint):
    url = endpoint
    payload = json.dumps({
        "jsonrpc": "2.0",
        "method": "hmy_latestHeader",
        "params": [
        ],
        "id": 1
    })
    headers = {
        'Content-Type': 'application/json'
    }
    response = requests.request('POST', url, headers=headers, data=payload, allow_redirects=False, timeout=30)
    return json.loads(response.content)["result"] 
Example 26
Project: pypubg   Author: lbrictson   File: core.py    License: MIT License 5 votes vote down vote up
def _get_player_profile(self, player_handle):
        """Returns pubg player profile from PUBG api, no filtering

            :param player_handle: player PUBG profile name
            :type player_handle: str
            :return: return json from PUBG API
            :rtype: dict
        """
        url = self.pubg_url + player_handle
        response = requests.request("GET", url, headers=self.headers)
        data = json.loads(response.text)
        return data 
Example 27
Project: pypubg   Author: lbrictson   File: core.py    License: MIT License 5 votes vote down vote up
def player(self, player_handle):
        """Returns the full set of data on a player, no filtering"""
        try:
            url = self.pubg_url + player_handle
            response = requests.request("GET", url, headers=self.headers)
            return json.loads(response.text)
        except BaseException as error:
            print('Unhandled exception: ' + str(error))
            raise 
Example 28
Project: pypubg   Author: lbrictson   File: core.py    License: MIT License 5 votes vote down vote up
def player_s(self, sid)       :
        """Returns the full set of data on a player, no filtering"""
        try:
            url = self.pubg_url_steam.format(str(sid))
            response = requests.request("GET", url, headers=self.headers)
            return json.loads(response.text)
        except BaseException as error:
            print('Unhandled exception: ' + str(error))
            raise 
Example 29
Project: thingsboard-gateway   Author: thingsboard   File: request_connector.py    License: Apache License 2.0 5 votes vote down vote up
def run(self):
        while not self.__stopped:
            if self.__requests_in_progress:
                for request in self.__requests_in_progress:
                    if time() >= request["next_time"]:
                        thread = Thread(target=self.__send_request, args=(request, self.__convert_queue, log), daemon=True, name="Request to endpoint \'%s\' Thread" % (request["config"].get("url")))
                        thread.start()
            else:
                sleep(.1)
            self.__process_data() 
Example 30
Project: thingsboard-gateway   Author: thingsboard   File: request_connector.py    License: Apache License 2.0 5 votes vote down vote up
def __fill_attribute_updates(self):
        for attribute_request in self.__config.get("attributeUpdates", []):
            if attribute_request.get("converter") is not None:
                converter = TBUtility.check_and_import("request", attribute_request["converter"])(attribute_request)
            else:
                converter = JsonRequestDownlinkConverter(attribute_request)
            attribute_request_dict = {**attribute_request, "converter": converter}
            self.__attribute_updates.append(attribute_request_dict)