Python request json

53 Python code examples are found related to " request json". 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.
Example 1
Source File: handler.py    From clusterfuzz with Apache License 2.0 6 votes vote down vote up
def extend_json_request(req):
  """Extends a request to support JSON."""
  try:
    params = json.loads(req.body)
  except ValueError:
    raise helpers.EarlyExitException(
        'Parsing the JSON request body failed: %s' % req.body, 400)

  def _get(key, default_value=None):
    """Return the value of the key or the default value."""
    return params.get(key, default_value)

  req.get = _get

  # We need the below method because setting req.params raises "can't set
  # attribute" error. It would have been cleaner to replace req.params.
  extend_request(req, params) 
Example 2
Source File: subzone.py    From SubZone with MIT License 6 votes vote down vote up
def request_json(self):
		#request json data to get list of registered subdomains with cert trans records
		subdomains = []
		try:
			r = requests.get(f'https://crt.sh/?q=%.{abuse.parse_url()}&output=json')
			#r = requests.get('https://crt.sh/?q=%.facebook.com&output=json')
			if r.status_code != 200:
				print('{!} host status-code: %s\n ~ unable to access records using this abuse certificate transparency method' % (r.status_code))
			else:
				try:
					json_data = json.loads(r.text)
					for sub in (json_data):
						subdomains.append(sub['name_value'])
				except Exception as e:
					print(f'json_data:Error {e}')
					pass
		except Exception as e:
			print(f'request_json//Error: {e}')
			pass
		return subdomains 
Example 3
Source File: check_update.py    From bazarr with GNU General Public License v3.0 6 votes vote down vote up
def request_json(url, **kwargs):
    """
    Wrapper for `request_response', which will decode the response as JSON
    object and return the result, if no exceptions are raised.

    As an option, a validator callback can be given, which should return True
    if the result is valid.
    """
    
    validator = kwargs.pop("validator", None)
    response = request_response(url, **kwargs)
    
    if response is not None:
        try:
            result = response.json()
            
            if validator and not validator(result):
                logging.error("BAZARR JSON validation result failed")
            else:
                return result
        except ValueError:
            logging.error("BAZARR Response returned invalid JSON data") 
Example 4
Source File: http_helpers.py    From snipsmanager with MIT License 6 votes vote down vote up
def post_request_json(url, data, headers={}):
    """

    :param url:
    :type url: basestring
    :param data:
    :type data: dict
    :param headers:
    :type headers: dict
    :return:
    :rtype: dict
    """
    headers['Content-Type'] = 'application/json'
    headers['Accept'] = 'application/json'

    response, info = post_request(url, data, headers)
    return json.loads(response), info 
Example 5
Source File: handlers_test.py    From jupyter_http_over_ws with Apache License 2.0 6 votes vote down vote up
def get_request_json(self,
                       path,
                       message_id,
                       method='GET',
                       body=None,
                       body_base64=None):
    request = {
        'path': path,
        'message_id': message_id,
        'method': method,
    }
    if body is not None:
      request['body'] = body
    if body_base64 is not None:
      request['body_base64'] = body_base64
    return json.dumps(request) 
Example 6
Source File: web_session.py    From bili2.0 with MIT License 6 votes vote down vote up
def request_json(self,
                           method,
                           url,
                           ctrl: Ctrl = DEFAULT_CTRL,
                           **kwargs) -> dict:
        while True:
            body = await self._req(self._recv_json, method, url, **kwargs)
            if not isinstance(body, dict):  # 这里是强制定制的,与b站配合的!!!!
                continue
            json_rsp_type = ctrl.verify(body)
            if json_rsp_type == JsonRspType.OK:
                return body
            elif json_rsp_type == JsonRspType.IGNORE:
                await asyncio.sleep(0.75)
            elif json_rsp_type == JsonRspType.LOGOUT:
                print('api提示没有登录')
                print(body)
                raise LogoutError(msg='提示没有登陆') 
