requests.codes.ok

Here are the examples of the python api requests.codes.ok taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

167 Examples 7

Example 51

Project: networking-odl Source File: test_mechanism_odl.py
Function: test_update_subnet_postcommit
    def test_update_subnet_postcommit(self):
        self._test_update_resource_postcommit(odl_const.ODL_SUBNET,
                                              requests.codes.ok)
        for status_code in (requests.codes.bad_request,
                            requests.codes.unauthorized,
                            requests.codes.forbidden,
                            requests.codes.not_found,
                            requests.codes.not_implemented):
            self._test_update_resource_postcommit(
                odl_const.ODL_SUBNET, status_code,
                requests.exceptions.HTTPError)

Example 52

Project: vmware-nsx Source File: nsxv3_cleanup.py
    def cleanup_os_switching_profiles(self):
        """
        Cleanup all Switching Profiles created from OpenStack plugin
        """
        sw_profiles = self.get_os_switching_profiles()
        print("Number of OS SwitchingProfiles to be deleted: %s" %
              len(sw_profiles))
        for swp in sw_profiles:
            endpoint = "/switching-profiles/%s" % swp['id']
            response = self.delete(endpoint=endpoint)
            if response.status_code == requests.codes.ok:
                print("Successfully deleted Switching Profile: %s" %
                      swp['display_name'])
            else:
                print("Failed to delete Switching Profile: %s" %
                      swp['display_name'])

Example 53

Project: vmware-nsx Source File: nsxv3_cleanup.py
    def cleanup_os_ns_groups(self):
        """
        Cleanup all NSGroups created from OpenStack plugin
        """
        ns_groups = self.get_ns_groups()
        print("Number of OS NSGroups to be deleted: %s" % len(ns_groups))
        for nsg in ns_groups:
            endpoint = "/ns-groups/%s?force=true" % nsg['id']
            response = self.delete(endpoint=endpoint)
            if response.status_code == requests.codes.ok:
                print("Successfully deleted NSGroup: %s" % nsg['display_name'])
            else:
                print("Failed to delete NSGroup: %s" % nsg['display_name'])

Example 54

Project: kitsune Source File: test_rewrites.py
    @pytest.mark.parametrize(('input'), [
        ('/1/firefox-home/4.0/iPhone/en-US'),
        ('/1/firefox-home/4.0/iPhone/en-US/log-in')])
    def test_iphone_kb_redirects_status_ok(self, base_url, input):
        r = self._check_redirect(base_url, input)
        assert requests.codes.ok == r.status_code

Example 55

Project: EDMarketConnector Source File: companion.py
Function: verify
    def verify(self, code):
        if not code:
            raise VerificationRequired()
        r = self.session.post(URL_CONFIRM, data = {'code' : code}, timeout=timeout)
        if r.status_code != requests.codes.ok or r.url == URL_CONFIRM:	# would have redirected away if success
            raise VerificationRequired()
        self.save()	# Save cookies now for use by command-line app
        self.login()

Example 56

Project: contrail-neutron-plugin Source File: contrail_plugin.py
Function: transform_response
    def _transform_response(self, status_code, info=None, obj_name=None,
                            fields=None):
        if status_code == requests.codes.ok:
            if not isinstance(info, list):
                return self._prune(info, fields)
            else:
                return [self._prune(items, fields) for items in info]

        plugin_base._raise_contrail_error(info, obj_name)

Example 57

Project: vmware-nsx Source File: nsxv3_cleanup.py
    def cleanup_os_logical_switches(self):
        """
        Delete all logical switches created from OpenStack
        """
        lswitches = self.get_os_logical_switches()
        print("Number of OS Logical Switches to be deleted: %s" %
              len(lswitches))
        for ls in lswitches:
            endpoint = '/logical-switches/%s' % ls['id']
            response = self.delete(endpoint=endpoint)
            if response.status_code == requests.codes.ok:
                print("Successfully deleted logical switch %s-%s" %
                      (ls['display_name'], ls['id']))
            else:
                print("Failed to delete lswitch %s-%s, and response is %s" %
                      (ls['display_name'], ls['id'], response.status_code))

Example 58

Project: vmware-nsx Source File: nsxv3_cleanup.py
Function: cleanup_os_logical_ports
    def cleanup_os_logical_ports(self):
        """
        Delete all logical ports created by OpenStack
        """
        lports = self.get_logical_ports()
        os_lports = self.get_os_resources(lports)
        print("Number of OS Logical Ports to be deleted: %s" % len(os_lports))
        # logical port vif detachment
        self.update_logical_port_attachment(os_lports)
        for p in os_lports:
            endpoint = '/logical-ports/%s' % p['id']
            response = self.delete(endpoint=endpoint)
            if response.status_code == requests.codes.ok:
                print("Successfully deleted logical port %s" % p['id'])
            else:
                print("ERROR: Failed to delete lport %s, response code %s" %
                      (p['id'], response.status_code))

Example 59

Project: rauth Source File: base.py
Function: set_up
    def setUp(self):
        response = Mock()
        response.content = json.dumps({'status': 'ok'})
        response.headers = {'Content-Type': 'application/json'}
        response.ok = True
        response.status_code = requests.codes.ok
        self.response = response

Example 60

Project: python_tutorial Source File: outlookservice.py
def get_my_messages(access_token, user_email):
  get_messages_url = outlook_api_endpoint.format('/Me/MailFolders/Inbox/Messages')
  
  # Use OData query parameters to control the results
  #  - Only first 10 results returned
  #  - Only return the ReceivedDateTime, Subject, and From fields
  #  - Sort the results by the ReceivedDateTime field in descending order
  query_parameters = {'$top': '10',
                      '$select': 'ReceivedDateTime,Subject,From',
                      '$orderby': 'ReceivedDateTime DESC'}
                      
  r = make_api_call('GET', get_messages_url, access_token, user_email, parameters = query_parameters)
  
  if (r.status_code == requests.codes.ok):
    return r.json()
  else:
    return "{0}: {1}".format(r.status_code, r.text)

Example 61

Project: kitsune Source File: test_rewrites.py
    @pytest.mark.parametrize(('input'), [
        ('/1/firefox/4.0/WINNT/en-US/prefs-main/'),
        ('/1/firefox/4.0/Darwin/en-US/prefs-main/'),
        ('/1/firefox/4.0/Linux/en-US/prefs-main/'),
        ('/1/firefox/4.0/WINNT/en-US/prefs-clear-private-data/'),
        ('/1/firefox/4.0/Darwin/en-US/prefs-clear-private-data/'),
        ('/1/firefox/4.0/Linux/en-US/prefs-clear-private-data/'),
        ('/1/firefox/4.0/WINNT/en-US/prefs-fonts-and-colors/')])
    def test_kb_redirects_status_ok(self, base_url, input):
        r = self._check_redirect(base_url, input)
        assert requests.codes.ok == r.status_code

