def __init__(self, path, method, body_as_dict): assert not path.startswith('/') url = "%s/%s" % (database, path) headers = { "Accept": "application/json", "Accept-Encoding": "charset=utf8", } if body_as_dict is not None: if tampering_signer: tamper.sign(tampering_signer, body_as_dict) body = json.dumps(body_as_dict) headers["Content-Type"] = "application/json; charset=utf8" else: body = None auth_mode = "basic" if username or password else None tornado.httpclient.HTTPRequest.__init__( self, url, method=method, body=body, headers=tornado.httputil.HTTPHeaders(headers), validate_cert=validate_cert, auth_mode=auth_mode, auth_username=username, auth_password=password)
def _create_seed_docs(database, host, session, verify_host_ssl_cert, seed_docs_folder, seed_doc_signer_dir_name): # # iterate thru each file in the seed doc module's directory # for files that end with ".py" - these files are assumed to be # JSON documents. The filename is ignored. # _logger.info("Creating seed documents in database '%s' on '%s'", database, host) seed_doc_signer = None if seed_doc_signer_dir_name: try: seed_doc_signer = keyczar.Signer.Read(seed_doc_signer_dir_name) except: _logger.error("Error creating seed doc signer from '%s'", seed_doc_signer_dir_name) return False seed_doc_filename_pattern = os.path.join(seed_docs_folder, "*.json") for seed_doc_filename in glob.glob(seed_doc_filename_pattern): _logger.info( "Creating seed doc in database '%s' on '%s' from file '%s'", database, host, seed_doc_filename) with open(seed_doc_filename, "r") as seed_doc_file: seed_doc = seed_doc_file.read() try: json.loads(seed_doc) except Exception as ex: _logger.error( "Failed to create seed doc from '%s' - invalid JSON '%s'", seed_doc_filename, ex) return False if seed_doc_signer is not None: seed_doc = json.loads(seed_doc) tamper.sign(seed_doc_signer, seed_doc) seed_doc = json.dumps(seed_doc) url = "%s/%s" % (host, database) response = session.post( url, data=seed_doc, headers={"Content-Type": "application/json; charset=utf8"}, verify=verify_host_ssl_cert) if response.status_code != httplib.CREATED: _logger.error("Failed to create seed doc from '%s'", seed_doc_filename) return False _logger.info("Successfully created seed doc '%s' from '%s'", response.headers["location"], seed_doc_filename) return True
def fetch(self, callback): """fetch() is perhaps not the best name but it matches the key method in the async HTTP client classs:-). """ assert self._callback is None self._callback = callback url = "%s/%s" % (database, self.path) headers = { "Accept": "application/json", "Accept-Encoding": "charset=utf8", } if self.body_as_dict is not None: if tampering_signer is not None: tamper.sign(tampering_signer, self.body_as_dict) body = json.dumps(self.body_as_dict) headers["Content-Type"] = "application/json; charset=utf8" else: body = None auth_mode = "basic" if username or password else None request = tornado.httpclient.HTTPRequest( url, method=self.method, body=body, headers=tornado.httputil.HTTPHeaders(headers), validate_cert=validate_cert, auth_mode=auth_mode, auth_username=username, auth_password=password) http_client = tornado.httpclient.AsyncHTTPClient() http_client.fetch( request, callback=self._on_http_client_fetch_done)
def _create_seed_docs(database, host, session, verify_host_ssl_cert, seed_docs_module, seed_doc_signer_dir_name): # # iterate thru each file in the seed doc module's directory # for files that end with ".py" - these files are assumed to be # JSON documents. The filename is ignored. # _logger.info( "Creating seed documents in database '%s' on '%s'", database, host) seed_doc_signer = None if seed_doc_signer_dir_name: try: seed_doc_signer = keyczar.Signer.Read(seed_doc_signer_dir_name) except: _logger.error( "Error creating seed doc signer from '%s'", seed_doc_signer_dir_name) return False path = os.path.split(seed_docs_module.__file__)[0] seed_doc_filename_pattern = os.path.join(path, "*.py") for seed_doc_filename in glob.glob(seed_doc_filename_pattern): if seed_doc_filename.endswith("__init__.py"): continue _logger.info( "Creating seed doc in database '%s' on '%s' from file '%s'", database, host, seed_doc_filename) with open(seed_doc_filename, "r") as seed_doc_file: seed_doc = seed_doc_file.read() try: json.loads(seed_doc) except Exception as ex: _logger.error( "Failed to create seed doc from '%s' - invalid JSON '%s'", seed_doc_filename, ex) return False if seed_doc_signer is not None: seed_doc = json.loads(seed_doc) tamper.sign(seed_doc_signer, seed_doc) seed_doc = json.dumps(seed_doc) url = "%s/%s" % (host, database) response = session.post( url, data=seed_doc, headers={"Content-Type": "application/json; charset=utf8"}, verify=verify_host_ssl_cert) if response.status_code != httplib.CREATED: _logger.error("Failed to create seed doc from '%s'", seed_doc_filename) return False _logger.info( "Successfully created seed doc '%s' from '%s'", response.headers["location"], seed_doc_filename) return True