Example 7
Source File: pipeline.py    From cloudml-samples with Apache License 2.0 6 votes vote down vote up
def make_request_json(self, uri, output_json):
    """Produces a JSON request suitable to send to CloudML Prediction API.

    Args:
      uri: The input image URI.
      output_json: File handle of the output json where request will be written.
    """
    def _open_file_read_binary(uri):
      try:
        return file_io.FileIO(uri, mode='rb')
      except errors.InvalidArgumentError:
        return file_io.FileIO(uri, mode='r')

    with open(output_json, 'w') as outf:
      with _open_file_read_binary(uri) as f:
        image_bytes = f.read()
        image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
        image = image.resize((299, 299), Image.BILINEAR)
        resized_image = io.BytesIO()
        image.save(resized_image, format='JPEG')
        encoded_image = base64.b64encode(resized_image.getvalue())
        row = json.dumps({'key': uri, 'image_bytes': {'b64': encoded_image}})
        outf.write(row)
        outf.write('\n') 
Example 8
Source File: base.py    From omniduct with MIT License 6 votes vote down vote up
def request_json(self, endpoint, method='get', **kwargs):
        """
        Request JSON data from a nominated endpoint.

        Args:
            endpoint (str): The endpoint from which to receive data.
            method (str): The method to use when requestion this resource.
            **kwargs (dict): Additional arguments to pass through to
                `requests.request`.

        Returns:
            list, dict: The representation of the JSON response from the server.
        """
        request = self.request(endpoint, method=method, **kwargs)
        if not request.status_code == 200:
            try:
                raise RuntimeError("Server responded with HTTP response code {}, with content: {}.".format(request.status_code, json.dumps(request.json())))
            except:
                raise RuntimeError("Server responded with HTTP response code {}, with content: {}.".format(request.status_code, request.content.decode('utf-8')))
        return request.json() 
Example 9
Source File: storm.py    From integrations-extras with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_request_json(self, url_part, error_message, params=None):
        url = "{}{}".format(self.nimbus_server, url_part)
        try:
            self.log.debug("Fetching url %s", url)
            if params:
                self.log.debug("Request params: %s", params)
            resp = requests.get(url, params=params)
            resp.encoding = 'utf-8'
            data = resp.json()
            # Log response data exluding configuration section
            self.log.debug("Response data: %s", json.dumps({x: data[x] for x in data if x != 'configuration'}))
            if 'error' in data:
                self.log.warning("Error message returned in JSON response")
                raise Exception(data['error'])
            resp.raise_for_status()
            return data
        except requests.exceptions.ConnectionError as e:
            self.log.error("Unable to establish a connection to Storm UI [url:%s]", self.nimbus_server)
            raise e
        except Exception as e:
            self.log.warning("[url:%s] %s", url, error_message)
            self.log.exception(e)
            raise e 
Example 10
Source File: http_helper.py    From ns4_chatbot with Apache License 2.0 5 votes vote down vote up
def do_request_json(url,param):
    try:
        headers = {'Content-Type': 'application/json'}
        request = urllib2.Request(url=url, headers=headers, data=json.dumps(param))
        response = urllib2.urlopen(request,timeout=3)
        r = response.read().decode('utf-8')
        return json.loads(r)
    except Exception as e:
        logger.warn("http调用失败:%s",str(e))
        return None


#post请求 param = {"data": ""} 
Example 11
Source File: home.py    From bluemix-python-eve-sample with Apache License 2.0 5 votes vote down vote up
def request_wants_json():
    best = request.accept_mimetypes \
        .best_match(['application/json',
                     'application/json; charset=utf-8',
                     'text/html'])
    return best == 'application/json' and \
        request.accept_mimetypes[best] > \
        request.accept_mimetypes['text/html'] 
Example 12
Source File: serializer.py    From jaeger-client-python with Apache License 2.0 5 votes vote down vote up
def join_trace_request_to_json(downstream, server_role):
    req = {}
    if downstream is not None:
        req['downstream'] = traced_service_object_to_json(downstream)
    if server_role is not None:
        req['serverRole'] = str(server_role)
    return json.dumps(req)

#
# Serializers for the upstream responses
# 
Example 13
Source File: youtube.py    From rssit with MIT License 5 votes vote down vote up
def request_youtube_json(config, path, regex):
    youtubepage = request_youtube_webpage(config, path + "?disable_polymer=0")
    #print(youtubepage)
    jsondata = re.search(regex, youtubepage)
    if not jsondata:
        return None

    json = rssit.util.json_loads(jsondata.group("json"))
    return json 
Example 14
Source File: ff_ci_pr_build.py    From dask-image with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def request_json(url, headers={}):
    request = Request(url, headers=headers)
    with contextlib.closing(urlopen(request)) as response:
        reader = codecs.getreader("utf-8")
        return json.load(reader(response)) 