Example 62

Project: wikiwho Source File: api.py
Function: process
	def __process(self, requester, data):
		data.update({'format': 'json'})
		response = requester(self.uri, data)
	
		if response.status_code == requests.codes.ok:
			#print(response.text)
			js = json.loads(response.text)
			if 'error' in js: 
				raise MediaWikiAPIError("%s: %s" % (js['error'].get('code'), js['error'].get('info')))
			else:
				return js
			
		else:
			response.raise_for_status()

Example 63

Project: DerPyBooru Source File: request.py
Function: request
def request(params):
  search, p = "https://derpibooru.org/search.json", format_params(params)

  request = get(search, params=p)

  while request.status_code == codes.ok:
    images, image_count = request.json()["search"], 0
    for image in images:
      yield image
      image_count += 1
    if image_count < 50:
      break

    p["page"] += 1

    request = get(search, params=p)

Example 64

Project: networking-mlnx Source File: test_mechanism_sdn.py
    def _get_http_request_codes(self):
        for err_code in (requests.codes.ok,
                         requests.codes.created,
                         requests.codes.no_content,
                         requests.codes.bad_request,
                         requests.codes.unauthorized,
                         requests.codes.forbidden,
                         requests.codes.not_found,
                         requests.codes.conflict,
                         requests.codes.not_implemented,
                         requests.codes.service_unavailable):
            yield err_code

Example 65

Project: lastpass-python Source File: fetcher.py
Function: fetch
def fetch(session, web_client=requests):
    response = web_client.get('https://lastpass.com/getaccts.php?mobile=1&b64=1&hash=0.0&hasplugin=3.0.23&requestsrc=android',
                              cookies={'PHPSESSID': session.id})

    if response.status_code != requests.codes.ok:
        raise NetworkError()

    return blob.Blob(decode_blob(response.content), session.key_iteration_count)

Example 66

Project: vmware-nsx Source File: nsxv3_cleanup.py
Function: update_logical_port_attachment
    def update_logical_port_attachment(self, lports):
        """
        In order to delete logical ports, we need to detach
        the VIF attachment on the ports first.
        """
        for p in lports:
            p['attachment'] = None
            endpoint = "/logical-ports/%s" % p['id']
            response = self.put(endpoint=endpoint, body=p)
            if response.status_code != requests.codes.ok:
                print("ERROR: Failed to update lport %s" % p['id'])

Example 67

Project: contrail-neutron-plugin Source File: contrail_plugin.py
Function: remove_router_interface
    def remove_router_interface(self, context, router_id, interface_info):
        """Delete interface from a router."""

        if not interface_info:
            msg = _("Either subnet_id or port_id must be specified")
            raise exc.BadRequest(resource='router', msg=msg)

        res_dict = self._encode_resource(resource_id=router_id,
                                         resource=interface_info)
        status_code, res_info = self._request_backend(context, res_dict,
                                                      'router', 'DELINTERFACE')
        if status_code != requests.codes.ok:
            plugin_base._raise_contrail_error(info=res_info,
                                              obj_name='remove_router_interface')
        return res_info

Example 68

Project: vmware-nsx Source File: nsxv3_cleanup.py
    def cleanup_os_firewall_sections(self):
        """
        Cleanup all firewall sections created from OpenStack
        """
        fw_sections = self.get_os_firewall_sections()
        print("Number of OS Firewall Sections to be deleted: %s" %
              len(fw_sections))
        for fw in fw_sections:
            self.cleanup_firewall_section_rules(fw)
            endpoint = "/firewall/sections/%s" % fw['id']
            response = self.delete(endpoint=endpoint)
            if response.status_code == requests.codes.ok:
                print("Successfully deleted firewall section %s" %
                      fw['display_name'])
            else:
                print("Failed to delete firewall section %s" %
                      fw['display_name'])

Example 69

Project: lastpass-python Source File: fetcher.py
def request_iteration_count(username, web_client=requests):
    response = web_client.post('https://lastpass.com/iterations.php',
                               data={'email': username})
    if response.status_code != requests.codes.ok:
        raise NetworkError()

    try:
        count = int(response.content)
    except:
        raise InvalidResponseError('Key iteration count is invalid')

    if count > 0:
        return count
    raise InvalidResponseError('Key iteration count is not positive')

Example 70

Project: dweepy Source File: api.py
Function: request
def _request(method, url, **kwargs):
    """Make HTTP request, raising an exception if it fails.
    """
    url = BASE_URL + url
    request_func = getattr(requests, method)
    response = request_func(url, **kwargs)
    # raise an exception if request is not successful
    if not response.status_code == requests.codes.ok:
        raise DweepyError('HTTP {0} response'.format(response.status_code))
    response_json = response.json()
    if response_json['this'] == 'failed':
        raise DweepyError(response_json['because'])
    return response_json['with']

Example 71

Project: vmware-nsx Source File: nsxv3_cleanup.py
    def cleanup_logical_router_ports(self, lrouter):
        """
        Cleanup all logical ports on a logical router
        """
        lports = self.get_os_logical_router_ports(lrouter)
        for lp in lports:
            endpoint = "/logical-router-ports/%s" % lp['id']
            response = self.delete(endpoint=endpoint)
            if response.status_code == requests.codes.ok:
                print("Successfully deleted logical router port %s-%s" %
                      (lp['display_name'], lp['id']))
            else:
                print("Failed to delete lr port %s-%s, and response is %s" %
                      (lp['display_name'], lp['id']))

Example 72

Project: mangopay2-python-sdk Source File: resttool.py
    def _checkResponseCode(self, response, decodedResp):
        """Check response code.
        param object response Response from REST API
        @throws RequestException If response code not OK
        """
        
        if response.status_code != requests.codes.ok and response.status_code != requests.codes.no_content:
            message = str(response.status_code)
            if decodedResp != None and decodedResp.get('Message') != None:
                message = decodedResp.get('Message') + u' '
            if decodedResp != None and decodedResp.get('errors') != None:
                message = message + u'Errors: '
                for e in decodedResp.get('errors'):
                    message = message + u'[' + e + u': ' + decodedResp.get('errors')[e] + u'] '
            raise ResponseException(response.request.url, response.status_code, message)

Example 73

