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: 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 #3
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 #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: 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 #6
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 #7
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 #8
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 #9
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 #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: 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 #20
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 #21
Source File: kobo.py    From calibre-web with GNU General Public License v3.0 5 votes vote down vote up
def HandleTagAddItem(tag_id):
    items = None
    try:
        tag_request = request.json
        items = tag_request["Items"]
    except (KeyError, TypeError):
        log.debug("Received malformed v1/library/tags/<tag_id>/items/delete request.")
        abort(400, description="Malformed tags POST request. Data is missing 'Items' field")

    shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.uuid == tag_id,
                                              ub.Shelf.user_id == current_user.id).one_or_none()
    if not shelf:
        log.debug("Received Kobo request on a collection unknown to CalibreWeb")
        abort(404, description="Collection isn't known to CalibreWeb")

    if not shelf_lib.check_shelf_edit_permissions(shelf):
        abort(401, description="User is unauthaurized to edit shelf.")

    items_unknown_to_calibre = add_items_to_shelf(items, shelf)
    if items_unknown_to_calibre:
        log.debug("Received request to add an unknown book to a collection. Silently ignoring item.")

    ub.session.merge(shelf)
    ub.session.commit()

    return make_response('', 201) 
Example #22
Source File: kobo.py    From calibre-web with GNU General Public License v3.0 5 votes vote down vote up
def HandleTagUpdate(tag_id):
    shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.uuid == tag_id,
                                              ub.Shelf.user_id == current_user.id).one_or_none()
    if not shelf:
        log.debug("Received Kobo tag update request on a collection unknown to CalibreWeb")
        if config.config_kobo_proxy:
            return redirect_or_proxy_request()
        else:
            abort(404, description="Collection isn't known to CalibreWeb")

    if not shelf_lib.check_shelf_edit_permissions(shelf):
        abort(401, description="User is unauthaurized to edit shelf.")

    if request.method == "DELETE":
        shelf_lib.delete_shelf_helper(shelf)
    else:
        name = None
        try:
            shelf_request = request.json
            name = shelf_request["Name"]
        except (KeyError, TypeError):
            log.debug("Received malformed v1/library/tags rename request.")
            abort(400, description="Malformed tags POST request. Data is missing 'Name' field")

        shelf.name = name
        ub.session.merge(shelf)
        ub.session.commit()
    return make_response(' ', 200)


# Adds items to the given shelf. 
Example #23
Source File: kobo.py    From calibre-web with GNU General Public License v3.0 5 votes vote down vote up
def HandleUnimplementedRequest(dummy=None):
    log.debug("Unimplemented Library Request received: %s", request.base_url)
    return redirect_or_proxy_request()


# TODO: Implement the following routes 
Example #24
Source File: kobo.py    From calibre-web with GNU General Public License v3.0 5 votes vote down vote up
def HandleAuthRequest():
    log.debug('Kobo Auth request')
    if config.config_kobo_proxy:
        try:
            return redirect_or_proxy_request()
        except:
            log.error("Failed to receive or parse response from Kobo's auth endpoint. Falling back to un-proxied mode.")
    return make_calibre_web_auth_response() 
Example #25
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 #26
Source File: OpenIDClient.py    From im with GNU General Public License v3.0 5 votes vote down vote up
def get_user_info_request(token):
        """
        Get a the user info from a token
        """
        try:
            decoded_token = JWT().get_info(token)
            headers = {'Authorization': 'Bearer %s' % token}
            url = "%s%s" % (decoded_token['iss'], "/userinfo")
            resp = requests.request("GET", url, verify=OpenIDClient.VERIFY_SSL, headers=headers)
            if resp.status_code != 200:
                return False, "Code: %d. Message: %s." % (resp.status_code, resp.text)
            return True, json.loads(resp.text)
        except Exception as ex:
            return False, str(ex) 
Example #27
Source File: OpenIDClient.py    From im with GNU General Public License v3.0 5 votes vote down vote up
def get_token_introspection(token, client_id, client_secret):
        """
        Get token introspection
        """
        try:
            decoded_token = JWT().get_info(token)
            url = "%s%s" % (decoded_token['iss'], "/introspect?token=%s&token_type_hint=access_token" % token)
            resp = requests.request("GET", url, verify=OpenIDClient.VERIFY_SSL,
                                    auth=requests.auth.HTTPBasicAuth(client_id, client_secret))
            if resp.status_code != 200:
                return False, "Code: %d. Message: %s." % (resp.status_code, resp.text)
            return True, json.loads(resp.text)
        except Exception as ex:
            return False, str(ex) 
Example #28
Source File: AppDB.py    From im with GNU General Public License v3.0 5 votes vote down vote up
def appdb_call(path):
        """
        Basic AppDB REST API call
        """
        resp = requests.request("GET", AppDB.APPDB_URL + path, verify=False)
        if resp.status_code == 200:
            resp.text.replace('\n', '')
            return xmltodict.parse(resp.text)
        else:
            return None 
Example #29
Source File: tts.py    From im with GNU General Public License v3.0 5 votes vote down vote up
def _perform_get(self, url, headers=None):
        """
        Perform the GET operation on the TTS with the specified URL
        """
        url = "%s://%s:%s%s" % (self.uri_scheme, self.host, self.port, url)
        resp = requests.request("GET", url, verify=self.ssl_verify, headers=headers)

        if resp.status_code >= 200 and resp.status_code <= 299:
            return True, resp.text
        else:
            return False, "Error code %d. Msg: %s" % (resp.status_code, resp.text) 
Example #30
Source File: kobo.py    From calibre-web with GNU General Public License v3.0 5 votes vote down vote up
def HandleProductsRequest(dummy=None):
    log.debug("Unimplemented Products Request received: %s", request.base_url)
    return redirect_or_proxy_request()