Example 15
Source File: reporter.py    From itc-reporter with MIT License 5 votes vote down vote up
def build_json_request_string(credentials, query):
    """Build a JSON string from the urlquoted credentials and the actual query input"""

    userid, accessToken, password, account, mode = credentials

    request = dict(userid=userid, version=VERSION, mode=mode, queryInput=query)
    if account: request.update(account=account) # empty account info would result in error 404
    if accessToken: request.update(accesstoken=accessToken)
    if password: request.update(password=password)

    return urllib.urlencode(dict(jsonRequest=json.dumps(request))) 
Example 16
Source File: ChromeREPLHelpers.py    From ChromeREPL with MIT License 5 votes vote down vote up
def request_json_from_chrome():
  settings = sublime.load_settings('ChromeREPL.sublime-settings')
  try:
    return requests.get('http://{}:{}/json'.format(settings.get('hostname'),
                                                   settings.get('port')))
  except requests.exceptions.ConnectionError as e:
    return None 
Example 17
Source File: rpc_handler.py    From trinity with MIT License 5 votes vote down vote up
def load_json_request(request: web.Request) -> Any:
    try:
        body_json = await request.json()
    except Exception:
        raise JsonParsingException(f"Invalid request: {request}")
    else:
        return body_json 
Example 18
Source File: requestdict.py    From open-context-py with GNU General Public License v3.0 5 votes vote down vote up
def make_request_dict_json(self, request, spatial_context):
        """ makes a JSON object of the request object
            to help deal with memory problems
        """
        request_dict = self.make_request_obj_dict(request, spatial_context)
        json_string = json.dumps(request_dict,
                                 ensure_ascii=False, indent=4)
        return json_string 
Example 19
Source File: utils.py    From pizzapi with MIT License 5 votes vote down vote up
def request_json(url, **kwargs):
    """Send a GET request to one of the API endpoints that returns JSON.

    Send a GET request to an endpoint, ideally a URL from the urls module.
    The endpoint is formatted with the kwargs passed to it.

    This will error on an invalid request (requests.Request.raise_for_status()), but will otherwise return a dict.
    """
    r = requests.get(url.format(**kwargs))
    r.raise_for_status()
    return r.json() 
Example 20
Source File: generic.py    From ceph-lcm with Apache License 2.0 5 votes vote down vote up
def request_json(self):
        """Tries to parse JSON body (with caching).

        Raises proper exception on problems.
        """

        try:
            return flask.request.get_json(force=True)
        except werkzeug.exceptions.BadRequest as exc:
            LOG.error("Cannot process user request: %s", exc)
            raise exceptions.NotAcceptable() from exc 
Example 21
Source File: request.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def request_json(url, **kwargs):
    """
    Wrapper for `request_response', which will decode the response as JSON
    object and return the result, if no exceptions are raised.

    As an option, a validator callback can be given, which should return True
    if the result is valid.
    """

    validator = kwargs.pop("validator", None)
    response = request_response(url, **kwargs)

    if response is not None:
        try:
            result = response.json()

            if validator and not validator(result):
                logger.error("JSON validation result failed")
            else:
                return result
        except ValueError:
            logger.error("Response returned invalid JSON data")

            # Debug response
            if plexpy.VERBOSE:
                server_message(response) 
Example 22
Source File: Curl.py    From BiliBiliHelper with GNU General Public License v3.0 5 votes vote down vote up
def request_json(self,
                     method,
                     url,
                     headers=None,
                     data=None,
                     params=None,
                     sign=True):
        i = 0
        while True:
            i += 1
            if i >= 10:
                Log.warning(url)
            try:
                if method == "GET":
                    if sign == True:
                        params = Sign(params)
                    if self.proxies != None:
                        r = requests.get(url, headers=headers, params=params, proxies=self.proxies)
                    else:
                        r = requests.get(url, headers=headers, params=params)
                    return json.loads(r.text)
                elif method == "POST":
                    if sign == True:
                        data = Sign(data)
                    if self.proxies != None:
                        r = requests.post(url, headers=headers, data=data, proxies=self.proxies)
                    else:
                        r = requests.post(url, headers=headers, data=data)
                    return json.loads(r.text)
            except Exception as e:
                Log.error(e)
                continue 