Project: contrail-neutron-plugin Source File: contrail_plugin.py
Function: delete_resource
    def _delete_resource(self, res_type, context, id):
        """Delete a resource in API server

        This method deletes a resource in the contrail api server
        """

        res_dict = self._encode_resource(resource_id=id)
        LOG.debug("delete_%(res_type)s(): %(id)s",
                  {'res_type': res_type, 'id': id})
        status_code, res_info = self._request_backend(context, res_dict,
                                                      res_type, 'DELETE')
        if status_code != requests.codes.ok:
            plugin_base._raise_contrail_error(info=res_info,
                                              obj_name=res_type)

Example 74

Project: EDMarketConnector Source File: eddn.py
Function: send
    def send(self, cmdr, msg):
        msg['header'] = {
            'softwareName'    : '%s [%s]' % (applongname, platform=='darwin' and "Mac OS" or system()),
            'softwareVersion' : appversion,
            'uploaderID'      : config.getint('anonymous') and hashlib.md5(cmdr.encode('utf-8')).hexdigest() or cmdr.encode('utf-8'),
        }
        if not msg['message'].get('timestamp'):	# already present in journal messages
            msg['message']['timestamp'] = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(config.getint('querytime') or int(time.time())))

        r = self.session.post(self.UPLOAD, data=json.dumps(msg), timeout=timeout)
        if __debug__ and r.status_code != requests.codes.ok:
            print 'Status\t%s'  % r.status_code
            print 'URL\t%s'  % r.url
            print 'Headers\t%s' % r.headers
            print ('Content:\n%s' % r.text).encode('utf-8')
        r.raise_for_status()

Example 75

Project: python_tutorial Source File: outlookservice.py
def get_my_events(access_token, user_email):
  get_events_url = outlook_api_endpoint.format('/Me/Events')
  
  # Use OData query parameters to control the results
  #  - Only first 10 results returned
  #  - Only return the Subject, Start, and End fields
  #  - Sort the results by the Start field in ascending order
  query_parameters = {'$top': '10',
                      '$select': 'Subject,Start,End',
                      '$orderby': 'Start/DateTime ASC'}
                      
  r = make_api_call('GET', get_events_url, access_token, user_email, parameters = query_parameters)
  
  if (r.status_code == requests.codes.ok):
    return r.json()
  else:
    return "{0}: {1}".format(r.status_code, r.text)

Example 76

Project: saleor Source File: utils.py
Function: handle_response
    def handle_response(self, response):
        response_content = parse_response(response)
        if response.status_code == requests.codes.ok:
            return response_content
        else:
            logger.error('[%s]: %s', response.status_code, response.text)
            error = self.extract_error_from_response(response_content)
            raise ValueError(error)

Example 77

Project: minqlx-plugins Source File: balance.py
    @minqlx.next_frame
    def handle_ratings_fetched(self, request_id, status_code):
        players, callback, channel, args = self.requests[request_id]
        del self.requests[request_id]
        if status_code != requests.codes.ok:
            # TODO: Put a couple of known errors here for more detailed feedback.
            channel.reply("ERROR {}: Failed to fetch ratings.".format(status_code))
        else:
            callback(players, channel, *args)

Example 78

Project: edce-client Source File: query.py
def submitLogin(s, u, p):
	if edce.globals.debug:
		print(">>>>>>>>>>>>>>>> submitLogin")
	url = edce.config.getString('urls','url_login')
	payload = { 'email' : u, 'password' : p }
	r = s.post(url, data=payload, verify=True)
	if r.status_code == requests.codes.ok:
		s.cookies.save()
		return r.text
	else:
		errstr = "Error: submitLogin FAIL %s" % r.status_code
		if edce.globals.debug and edce.globals.interactive:
			print(errstr)
		raise edce.error.ErrorLogin(errstr)

Example 79

Project: kitsune Source File: test_rewrites.py
    @pytest.mark.parametrize(('input', 'expected'), [
        ('/1/mobile/4.0/android/en-US/firefox-help',
         '/en-US/products/mobile/popular-articles-android?as=u&utm_source=inproduct'),
        ('/1/mobile/4.0/iphone/en-US/firefox-help',
         '/en-US/products/mobile/popular-articles-android?as=u&utm_source=inproduct'),
        ('/1/mobile/4.0/nokia/en-US/firefox-help',
         '/en-US/products/mobile/popular-articles-android?as=u&utm_source=inproduct')])
    def test_old_mobile_redirects(self, base_url, input, expected):
        expected_url = base_url + expected
        r = self._check_redirect(base_url, input)
        assert expected_url == urllib.unquote(r.url)
        assert requests.codes.ok == r.status_code

Example 80

Project: vmware-nsx Source File: nsxv3_cleanup.py
    def cleanup_firewall_section_rules(self, fw_section):
        """
        Cleanup all firewall rules for a given fw section
        """
        fw_rules = self.get_firewall_section_rules(fw_section)
        for rule in fw_rules:
            endpoint = "/firewall/sections/%s/rules/%s" % (fw_section['id'],
                                                           rule['id'])
            response = self.delete(endpoint=endpoint)
            if response.status_code == requests.codes.ok:
                print("Successfully deleted fw rule %s in fw section %s" %
                      (rule['display_name'], fw_section['display_name']))
            else:
                print("Failed to delete fw rule %s in fw section %s" %
                      (rule['display_name'], fw_section['display_name']))

Example 81

Project: python-social-auth Source File: untappd.py
    def process_error(self, data):
        """
        All errors from Untappd are contained in the 'meta' key of the response.
        """
        response_code = data.get('meta', {}).get('http_code')
        if response_code is not None and response_code != requests.codes.ok:
            raise AuthFailed(self, data['meta']['error_detail'])

Example 82

Project: DerPyBooru Source File: request.py
Function: get_image_data
def get_image_data(id_number):
  url = "https://derpibooru.org/{}.json?fav=&comments=".format(id_number)

  request = get(url)

  if request.status_code == codes.ok:
    data = request.json()

    if "duplicate_of" in data:
      return get_image_data(data["duplicate_of"])
    else:
      return data

Example 83

Project: networking-odl Source File: test_mechanism_odl.py
Function: test_update_network_postcommit
    def test_update_network_postcommit(self):
        self._test_update_resource_postcommit(odl_const.ODL_NETWORK,
                                              requests.codes.ok)
        for status_code in (requests.codes.bad_request,
                            requests.codes.forbidden,
                            requests.codes.not_found):
            self._test_update_resource_postcommit(
                odl_const.ODL_NETWORK, status_code,
                requests.exceptions.HTTPError)

Example 84

Project: networking-odl Source File: test_mechanism_odl.py
Function: test_update_port_postcommit
    def test_update_port_postcommit(self):
        self._test_update_resource_postcommit(odl_const.ODL_PORT,
                                              requests.codes.ok)
        for status_code in (requests.codes.bad_request,
                            requests.codes.unauthorized,
                            requests.codes.forbidden,
                            requests.codes.not_found,
                            requests.codes.conflict,
                            requests.codes.not_implemented):
            self._test_update_resource_postcommit(
                odl_const.ODL_PORT, status_code,
                requests.exceptions.HTTPError)

