Python requests.request() Examples

The following are 30 code examples of requests.request(). 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 also want to check out all available functions/classes of the module requests , or try the search function .
Example #1
Source File: get_blockchain.py    From harmony-ops with MIT License 7 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 #2
Source File: base.py    From tvdbsimple with 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 #3
Source File: get_blockchain.py    From harmony-ops with 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 #4
Source File: test.py    From harmony-ops with 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 #5
Source File: client.py    From python-unsplash with 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 #6
Source File: request_connector.py    From thingsboard-gateway with 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 #7
Source File: request_connector.py    From thingsboard-gateway with 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 #8
Source File: request_connector.py    From thingsboard-gateway with 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 #9
Source File: rest_connector.py    From thingsboard-gateway with 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 #10
Source File: test_models.py    From schemathesis with 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 #11
Source File: onelight.py    From ibllib with 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 #12
Source File: __init__.py    From ec2-api with 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 #13
Source File: __init__.py    From ec2-api with 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 #14
Source File: kobo.py    From calibre-web with 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 #15
Source File: kobo.py    From calibre-web with 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 #16
Source File: kobo.py    From calibre-web with 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 #17
Source File: kobo.py    From calibre-web with 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 #18
Source File: util.py    From x-proxies with 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 #19
Source File: __init__.py    From python-wordpress-json with 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 #20
Source File: base.py    From tvdbsimple with 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 #21
Source File: base.py    From tvdbsimple with 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 #22
Source File: base.py    From tvdbsimple with GNU General Public License v3.0 5 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 #23
Source File: get_blockchain.py    From harmony-ops with 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 #24
Source File: interfaces.py    From Jtyoui with MIT License 5 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 #25
Source File: core.py    From pypubg with 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 #26
Source File: core.py    From pypubg with 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 #27
Source File: core.py    From pypubg with 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 #28
Source File: request_connector.py    From thingsboard-gateway with 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 #29
Source File: request_connector.py    From thingsboard-gateway with 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) 
Example #30
Source File: rest_connector.py    From thingsboard-gateway with Apache License 2.0 5 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["downlink_converter"].convert(rpc_request, content)
                    response_queue = Queue(1)
                    request_dict = {"config": {**rpc_request,
                                               **converted_data},
                                    "request": regular_request}
                    request_dict["converter"] = request_dict["config"].get("uplink_converter")
                    with self._app.test_request_context():
                        from flask import request as flask_request
                        rpc_request_thread = Thread(target=self.__send_request,
                                                    args=(request_dict, response_queue, log, flask_request),
                                                    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])
                    else:
                        self.__gateway.send_rpc_reply(device=content["device"], req_id=content["data"]["id"], success_sent=True)

                    del response_queue
        except Exception as e:
            log.exception(e)