Example 23
Source File: IS05Utils.py    From nmos-testing with Apache License 2.0 5 votes vote down vote up
def checkCleanRequestJSON(self, method, dest, data=None, code=200):
        valid, response = self.checkCleanRequest(method, dest, data, code)
        if valid:
            try:
                return True, response.json()
            except Exception:
                # Failed parsing JSON
                return False, "Invalid JSON received"
        else:
            return valid, response 
Example 24
Source File: resources.py    From canvas with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def request_finish_json(request, data):
    response = request_jsonp(request, data)
    request.setHeader('Content-type', 'text/javascript')
    request.write(response)
    request.finish() 
Example 25
Source File: utils.py    From graphite-api with Apache License 2.0 5 votes vote down vote up
def request_json():
    if hasattr(request, 'get_json'):
        return request.get_json()
    else:
        return request.json 
Example 26
Source File: paypal.py    From finance-dl with GNU General Public License v2.0 5 votes vote down vote up
def make_json_request(self, url):
        return self.driver.request(
            'GET', url, headers={
                'x-csrf-token': self.get_csrf_token(),
                'accept': 'application/json, text/javascript, */*; q=0.01',
                'x-requested-with': 'XMLHttpRequest'
            }) 
Example 27
Source File: module_helpers.py    From simplydomain with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def request_json(self, url, return_code=200):
        """
        Request JSON content and expected return code.
        :param url:  URL to request
        :param return_code: expected return code or raise error
        :return: JSON, SuccessState
        """
        try:
            header = {
                'User-Agent': str(self.ua.google)
            }
            if not validators.url(url):
                self.print_yellow(
                    " [!] Invalid URL Requested: %s" % (str(url)))
                return {}, False
            r = requests.get(url, headers=header)
            if r.status_code != return_code:
                self.print_yellow(" [!] Request returned invalid status code: (CODE): %s (EXPECTED): %s" %
                                  (str(r.status_code), str(return_code)))
                return {}, False
            return r.content, True
        except requests.ConnectTimeout as e:
            self.print_red(
                " [!] Request ConnectionTimeout: (URL): %s (ERROR): %s" % (str(url), str(e)))
            return {}, False
        except requests.TooManyRedirects as e:
            self.print_red(
                " [!] Request TooManyRedirects: (URL): %s (ERROR): %s" % (str(url), str(e)))
            return {}, False
        except requests.HTTPError as e:
            self.print_red(
                " [!] Request TooManyRedirects: (URL): %s (ERROR): %s" % (str(url), str(e)))
            return {}, False
        except ConnectionError as e:
            self.print_red(
                " [!] Request ConnectionError: (URL): %s (ERROR): %s" % (str(url), str(e)))
            return {}, False
        except Exception as e:
            self.print_red(
                " [!] Request Unknown Error: (URL): %s (ERROR): %s" % (str(url), str(e)))
            return {}, False 
Example 28
Source File: __init__.py    From rtv with MIT License 5 votes vote down vote up
def request_json(self, url, params=None, data=None, as_objects=True,
                     retry_on_error=True, method=None):
        """Get the JSON processed from a page.

        :param url: the url to grab content from.
        :param params: a dictionary containing the GET data to put in the url
        :param data: a dictionary containing the extra data to submit
        :param as_objects: if True return reddit objects else raw json dict.
        :param retry_on_error: if True retry the request, if it fails, for up
            to 3 attempts
        :returns: JSON processed page

        """
        if not url.endswith('.json'):
            url += '.json'
        response = self._request(url, params, data, method=method,
                                 retry_on_error=retry_on_error)
        hook = self._json_reddit_objecter if as_objects else None
        # Request url just needs to be available for the objecter to use
        self._request_url = url  # pylint: disable=W0201

        if response == '':
            # Some of the v1 urls don't return anything, even when they're
            # successful.
            return response

        data = json.loads(response, object_hook=hook)
        delattr(self, '_request_url')
        # Update the modhash
        if isinstance(data, dict) and 'data' in data \
                and 'modhash' in data['data']:
            self.modhash = data['data']['modhash']
        return data 