Example 85

Project: PythonMiniProbe Source File: probe.py
Function: send_data
    def send_data(self):
        """
        send processed data to the core
        """
        try:
            data_request = self.mini_probe.request_to_core("data", json.dumps(self.data_request_payload_json),
                                                           self.config)
            if data_request.status_code == requests.codes.ok:
                logging.info("Data success.")
                logging.debug("Data success. Details: HTTP Status %s, Message: %s"
                              % (data_request.status_code, data_request.text))
                self.data_request_payload_json = []
            else:
                logging.info("Data issue. Current data might be dropped, please turn on debug logging")
                logging.debug("Data issue. Details: HTTP Status %s, Message: %s"
                              % (data_request.status_code, data_request.text))
        except Exception as request_exception:
            logging.error(request_exception)

Example 86

Project: edce-client Source File: query.py
def submitProfile(s):
	if edce.globals.debug:
		print(">>>>>>>>>>>>>>>> submitProfile")
	url = edce.config.getString('urls','url_profile')
	r = s.get(url, verify=True)
	if r.status_code == requests.codes.ok:
		s.cookies.save()
		return r.text
	else:
		errstr = "Error: submitProfile FAIL %s" % r.status_code
		if edce.globals.debug and edce.globals.interactive:
			print(errstr)
		raise edce.error.ErrorProfile(errstr)

Example 87

Project: minqlx-plugins Source File: balance.py
Function: add_request
    def add_request(self, players, callback, channel, *args):
        req = next(self.request_counter)
        self.requests[req] = players.copy(), callback, channel, args

        # Only start a new thread if we need to make an API request.
        if self.remove_cached(players):
            self.fetch_ratings(players, req)
        else:
            # All players were cached, so we tell it to go ahead and call the callbacks.
            self.handle_ratings_fetched(req, requests.codes.ok)

Example 88

Project: inasafe Source File: push_shake.py
def push_shake_event_to_rest(shake_event, fail_silent=True):
    """Pushing shake event Grid.xml description files to REST server.

    :param shake_event: The shake event to push
    :type shake_event: ShakeEvent

    :param fail_silent: If set True, will still continue whan the push process
        failed. Default vaule to True. If False, this method will raise
        exception.
    :type fail_silent: bool

    :return: Return True if successfully pushed data
    :rtype: bool
    """
    inasafe_django = InaSAFEDjangoREST()
    # check credentials exists in os.environ
    if not inasafe_django.is_configured():
        LOGGER.info('Insufficient information to push shake map to '
                    'Django Realtime')
        LOGGER.info('Please set environment for INASAFE_REALTIME_REST_URL, '
                    'INASAFE_REALTIME_REST_LOGIN_URL, '
                    'INASAFE_REALTIME_REST_USER, and '
                    'INASAFE_REALTIME_REST_PASSWORD')
        return

    event_dict = shake_event.event_dict()

    # set headers and cookie
    # begin communicating with server
    LOGGER.info('----------------------------------')
    LOGGER.info('Push data to REST server: %s', inasafe_django.base_url())
    try:
        session = inasafe_django.rest
        headers = {
            'X-CSRFTOKEN': inasafe_django.csrf_token,
            'Content-Type': 'application/json'
        }

        # build the data request:
        earthquake_data = {
            'shake_id': shake_event.event_id,
            'magnitude': float(event_dict.get('mmi')),
            'depth': float(event_dict.get('depth-value')),
            'time': str(shake_event.shake_grid.time),
            'location': {
                'type': 'Point',
                'coordinates': [
                    shake_event.shake_grid.longitude,
                    shake_event.shake_grid.latitude
                ]
            },
            'location_description': event_dict.get('shake-grid-location')
        }
        earthquake_file = {
            'shake_grid': (
                '%s-grid.xml' % shake_event.event_id,
                open(shake_event.grid_file_path())),
        }
        # check does the shake event already exists?
        response = session.earthquake(
            earthquake_data['shake_id']).GET()
        if response.status_code == requests.codes.ok:
            # event exists, we should update using PUT Url
            response = session.earthquake(
                earthquake_data['shake_id']).PUT(
                data=json.dumps(earthquake_data),
                headers=headers)
        elif response.status_code == requests.codes.not_found:
            # event does not exists, create using POST url
            response = session.earthquake.POST(
                data=json.dumps(earthquake_data),
                headers=headers)

        # upload grid.xml
        headers = {
            'X-CSRFTOKEN': inasafe_django.csrf_token,
        }
        if response.status_code == requests.codes.ok:
            response = session.earthquake(
                earthquake_data['shake_id']).PUT(
                files=earthquake_file,
                headers=headers)

        if not (response.status_code == requests.codes.ok or
                response.status_code == requests.codes.created):
            # raise exceptions
            error = RESTRequestFailedError(
                url=response.url,
                status_code=response.status_code,
                data=json.dumps(earthquake_data))
            if fail_silent:
                LOGGER.info(error.message)
            else:
                raise error

        # post the report
        # build report data
        path_files = shake_event.generate_result_path_dict()
        event_report_dict = {
            'shake_id': shake_event.event_id,
            'language': shake_event.locale
        }
        event_report_files = {
            'report_pdf': open(path_files.get('pdf')),
            'report_image': open(path_files.get('image')),
            'report_thumbnail': open(path_files.get('thumbnail'))
        }
        # check report exists

        # build headers and cookies
        headers = {
            'X-CSRFTOKEN': inasafe_django.csrf_token,
        }
        response = session(
            'earthquake-report',
            event_report_dict['shake_id'],
            event_report_dict['language']).GET()
        if response.status_code == requests.codes.ok:
            # event exists, we should update using PUT Url
            response = session(
                'earthquake-report',
                event_report_dict['shake_id'],
                event_report_dict['language']).PUT(
                data=event_report_dict,
                files=event_report_files,
                headers=headers)
        elif response.status_code == requests.codes.not_found:
            # event doesn't exists, we should update using POST url
            response = session(
                'earthquake-report',
                event_report_dict['shake_id']).POST(
                    data=event_report_dict,
                    files=event_report_files,
                    headers=headers)

        if not (response.status_code == requests.codes.ok or
                response.status_code == requests.codes.created):
            error = RESTRequestFailedError(
                url=response.url,
                status_code=response.status_code,
                data=event_report_dict,
                files=event_report_files)

            if fail_silent:
                LOGGER.info(error.message)
            else:
                raise error

        return True
    # pylint: disable=broad-except
    except Exception as exc:
        if fail_silent:
            LOGGER.warning(exc)
        else:
            raise exc

