def make_request(method, host, url, username, password, fields=None): if host.lower().startswith('https://'): connection = urllib3.connection_from_url(host, cert_reqs=ssl.CERT_REQUIRED, ca_certs=certs_file()) else: connection = urllib3.connection_from_url(host) headers = urllib3.util.make_headers(basic_auth='{0}:{1}'.format( username, password), accept_encoding=True, user_agent=user_agent_identifier(), keep_alive=True) r = None try: r = connection.request(method, url, headers=headers, fields=fields) data = r.data charset = determine_charset(r) if isinstance(data, bytes): data = data.decode(charset) if r.status < 200 or r.status >= 400: if r.status == 404: raise HttpNotFound(data) else: raise Exception(data) return data, charset except SSLError: logger.error("Invalid SSL certificate") raise finally: if not r is None: r.close()
def _init(self, path_to_tx=None): instructions = "Run 'tx init' to initialize your project first!" try: self.root = self._get_tx_dir_path(path_to_tx) self.config_file = self._get_config_file_path(self.root) self.config = self._read_config_file(self.config_file) local_txrc_file = self._get_transifex_file(os.getcwd()) if os.path.exists(local_txrc_file): self.txrc_file = local_txrc_file else: self.txrc_file = self._get_transifex_file() self.txrc = self._get_transifex_config([self.txrc_file, ]) except ProjectNotInit as e: logger.error('\n'.join([six.u(str(e)), instructions])) raise host = self.config.get('main', 'host') if host.lower().startswith('https://'): self.conn = urllib3.connection_from_url( host, cert_reqs=ssl.CERT_REQUIRED, ca_certs=certs_file() ) else: self.conn = urllib3.connection_from_url(host)
def make_request(method, host, url, username, password, fields=None): if host.lower().startswith('https://'): connection = urllib3.connection_from_url( host, cert_reqs=ssl.CERT_REQUIRED, ca_certs=certs_file() ) else: connection = urllib3.connection_from_url(host) headers = urllib3.util.make_headers( basic_auth='{0}:{1}'.format(username, password), accept_encoding=True, user_agent=user_agent_identifier(), keep_alive=True ) r = None try: r = connection.request(method, url, headers=headers, fields=fields) data = r.data charset = determine_charset(r) if isinstance(data, bytes): data = data.decode(charset) if r.status < 200 or r.status >= 400: if r.status == 404: raise HttpNotFound(data) else: raise Exception(data) return data, charset except SSLError: logger.error("Invalid SSL certificate") raise finally: if not r is None: r.close()
def get_details(api_call, username, password, *args, **kwargs): """ Get the tx project info through the API. This function can also be used to check the existence of a project. """ url = (API_URLS[api_call] % (kwargs)).encode('UTF-8') conn = urllib3.connection_from_url(kwargs['hostname']) headers = urllib3.util.make_headers( basic_auth='{0}:{1}'.format(username, password), accept_encoding=True, user_agent=user_agent_identifier(), ) try: r = conn.request('GET', url, headers=headers) if r.status < 200 or r.status >= 400: raise Exception(r.data) remote_project = parse_json(r.data) return remote_project except ssl.SSLError: logger.error("Invalid SSL certificate") raise except Exception, e: logger.debug(unicode(e)) raise
def _init(self, path_to_tx=None): instructions = "Run 'tx init' to initialize your project first!" try: self.root = self._get_tx_dir_path(path_to_tx) self.config_file = self._get_config_file_path(self.root) self.config = self._read_config_file(self.config_file) self.txrc_file = self._get_transifex_file() local_txrc_file = self._get_transifex_file(os.getcwd()) self.txrc = self._get_transifex_config( [self.txrc_file, local_txrc_file]) if os.path.exists(local_txrc_file): self.txrc_file = local_txrc_file except ProjectNotInit as e: logger.error('\n'.join([six.u(str(e)), instructions])) raise host = self.config.get('main', 'host') if host.lower().startswith('https://'): self.conn = urllib3.connection_from_url( host, cert_reqs=ssl.CERT_REQUIRED, ca_certs=certs_file()) else: self.conn = urllib3.connection_from_url(host)