Example 29
Source File: request.py    From mlbv with GNU General Public License v3.0 5 votes vote down vote up
def request_json(url, output_filename=None, cache_stale=None):
    """Sends a request expecting a json-formatted response.
    If output_filename is given, then the output is saved to file.
    This also enables basic caching, where cache_stale is the number of seconds
    since file is last modified before the cached file is considered stale (0 means disable the cache).
    """
    cache_stale = _get_cache_stale_secs(cache_stale)
    # Guard against very long filenames:
    if output_filename and len(output_filename) >= MAX_CACHE_FILENAME_LEN:
        output_filename = output_filename[0:MAX_CACHE_FILENAME_LEN-1]
    if output_filename and cache_stale:
        if output_filename in CACHE:
            return CACHE[output_filename]
        json_file = os.path.join(_get_cachedir(), '{}.json'.format(output_filename))
        if os.path.exists(json_file) and (int(time.time()) - os.path.getmtime(json_file) < cache_stale):
            with open(json_file) as jfh:
                CACHE[output_filename] = json.load(jfh)
            if config.DEBUG:
                LOG.info('Loaded from cache: %s', output_filename)
            return CACHE[output_filename]

    LOG.debug('Getting url=%s ...', url)
    headers = {
        'User-Agent': config.CONFIG.ua_iphone,
        'Connection': 'close'
    }
    util.log_http(url, 'get', headers, sys._getframe().f_code.co_name)
    response = requests.get(url, headers=headers, verify=config.VERIFY_SSL)
    response.raise_for_status()

    # Note: this fails on windows in some cases https://github.com/kennethreitz/requests-html/issues/171
    if output_filename is not None or (config.DEBUG and config.SAVE_JSON_FILE):
        json_file = os.path.join(_get_cachedir(), '{}.json'.format(output_filename))
        with open(json_file, 'w', encoding='utf-8') as out:  # write date to json_file
            out.write(response.text)
    if cache_stale:
        LOG.debug('Caching url=%s, filename=%s', url, output_filename)
        CACHE[output_filename] = response.json()
        return CACHE[output_filename]
    return response.json() 
Example 30
Source File: data_store.py    From DHV3 with GNU Affero General Public License v3.0 5 votes vote down vote up
def request_json(self, url):
        self.bot.logger.debug(f"-> {url}")
        headers = self.headers
        async with aiohttp.ClientSession() as cs:
            async with cs.get(url, headers=headers) as r:
                res = await r.json()
                self.bot.logger.debug(f"<- {res[:75]}")
                return res 
Example 31
Source File: Requester.py    From gist-alfred with MIT License 5 votes vote down vote up
def requestJson(self, verb, url, parameters=None, headers=None, input=None, cnx=None):
        def encode(input):
            return "application/json", json.dumps(input)

        return self.__requestEncode(cnx, verb, url, parameters, headers, input, encode) 
Example 32
Source File: util.py    From deep-smoke-machine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def request_json(url):
    r = requests.get(url)
    if r.status_code == 200:
        return r.json()
    else:
        return None


# Convert a defaultdict to dict 
Example 33
Source File: mcafee_request.py    From insightconnect-plugins with MIT License 5 votes vote down vote up
def make_json_request(self, method, path, params=None, data=None, headers=None, files=None, full_response: bool = False):
        response = {"text": ""}

        try:
            response = requests.request(
                method,
                f"{self.url}/{path}",
                data=data,
                params=params,
                files=files,
                headers=headers,
                verify=self.verify_ssl
            )

            if response.status_code == 403:
                raise PluginException(preset=PluginException.Preset.API_KEY)
            if response.status_code >= 400:
                raise PluginException(preset=PluginException.Preset.UNKNOWN, data=response.text)
            if 200 <= response.status_code < 300:
                if full_response:
                    return response

                return response.json()

            raise PluginException(preset=PluginException.Preset.UNKNOWN, data=response.text)
        except json.decoder.JSONDecodeError as e:
            self.logger.info(f"Invalid JSON: {e}")
            raise PluginException(preset=PluginException.Preset.INVALID_JSON, data=response.text)
        except requests.exceptions.HTTPError as e:
            self.logger.info(f"Call to McAfee ATD API failed: {e}")
            raise PluginException(preset=PluginException.Preset.UNKNOWN, data=response.text) 
Example 34
Source File: microWebSrv.py    From MicroWebSrv with MIT License 5 votes vote down vote up
def ReadRequestContentAsJSON(self) :
            data = self.ReadRequestContent()
            if data :
                try :
                    return loads(data.decode())
                except :
                    pass
            return None
        
    # ============================================================================
    # ===( Class Response  )======================================================
    # ============================================================================ 