Example 89

Project: inasafe Source File: push_flood.py
def push_flood_event_to_rest(flood_event, fail_silent=True):
    """Pushing shake event Grid.xml description files to REST server.

    :param flood_event: The flood event to push
    :type flood_event: FloodEvent

    :param fail_silent: If set True, will still continue whan the push process
        failed. Default vaule to True. If False, this method will raise
        exception.
    :type fail_silent:

    :return: Return True if successfully pushed data
    :rtype: bool
    """
    if not flood_event.impact_exists:
        LOGGER.info('No impact exists. Will not push anything')
        return

    inasafe_django = InaSAFEDjangoREST()
    # check credentials exists in os.environ
    if not inasafe_django.is_configured():
        LOGGER.info('Insufficient information to push shake map to '
                    'Django Realtime')
        LOGGER.info('Please set environment for INASAFE_REALTIME_REST_URL, '
                    'INASAFE_REALTIME_REST_LOGIN_URL, '
                    'INASAFE_REALTIME_REST_USER, and '
                    'INASAFE_REALTIME_REST_PASSWORD')
        return

    # set headers and cookie
    # begin communicating with server
    LOGGER.info('----------------------------------')
    LOGGER.info('Push data to REST server: %s', inasafe_django.base_url())
    try:
        session = inasafe_django.rest

        # Create a zipped impact layer
        impact_zip_path = os.path.join(flood_event.report_path, 'impact.zip')

        with ZipFile(impact_zip_path, 'w') as zipf:
            for root, dirs, files in os.walk(flood_event.report_path):
                for f in files:
                    _, ext = os.path.splitext(f)
                    if ('impact' in f and
                            not f == 'impact.zip' and
                            not ext == '.pdf'):
                        filename = os.path.join(root, f)
                        zipf.write(filename, arcname=f)

        # build the data request:
        flood_data = {
            'event_id': flood_event.report_id,
            'time': flood_event.time,
            'interval': flood_event.duration,
            'source': flood_event.source,
            'region': flood_event.region
        }
        flood_data_file = {
            'hazard_layer': open(flood_event.hazard_zip_path),
            'impact_layer': open(impact_zip_path)
        }

        # modify headers
        headers = {
            'X-CSRFTOKEN': inasafe_django.csrf_token,
        }

        # check does the shake event already exists?
        response = session.flood(
            flood_data['event_id']).GET()
        if response.status_code == requests.codes.ok:
            # event exists, we should update using PUT Url
            response = session.flood(
                flood_data['event_id']).PUT(
                data=flood_data,
                files=flood_data_file,
                headers=headers)
        elif response.status_code == requests.codes.not_found:
            # event does not exists, create using POST url
            response = session.flood.POST(
                data=flood_data,
                files=flood_data_file,
                headers=headers)

        if not (response.status_code == requests.codes.ok or
                response.status_code == requests.codes.created):
            # raise exceptions
            error = RESTRequestFailedError(
                url=response.url,
                status_code=response.status_code,
                data=flood_data)
            if fail_silent:
                LOGGER.warning(error.message)
            else:
                raise error

        # post the report
        # build report data
        map_report_path = flood_event.map_report_path
        table_report_path = flood_event.table_report_path

        event_report_dict = {
            'event_id': flood_event.report_id,
            'language': flood_event.locale
        }
        event_report_files = {
            'impact_map': open(map_report_path),
            # 'impact_report': open(table_report_path)
        }
        # check report exists

        # build headers and cookies
        headers = {
            'X-CSRFTOKEN': inasafe_django.csrf_token,
        }
        response = session(
            'flood-report',
            event_report_dict['event_id'],
            event_report_dict['language']).GET()
        if response.status_code == requests.codes.ok:
            # event exists, we should update using PUT Url
            response = session(
                'flood-report',
                event_report_dict['event_id'],
                event_report_dict['language']).PUT(
                data=event_report_dict,
                files=event_report_files,
                headers=headers)
        elif response.status_code == requests.codes.not_found:
            # event doesn't exists, we should update using POST url
            response = session(
                'flood-report',
                event_report_dict['event_id']).POST(
                    data=event_report_dict,
                    files=event_report_files,
                    headers=headers)

        if not (response.status_code == requests.codes.ok or
                response.status_code == requests.codes.created):
            error = RESTRequestFailedError(
                url=response.url,
                status_code=response.status_code,
                data=event_report_dict,
                files=event_report_files)

            if fail_silent:
                LOGGER.warning(error.message)
            else:
                raise error
        return True
    # pylint: disable=broad-except
    except Exception as exc:
        if fail_silent:
            LOGGER.warning(exc)
        else:
            raise exc

Example 90