Example 35
Source File: views.py    From pdfhook with MIT License 5 votes vote down vote up
def request_wants_json():
    best = request.accept_mimetypes \
        .best_match(['application/json', 'text/html'])
    return best == 'application/json' and \
        request.accept_mimetypes[best] > \
        request.accept_mimetypes['text/html'] 
Example 36
Source File: microWebCli.py    From MicroWebCli with MIT License 5 votes vote down vote up
def OpenRequestJSONData(self, o=None) :
        if not 'json' in globals() :
            import json
        try :
            data = json.dumps(o)
        except :
            raise Exception('Error to convert object to JSON format')
        self.OpenRequest( data          = data,
                          contentType   = 'application/json' )

    # ------------------------------------------------------------------------ 
Example 37
Source File: wsgi.py    From ariadne with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def extract_data_from_json_request(self, environ: dict) -> Any:
        request_content_length = self.get_request_content_length(environ)
        request_body = self.get_request_body(environ, request_content_length)

        try:
            return json.loads(request_body)
        except ValueError:
            raise HttpBadRequestError("Request body is not a valid JSON") 
Example 38
Source File: pydevd_api.py    From PyDev.Debugger with Eclipse Public License 1.0 5 votes vote down vote up
def request_change_variable_json(self, py_db, request, thread_id):
        '''
        :param SetVariableRequest request:
        '''
        py_db.post_method_as_internal_command(
            thread_id, internal_change_variable_json, request) 
Example 39
Source File: pydevd_api.py    From PyDev.Debugger with Eclipse Public License 1.0 5 votes vote down vote up
def request_exception_info_json(self, py_db, request, thread_id, max_frames):
        py_db.post_method_as_internal_command(
            thread_id,
            internal_get_exception_details_json,
            request,
            thread_id,
            max_frames,
            set_additional_thread_info=set_additional_thread_info,
            iter_visible_frames_info=py_db.cmd_factory._iter_visible_frames_info,
        ) 
Example 40
Source File: pydevd_api.py    From PyDev.Debugger with Eclipse Public License 1.0 5 votes vote down vote up
def request_get_variable_json(self, py_db, request, thread_id):
        '''
        :param VariablesRequest request:
        '''
        py_db.post_method_as_internal_command(
            thread_id, internal_get_variable_json, request) 
Example 41
Source File: MispModules.py    From AIL-framework with GNU Affero General Public License v3.0 5 votes vote down vote up
def build_enrichment_request_json(module_name, var_name, var_value):
    # # TODO: add error handler
    request_dict = {'module': module_name, var_name: var_value}
    # add config
    config_json = build_config_json(module_name)
    if config_json:
        request_dict['config'] = config_json
    return json.dumps(request_dict) 
Example 42
Source File: utils.py    From ssbio with MIT License 5 votes vote down vote up
def request_json(link, outfile, force_rerun_flag, outdir=None):
    """Download a file in JSON format from a web request

    Args:
        link: Link to web request
        outfile: Name of output file
        outdir: Directory of output file
        force_rerun_flag: If true, redownload the file

    Returns:
        dict: contents of the JSON request

    """
    if not outdir:
        outdir = ''
    outfile = op.join(outdir, outfile)

    if force_rerun(flag=force_rerun_flag, outfile=outfile):
        text_raw = requests.get(link)
        my_dict = text_raw.json()
        with open(outfile, 'w') as f:
            json.dump(my_dict, f)

        log.debug('Loaded and saved {} to {}'.format(link, outfile))
    else:
        with open(outfile, 'r') as f:
            my_dict = json.load(f)
        log.debug('Loaded {}'.format(outfile))

    return my_dict 