Project: multidrive Source File: onedrivestorageservice.py
    def upload(self, file_path, destination=None, modified_time=None,
               create_folder=False, overwrite=False):
        logger = logging.getLogger("multidrive")
        logger.info("Upload {} OneDrive Storage Service".format(file_path))

        file_name = os.path.basename(file_path)
        full_remote_path = file_name
        if destination is not None:
            if self.is_folder(destination) is False:
                if create_folder is False:
                    raise RuntimeError("Destination folder not valid")
                self.create_folder(destination)

            if destination.endswith('/') is False:
                destination = destination+"/"
            full_remote_path = destination+full_remote_path

        file_size = os.path.getsize(file_path)

        payload = {}
        payload["@name.conflictBehavior"] = "fail"
        if overwrite is True:
            payload["@name.conflictBehavior"] = "replace"

        # Special case for empty file
        if file_size == 0:
            url = (self.onedrive_url_root+"/drive/root:/" +
                   urllib.parse.quote(full_remote_path)+":/content")
            if self.__app_folder__:
                url = (self.onedrive_url_root+"/drive/special/approot:/" +
                       urllib.parse.quote(full_remote_path)+":/content")

            response = self.http_request(url=url,
                                         request_type=RequestType.PUT,
                                         status_codes=(requests.codes.ok,
                                                       requests.codes.created,
                                                       requests.codes.accepted,
                                                       requests.codes.
                                                       conflict),
                                         data="",
                                         params=payload,
                                         use_access_token=True,
                                         action_string="Upload")

            if response.status_code in (requests.codes.conflict,):
                raise RuntimeError("File already exists")
            logger.info("Upload complete")
            return

        NUM_ATTEMPTS = 5
        cur_attempt = 1
        while cur_attempt <= NUM_ATTEMPTS:
            headers = {'Content-Type': "application/json"}

            url = (self.onedrive_url_root+"/drive/root:/" +
                   urllib.parse.quote(full_remote_path) +
                   ":/upload.createSession")

            if self.__app_folder__:
                url = (self.onedrive_url_root+"/drive/special/approot:/" +
                       urllib.parse.quote(full_remote_path) +
                       ":/upload.createSession")

            response = self.http_request(url=url,
                                         request_type=RequestType.POST,
                                         status_codes=(requests.codes.ok,),
                                         headers=headers,
                                         data=json.dumps(payload),
                                         use_access_token=True,
                                         action_string="Upload",
                                         timeout=120)

            data = json.loads(response.text)

            url = data['uploadUrl']

            CHUNK_SIZE = 10*1024*1024
            chunk_start = 0
            chunk_end = CHUNK_SIZE - 1
            if chunk_end+1 >= file_size:
                chunk_end = file_size - 1
            response = None

            # TODO: Deal with insufficient Storage error (507)
            # TODO: Deal with other 400/500 series errors

            cur_file_hash = hashlib.sha1()
            with open(file_path, "rb") as f:
                retry_chunk = False
                while chunk_start < file_size:
                    if retry_chunk is False:
                        chunk_data = f.read(CHUNK_SIZE)
                        cur_file_hash.update(chunk_data)
                    retry_chunk = False
                    headers = {}
                    headers['Content-Length'] = str(file_size)
                    headers['Content-Range'] = ('bytes {}-{}/{}'.
                                                format(chunk_start,
                                                       chunk_end,
                                                       file_size))
                    status_codes = (requests.codes.ok,
                                    requests.codes.created,
                                    requests.codes.accepted,
                                    requests.codes.conflict,
                                    requests.codes.range_not_satisfiable)
                    # TODO: Further testing on some errors
                    # err_codes = (requests.codes.server_error,)
                    response = self.http_request(url=url,
                                                 request_type=RequestType.PUT,
                                                 headers=headers,
                                                 status_codes=status_codes,
                                                 # severe_status_codes=err_codes,
                                                 data=chunk_data,
                                                 use_access_token=True,
                                                 action_string="Upload Chunk",
                                                 timeout=120)

                    # TODO: Check for proper response based on
                    # location in file uploading.

                    if response.status_code in (requests.codes.conflict,):
                        raise RuntimeError("File Already Exists")

                    if response.status_code in (requests.codes.
                                                range_not_satisfiable,):
                        logger.warning("Got error {}".format(response.text))
                        logger.warning("DEBUG: Getting upload status")
                        logger.warning("Current Chunk  Start: " +
                                       str(chunk_start))
                        upload_status = self.get_upload_status(url)
                        logger.warning("Status: " + str(upload_status))
                        if 'nextExpectedRanges' in upload_status:
                            new_start_range = int(upload_status
                                                  ['nextExpectedRanges']
                                                  [0].split("-")[0])
                            valid_chunk = (new_start_range > chunk_start and
                                           new_start_range < chunk_end)
                            if (valid_chunk):
                                difference = new_start_range-chunk_start
                                chunk_start = new_start_range
                                chunk_data = chunk_data[difference:]
                                retry_chunk = True
                                logger.warning("Attempting to retry part of "
                                               "current chunk")
                                logger.warning("new chunk start: " +
                                               str(chunk_start))
                                logger.warning("new chunk end: " +
                                               str(chunk_end))
                                continue
                            break

                    print("{} of {} bytes sent, {}% complete"
                          .format(str(chunk_end+1),
                                  str(file_size),
                                  "%.2f" % (float(chunk_end+1)
                                            / float(file_size)*100)),
                          end='\r')
                    chunk_start = chunk_end+1
                    chunk_end += CHUNK_SIZE
                    if chunk_end+1 >= file_size:
                        chunk_end = file_size - 1

            logger.info(response.status_code)
            logger.info(response.text)

            data = json.loads(response.text)
            if ('file' in data and 'hashes' in data['file']):
                server_hash = data['file']['hashes']['sha1Hash']
            else:
                server_hash = "None"

            logger.info("SHA1 local:"+cur_file_hash.hexdigest())
            logger.info("SHA1 remote:"+server_hash)
            if (cur_file_hash.hexdigest() == server_hash.lower()):
                print("\nUpload of file {} complete".
                      format(os.path.basename(file_name)))
                return
            cur_attempt += 1
            logger.warning("Hash of uploaded file does "
                           "not match server.  Attempting again")
            # If it doesn't match, we need to replace the existing file now
            payload["@name.conflictBehavior"] = "replace"

        if (cur_file_hash.hexdigest() != server_hash.lower()):
                raise RuntimeError("Hash of uploaded file does "
                                   "not match server.")

Example 91

Project: flask-googleauth Source File: flask_googleauth.py
    def _on_authentication_verified(self, callback, response):
        ok = response.status_code == requests.codes.ok
        if not ok or "is_valid:true" not in response.content:
            logging.warning("Invalid OpenID response: %s", response.content)
            return callback(None)

        # Make sure we got back at least an email from attribute exchange
        ax_ns = None
        for name in request.args:
            if (name.startswith("openid.ns.") and
               request.args.get(name) == u"http://openid.net/srv/ax/1.0"):
                ax_ns = name[10:]
                break

        def get_ax_arg(uri):
            if not ax_ns:
                return u""
            prefix = "openid.%s.type." % ax_ns
            ax_name = None
            for name in request.args:
                if request.args.get(name) == uri and name.startswith(prefix):
                    part = name[len(prefix):]
                    ax_name = "openid.%s.value.%s" % (ax_ns, part)
                    break
            if not ax_name:
                return u""
            return request.args.get(ax_name, u"")

        email = get_ax_arg("http://axschema.org/contact/email")
        name = get_ax_arg("http://axschema.org/namePerson")
        first_name = get_ax_arg("http://axschema.org/namePerson/first")
        last_name = get_ax_arg("http://axschema.org/namePerson/last")
        username = get_ax_arg("http://axschema.org/namePerson/friendly")
        locale = get_ax_arg("http://axschema.org/pref/language").lower()
        identity = request.args.get("openid.claimed_id", u"")

        user = ObjectDict()
        name_parts = []
        if first_name:
            user["first_name"] = first_name
            name_parts.append(first_name)
        if last_name:
            user["last_name"] = last_name
            name_parts.append(last_name)
        if name:
            user["name"] = name
        elif name_parts:
            user["name"] = u" ".join(name_parts)
        elif email:
            user["name"] = email.split("@")[0]
        if email:
            user["email"] = email
        if locale:
            user["locale"] = locale
        if username:
            user["username"] = username
        if identity:
            user["identity"] = identity
        return callback(user)

Example 92