Example 43
Source File: context.py    From votr with Apache License 2.0 5 votes vote down vote up
def request_json(self, url, **data):
        '''Sends a request to REST API and parses the response as JSON.

        :param url: the URL, either absolute or relative to the AIS server.
        :param method: HTTP method for the request.
        :param \*\*kwargs: arguments for :meth:`requests.Session.request`.
        :return: a dictionary.
        '''
        self.log('benchmark', 'Begin REST network request')
        self.log('http', 'Requesting POST {}'.format(
            url.partition('?')[0]), [url, data])
        url = urljoin(self.rest_url, url)
        response = self.connection.request("POST", url, data=data)
        response.raise_for_status()
        self.log('http', 'Received response', response.text)
        self.log('benchmark', 'End REST network request')

        if not response.url.startswith(self.rest_url):
            raise LoggedOutError('REST login expired.')
        response = json.loads(response.text)

        if response['status'] != 'OK':
            raise RESTServerError(
                "Status: {status} Error: {error}".format(**response))

        self.log('http', 'Parsed JSON data')
        return response['response'] 
Example 44
Source File: vessel.py    From tvalacarta with GNU General Public License v3.0 5 votes vote down vote up
def make_json_request(url, data):
        payload = json.dumps(data).encode('utf-8')
        req = sanitized_Request(url, payload)
        req.add_header('Content-Type', 'application/json; charset=utf-8')
        return req 
Example 45
Source File: WebUI.py    From p2ptv-pi with MIT License 5 votes vote down vote up
def process_json_request(self, method, args = None):
        try:
            return self.doprocess_json_request(method, args=args)
        except:
            print_exc()
            return json.JSONEncoder().encode({'success': 'false'}) 
Example 46
Source File: factory.py    From apidaora with MIT License 5 votes vote down vote up
def make_json_request_body(body: bytes, body_type: Optional[Type[Any]]) -> Any:
    try:
        return orjson.loads(body)
    except JSONDecodeError:
        schema = (
            getattr(body_type, '__annotations__', {}) if body_type else None
        )
        schema = {k: t.__name__ for k, t in schema.items()}
        raise BadRequestError(name='invalid-body', info={'schema': schema}) 
Example 47
Source File: __init__.py    From AstroBox with GNU Affero General Public License v3.0 5 votes vote down vote up
def getJsonCommandFromRequest(request, valid_commands):
	if not "application/json" in request.headers["Content-Type"]:
		return None, None, make_response("Expected content-type JSON", 400)

	data = request.json
	if not "command" in data.keys() or not data["command"] in valid_commands.keys():
		return None, None, make_response("Expected valid command", 400)

	command = data["command"]
	for parameter in valid_commands[command]:
		if not parameter in data:
			return None, None, make_response("Mandatory parameter %s missing for command %s" % (parameter, command), 400)

	return command, data, None 
Example 48
Source File: dropbox.py    From dropbox-sdk-python with MIT License 5 votes vote down vote up
def request_json_object(self,
                            host,
                            route_name,
                            route_style,
                            request_arg,
                            request_binary,
                            timeout=None):
        """
        Makes a request to the Dropbox API, taking a JSON-serializable Python
        object as an argument, and returning one as a response.

        :param host: The Dropbox API host to connect to.
        :param route_name: The name of the route to invoke.
        :param route_style: The style of the route.
        :param str request_arg: A JSON-serializable Python object representing
            the argument for the route.
        :param Optional[bytes] request_binary: Bytes representing the binary
            payload. Use None if there is no binary payload.
        :param Optional[float] timeout: Maximum duration in seconds
            that client will wait for any single packet from the
            server. After the timeout the client will give up on
            connection. If `None`, will use default timeout set on
            Dropbox object.  Defaults to `None`.
        :return: The route's result as a JSON-serializable Python object.
        """
        serialized_arg = json.dumps(request_arg)
        res = self.request_json_string_with_retry(host,
                                                  route_name,
                                                  route_style,
                                                  serialized_arg,
                                                  request_binary,
                                                  timeout=timeout)
        # This can throw a ValueError if the result is not deserializable,
        # but that would be completely unexpected.
        deserialized_result = json.loads(res.obj_result)
        if isinstance(res, RouteResult) and res.http_resp is not None:
            return (deserialized_result, res.http_resp)
        else:
            return deserialized_result 
Example 49
Source File: download-blockchain-wallet.py    From btcrecover with GNU General Public License v2.0 5 votes vote down vote up
def do_request_json(query, body = None):
    return json.load(do_request(query, body))


# Get an auth_token 
Example 50
Source File: interface.py    From plaso with Apache License 2.0 4 votes vote down vote up
def MakeRequestAndDecodeJSON(self, url, method, **kwargs):
    """Make a HTTP request and decode the results as JSON.

    Args:
      url (str): URL to make a request to.
      method (str): HTTP method to used to make the request. GET and POST are
          supported.
      kwargs: parameters to the requests .get() or post() methods, depending
          on the value of the method parameter.

    Returns:
      dict[str, object]: body of the HTTP response, decoded from JSON.

    Raises:
      ConnectionError: If it is not possible to connect to the given URL, or it
          the request returns a HTTP error.
      ValueError: If an invalid HTTP method is specified.
    """
    method_upper = method.upper()
    if method_upper not in ('GET', 'POST'):
      raise ValueError('Method {0:s} is not supported')

    try:
      if method_upper == 'GET':
        response = requests.get(url, **kwargs)

      elif method_upper == 'POST':
        response = requests.post(url, **kwargs)

      response.raise_for_status()

    except requests.ConnectionError as exception:
      error_string = 'Unable to connect to {0:s} with error: {1!s}'.format(
          url, exception)
      raise errors.ConnectionError(error_string)

    except requests.HTTPError as exception:
      error_string = '{0:s} returned a HTTP error: {1!s}'.format(
          url, exception)
      raise errors.ConnectionError(error_string)

    return response.json() 
Example 51
Source File: utils.py    From mnamer with MIT License 4 votes vote down vote up
def request_json(
    url, parameters=None, body=None, headers=None, cache=True
) -> Tuple[int, dict]:
    """
    Queries a url for json data.

    Note: Requests are cached using requests_cached for a week, this is done
    transparently by using the package's monkey patching.
    """
    assert url
    session = get_session()

    if isinstance(headers, dict):
        headers = clean_dict(headers)
    else:
        headers = dict()
    if isinstance(parameters, dict):
        parameters = [(k, v) for k, v in clean_dict(parameters).items()]
    if body:
        method = "POST"
        headers["content-type"] = "application/json"
        headers["content-length"] = str(len(body))
    else:
        method = "GET"
    headers["user-agent"] = (
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, "
        "like Gecko) Chrome/79.0.3945.88 Safari/537.36"
    )

    initial_cache_state = session._is_cache_disabled  # yes, i'm a bad person
    try:
        session._is_cache_disabled = not cache
        response = session.request(
            url=url,
            params=parameters,
            json=body,
            headers=headers,
            method=method,
            timeout=1,
        )
        status = response.status_code
        content = response.json() if status // 100 == 2 else None
    except:
        content = None
        status = 500
    finally:
        session._is_cache_disabled = initial_cache_state
    return status, content 
Example 52
Source File: dropbox.py    From dropbox-sdk-python with MIT License 4 votes vote down vote up
def request_json_string_with_retry(self,
                                       host,
                                       route_name,
                                       route_style,
                                       request_json_arg,
                                       request_binary,
                                       timeout=None):
        """
        See :meth:`request_json_object` for description of parameters.

        :param request_json_arg: A string representing the serialized JSON
            argument to the route.
        """
        attempt = 0
        rate_limit_errors = 0
        has_refreshed = False
        while True:
            self._logger.info('Request to %s', route_name)
            try:
                return self.request_json_string(host,
                                                route_name,
                                                route_style,
                                                request_json_arg,
                                                request_binary,
                                                timeout=timeout)
            except AuthError as e:
                if e.error and e.error.is_expired_access_token():
                    if has_refreshed:
                        raise
                    else:
                        self._logger.info(
                            'ExpiredCredentials status_code=%s: Refreshing and Retrying',
                            e.status_code)
                        self.refresh_access_token()
                        has_refreshed = True
                else:
                    raise
            except InternalServerError as e:
                attempt += 1
                if attempt <= self._max_retries_on_error:
                    # Use exponential backoff
                    backoff = 2**attempt * random.random()
                    self._logger.info(
                        'HttpError status_code=%s: Retrying in %.1f seconds',
                        e.status_code, backoff)
                    time.sleep(backoff)
                else:
                    raise
            except RateLimitError as e:
                rate_limit_errors += 1
                if (self._max_retries_on_rate_limit is None or
                        self._max_retries_on_rate_limit >= rate_limit_errors):
                    # Set default backoff to 5 seconds.
                    backoff = e.backoff if e.backoff is not None else 5.0
                    self._logger.info(
                        'Ratelimit: Retrying in %.1f seconds.', backoff)
                    time.sleep(backoff)
                else:
                    raise