Project: pyrdm Source File: figshare.py
Function: init
   def __init__(self, token):
   
      self.base_url = "https://api.figshare.com/v2"
      
      # The Figshare OAuth2 authentication token.
      self.token = token
      
      super(Figshare, self).__init__(self.base_url)

      # Before doing any creating/uploading on Figshare, try something simple like listing the user's articles
      # to check that the authentication is successful.
      _LOG.info("Testing Figshare authentication...")
      try:
         response = self.get('/account/articles', params_dict={"limit":10}, headers=self.get_headers(token=self.token))
         _LOG.debug("Server returned response %d" % response.status_int)
         if(response.status_int != requests.codes.ok): # If the status is not "OK", then exit here.
            raise Exception("Could not authenticate with the Figshare server.")
         else:
            _LOG.info("Authentication test successful.\n")
      except Exception as e:
         _LOG.error("Could not authenticate with the Figshare server. Check Internet connection? Check Figshare authentication keys in ~/.config/pyrdm.ini ?")
         sys.exit(1)

      return

Example 93

Project: svtplay-dl Source File: __init__.py
    def sublanguage(self):
        # parse() function partly borrowed from a guy on github. /thanks!
        # https://github.com/riobard/srt.py/blob/master/srt.py
        def parse(self):
            def parse_block(block):
                lines   = block.strip('-').split('\n')
                txt     = '\r\n'.join(lines[2:])
                return txt
            return list(map(parse_block,
                        open(self).read().strip().replace('\r', '').split('\n\n')))
        
        def query(self):
            random_sentences = ' '.join(sample(parse(self),8)).replace('\r\n', '')
            url = 'https://whatlanguage.herokuapp.com'
            payload = { "query": random_sentences }
            headers = {'content-type': 'application/json'} # Note: requests handles json from version 2.4.2 and onwards so i use json.dumps for now.
            try:
                r = post(url, data=dumps(payload), headers=headers, timeout=30) # Note: reasonable timeout i guess? svtplay-dl is mainly used while multitasking i presume, and it is heroku after all (fast enough)
                if r.status_code == codes.ok:
                    response = r.json()
                    return response['language']
                else:
                    log.error("Server error appeared. Setting language as undetermined.")
                    return 'und'
            except Timeout:
                log.error("30 seconds server timeout reached. Setting language as undetermined.")
                return 'und'

        langs = []
        exceptions = {
            'lulesamiska': 'smj',
            'meankieli': 'fit',
            'jiddisch': 'yid'
        }
        if len(self.subfixes) >= 2:
            log.info("Determining the languages of the subtitles.")
        else: log.info("Determining the language of the subtitle.")
        if self.get_all_subtitles:
            from re import match
            for subfix in self.subfixes:
                if [exceptions[key] for key in exceptions.keys() if match(key, subfix.strip('-'))]:
                    if 'oversattning' in subfix.strip('-'):
                        subfix = subfix.strip('-').split('.')[0]
                    else:
                        subfix = subfix.strip('-')
                    langs += [exceptions[subfix]]
                    continue
                subfile = "{}.srt".format(os.path.splitext(self.stream.options.output)[0] + subfix)
                langs += [query(subfile)]
        else:
            subfile = "{}.srt".format(os.path.splitext(self.stream.options.output)[0])
            langs += [query(subfile)]
        if len(langs) >= 2:
            log.info("Language codes: " + ', '.join(langs))
        else: log.info("Language code: " + langs[0])
        return langs

Example 94

Project: presto-admin Source File: test_collect.py
    @patch("prestoadmin.collect.json.dumps")
    @patch("prestoadmin.collect.requests.models.json")
    @patch("__builtin__.open")
    @patch("prestoadmin.collect.os.makedirs")
    @patch("prestoadmin.collect.requests.get")
    @patch('prestoadmin.collect.request_url')
    def test_collect_query_info(self, requests_url_mock, requests_get_mock,
                                mkdir_mock, open_mock,
                                req_json_mock, json_dumps_mock):
        query_id = "1234_abcd"
        query_info_file_name = path.join(TMP_PRESTO_DEBUG,
                                         "query_info_" + query_id + ".json")
        file_obj = open_mock.return_value.__enter__.return_value
        requests_get_mock.return_value.json.return_value = req_json_mock
        requests_get_mock.return_value.status_code = requests.codes.ok
        env.host = "myhost"
        env.roledefs["coordinator"] = ["myhost"]

        collect.query_info(query_id)

        mkdir_mock.assert_called_with(TMP_PRESTO_DEBUG)

        open_mock.assert_called_with(query_info_file_name, "w")

        json_dumps_mock.assert_called_with(req_json_mock, indent=4)

        file_obj.write.assert_called_with(json_dumps_mock.return_value)

Example 95

Project: anaconda Source File: geoloc.py
    def _refresh(self):
        try:
            reply = self._session.get(self.API_URL, timeout=constants.NETWORK_CONNECTION_TIMEOUT, verify=True)
            if reply.status_code == requests.codes.ok:
                json_reply = reply.json()
                territory = json_reply.get("country_code", None)
                timezone_source = "GeoIP"
                timezone_code = json_reply.get("time_zone", None)

                # check if the timezone returned by the API is valid
                if not is_valid_timezone(timezone_code):
                    # try to get a timezone from the territory code
                    timezone_code = get_preferred_timezone(territory)
                    timezone_source = "territory code"
                if territory or timezone_code:
                    self._set_result(LocationResult(
                        territory_code=territory,
                        timezone=timezone_code,
                        timezone_source=timezone_source))
            else:
                log.error("Geoloc: Fedora GeoIP API lookup failed with status code: %s", reply.status_code)
        except requests.exceptions.RequestException as e:
            log.debug("Geoloc: RequestException for Fedora GeoIP API lookup:\n%s", e)
        except ValueError as e:
            log.debug("Geoloc: Unable to decode GeoIP JSON:\n%s", e)

Example 96

Project: python-indieweb Source File: indieweb.py
Function: handle_login_success
@app.route('/success', methods=['GET',])
def handleLoginSuccess():
    app.logger.info('handleLoginSuccess [%s]' % request.method)
    me   = request.args.get('me')
    code = request.args.get('code')
    app.logger.info('me [%s] code [%s]' % (me, code))

    if db is not None:
        app.logger.info('getting data to validate auth code')
        key  = 'login-%s' % me
        data = db.hgetall(key)
        if data:
            r = ninka.indieauth.validateAuthCode(code=code, 
                                                 client_id=me,
                                                 redirect_uri=data['redirect_uri'])
            if r['status'] == requests.codes.ok:
                app.logger.info('login code verified')
                scope    = r['response']['scope']
                from_uri = data['from_uri']
                token    = str(uuid.uuid4())

                db.hset(key, 'code',  code)
                db.hset(key, 'token', token)
                db.expire(key, cfg['auth_timeout'])
                db.set('token-%s' % token, key)
                db.expire('token-%s' % code, cfg['auth_timeout'])

                session['indieauth_token'] = token
                session['indieauth_scope'] = scope
                session['indieauth_id']    = me
            else:
                app.logger.info('login invalid')
                clearAuth()
        else:
            app.logger.info('nothing found for [%s]' % me)

    if scope:
        if from_uri:
            return redirect(from_uri)
        else:
            return redirect('/')
    else:
        return 'authentication failed', 403

Example 97

Project: anaconda Source File: geoloc.py
Function: refresh
    def _refresh(self):
        try:
            reply = self._session.get(self.API_URL, timeout=constants.NETWORK_CONNECTION_TIMEOUT, verify=True)
            if reply.status_code == requests.codes.ok:
                reply_dict = reply.json()
                territory = reply_dict.get("country_code", None)

                # unless at least country_code is available,
                # we don't return any results
                if territory is not None:
                    self._set_result(LocationResult(
                        territory_code=territory,
                        public_ip_address=reply_dict.get("ip", None),
                        city=reply_dict.get("city", None)
                    ))
            else:
                log.error("Geoloc: Hostip lookup failed with status code: %s", reply.status_code)
        except requests.exceptions.RequestException as e:
            log.debug("Geoloc: RequestException during Hostip lookup:\n%s", e)
        except ValueError as e:
            log.debug("Geoloc: Unable to decode Hostip JSON:\n%s", e)

Example 98

Project: habitica Source File: api.py
Function: call
    def __call__(self, **kwargs):
        method = kwargs.pop('_method', 'get')

        # build up URL... Habitica's api is the *teeniest* bit annoying
        # so either i need to find a cleaner way here, or i should
        # get involved in the API itself and... help it.
        if self.aspect:
            aspect_id = kwargs.pop('_id', None)
            direction = kwargs.pop('_direction', None)
            uri = '%s/%s' % (self.auth['url'],
                             API_URI_BASE)
            if aspect_id is not None:
                uri = '%s/%s/%s' % (uri,
                                    self.aspect,
                                    str(aspect_id))
            elif self.aspect == 'tasks':
                uri = '%s/%s/%s' % (uri,
                                    self.aspect,
                                    self.resource)
            else:
                uri = '%s/%s/%s' % (uri,
                                    self.resource,
                                    self.aspect)
            if direction is not None:
                uri = '%s/score/%s' % (uri, direction)
        else:
            uri = '%s/%s/%s' % (self.auth['url'],
                                API_URI_BASE,
                                self.resource)

        # actually make the request of the API
        if method in ['put', 'post']:
            res = getattr(requests, method)(uri, headers=self.headers,
                                            data=json.dumps(kwargs))
        else:
            res = getattr(requests, method)(uri, headers=self.headers,
                                            params=kwargs)

        # print(res.url)  # debug...
        if res.status_code == requests.codes.ok:
            return res.json()["data"]
        else:
            res.raise_for_status()

Example 99

Project: presto-admin Source File: test_collect.py
Function: test_collect_system_info
    @patch("prestoadmin.collect.make_tarfile")
    @patch('prestoadmin.collect.get_connector_info_from')
    @patch("prestoadmin.collect.json.dumps")
    @patch("prestoadmin.collect.requests.models.json")
    @patch('prestoadmin.collect.execute')
    @patch("__builtin__.open")
    @patch("prestoadmin.collect.os.makedirs")
    @patch("prestoadmin.collect.requests.get")
    @patch('prestoadmin.collect.request_url')
    def test_collect_system_info(self, requests_url_mock, requests_get_mock,
                                 makedirs_mock, open_mock,
                                 execute_mock, req_json_mock,
                                 json_dumps_mock, conn_info_mock,
                                 make_tarfile_mock):
        downloaded_sys_info_loc = path.join(TMP_PRESTO_DEBUG, "sysinfo")
        node_info_file_name = path.join(downloaded_sys_info_loc,
                                        "node_info.json")
        conn_info_file_name = path.join(downloaded_sys_info_loc,
                                        "connector_info.txt")

        file_obj = open_mock.return_value.__enter__.return_value
        requests_get_mock.return_value.json.return_value = req_json_mock
        requests_get_mock.return_value.status_code = requests.codes.ok
        connector_info = conn_info_mock.return_value

        env.host = "myhost"
        env.roledefs["coordinator"] = ["myhost"]
        collect.system_info()

        makedirs_mock.assert_called_with(downloaded_sys_info_loc)
        makedirs_mock.assert_called_with(downloaded_sys_info_loc)

        open_mock.assert_any_call(node_info_file_name, "w")

        json_dumps_mock.assert_called_with(req_json_mock, indent=4)

        file_obj.write.assert_any_call(json_dumps_mock.return_value)

        open_mock.assert_any_call(conn_info_file_name, "w")

        assert conn_info_mock.called

        file_obj.write.assert_any_call(connector_info + '\n')

        execute_mock.assert_called_with(collect.get_system_info,
                                        downloaded_sys_info_loc, roles=[])

        make_tarfile_mock.assert_called_with(OUTPUT_FILENAME_FOR_SYS_INFO,
                                             downloaded_sys_info_loc)

Example 100

Project: anaconda Source File: geoloc.py
    def _reverse_geocode_nominatim(self, coordinates):
        """Reverse geocoding using the Nominatim API
        Reverse geocoding tries to convert geographic coordinates
        to an accurate address.

        :param coordinates: input coordinates
        :type coordinates: Coordinates
        :return: an address or None if no address was found
        :rtype: GeocodingResult or None
        """
        url = "%s&addressdetails=1&lat=%f&lon=%f" % (
            self.NOMINATIM_API_URL,
            coordinates.latitude,
            coordinates.longitude)
        try:
            reply = requests_session().get(url, timeout=constants.NETWORK_CONNECTION_TIMEOUT, verify=True)
            if reply.status_code == requests.codes.ok:
                reply_dict = reply.json()
                territory_code = reply_dict['address']['country_code'].upper()
                return GeocodingResult(coordinates=coordinates,
                                       territory_code=territory_code)
            else:
                log.error("Geoloc: Nominatim reverse geocoding failed with status code: %s", reply.status_code)
                return None
        except requests.exceptions.RequestException as e:
            log.debug("Geoloc: RequestException during Nominatim reverse geocoding:\n%s", e)
        except ValueError as e:
            log.debug("Geoloc: Unable to decode Nominatim reverse geocoding JSON:\n%s", e)
See More Examples - Go to Next Page
Page 1 Page 2 Selected Page 3 Page 4