def __init__(self, api_token=None, proxies=None): self._proxies = proxies # This is not a top-level import because of circular dependencies from neptune import __version__ self.client_lib_version = __version__ self.credentials = Credentials(api_token) ssl_verify = True if os.getenv("NEPTUNE_ALLOW_SELF_SIGNED_CERTIFICATE"): urllib3.disable_warnings() ssl_verify = False self._http_client = RequestsClient(ssl_verify=ssl_verify) # for session re-creation we need to keep an authenticator-free version of http client self._http_client_for_token = RequestsClient(ssl_verify=ssl_verify) user_agent = 'neptune-client/{lib_version} ({system}, python {python_version})'.format( lib_version=self.client_lib_version, system=platform.platform(), python_version=platform.python_version()) self._http_client.session.headers.update({'User-Agent': user_agent}) self._http_client_for_token.session.headers.update( {'User-Agent': user_agent}) update_session_proxies(self._http_client.session, proxies) update_session_proxies(self._http_client_for_token.session, proxies) config_api_url = self.credentials.api_url_opt or self.credentials.token_origin_address # We don't need to be able to resolve Neptune host if we use proxy if proxies is None: self._verify_host_resolution(config_api_url, self.credentials.token_origin_address) # this backend client is used only for initial configuration and session re-creation backend_client = self._get_swagger_client( '{}/api/backend/swagger.json'.format(config_api_url), self._http_client_for_token) self._client_config = self._create_client_config( self.credentials.api_token, backend_client) self._verify_version() self._set_swagger_clients(self._client_config) self.authenticator = self._create_authenticator( self.credentials.api_token, ssl_verify, proxies, backend_client) self._http_client.authenticator = self.authenticator
def __init__(self, access_key: str = None, secret_key: str = None, **kwargs): arg_config = kwargs.get("config") arg_spec_uri = kwargs.get("spec_uri") config = { "also_return_response": False, "validate_responses": False, "use_models": False, "host": HOST } if not arg_config else arg_config spec_uri = SPEC_URI if not arg_spec_uri else arg_spec_uri if access_key and secret_key: request_client = RequestsClient() request_client.authenticator = APIKeyAuthenticator( host=config["host"], access_key=access_key, secret_key=secret_key) self.__client = SwaggerClient.from_url(spec_url=spec_uri, http_client=request_client, config=config) else: self.__client = SwaggerClient.from_url(spec_url=spec_uri, config=config)
def __init__(self, API_KEY, API_SECRET): print("websocket start 1") self.initTradeSide = "Buy" HOST = "https://www.bitmex.com" SPEC_URI = HOST + "/api/explorer/swagger.json" config = { 'use_models': False, 'validate_responses': False, 'also_return_response': True, } bitMEX = SwaggerClient.from_url(SPEC_URI, config=config) self.API_KEY = API_KEY self.API_SECRET = API_SECRET request_client = RequestsClient() print("websocket start 2") request_client.authenticator = APIKeyAuthenticator( HOST, self.API_KEY, self.API_SECRET) self.bitMEXAuthenticated = SwaggerClient.from_url( SPEC_URI, config=config, http_client=request_client) print("websocket end") # Basic authenticated call print('\n---A basic Position GET:---') print( 'The following call requires an API key. If one is not set, it will throw an Unauthorized error.' ) self.avgPrice = 0 self.pos = 0
def get_api_client(config, validation=False): if config.has_key('client'): return config['client'] url = "https://%s/api/v1/spec/openapi/nsx_api.json" % config['nsxManager'][ 'ip'] base64string = base64.encodestring( ('%s:%s' % (config['nsxManager']['username'], config['nsxManager']['password'])).encode("utf-8"))[:-1] headers = {"Authorization": "Basic %s" % base64string.decode("utf-8")} context = ssl.create_default_context() context.check_hostname = False context.verify_mode = ssl.CERT_NONE req = urllib2.Request(url=url, headers=headers) response = urllib2.urlopen(req, context=context) raw_spec = json.loads(response.read()) raw_spec['host'] = config['nsxManager']['ip'] http_client = RequestsClient() http_client.session.verify = False http_client.set_basic_auth(config['nsxManager']['ip'], config['nsxManager']['username'], config['nsxManager']['password']) config = { 'also_return_response': True, 'validate_swagger_spec': validation, 'validate_responses': False, 'validate_requests': False, 'use_models': False } client = SwaggerClient.from_spec(raw_spec, http_client=http_client, config=config) config['client'] = client return client
def bybit(test=True, config=None, api_key=None, api_secret=None): host = TESTNET if test else MAINNET config = { # Don't use models (Python classes) instead of dicts for #/definitions/{models} 'use_models': False, # bravado has some issues with nullable fields 'validate_responses': False, # Returns response in 2-tuple of (body, response); if False, will only return body 'also_return_response': True, 'host': host } if not config else config spec_uri = urljoin(host, "/doc/swagger/v_0_2_10.txt") if api_key and api_secret: request_client = RequestsClient() request_client.authenticator = APIKeyAuthenticator( host, api_key, api_secret) return SwaggerClient.from_url(spec_uri, config=config, http_client=request_client) else: return SwaggerClient.from_url(spec_uri, config=config)
def __init__(self, swagger_spec: Spec): """ :param swagger_spec: Complete swagger specification for the API to test. """ self.host = "localhost" self.swagger_spec = swagger_spec self.base_path = swagger_spec.client_spec_dict.get("basePath", "") self.swagger_spec.spec_dict['host'] = f'{self.host}:{TEST_PORT}' # setting validate requests to false since we only care about # responses config = { "also_return_response": True, "use_models": False, "validate_requests": False, "formats": swagger_spec.config.get("formats", []) } self.config = config # Bravado provides an async client, but it doesnt run on ioloop? from docs: # Fido is a simple, asynchronous HTTP client built on top of Crochet and Twisted with an implementation # inspired by the book "Twisted Network Programming Essentials". It is intended to be used in environments # where there is no event loop, and where you cannot afford to spin up lots of threads (otherwise you # could just use a ThreadPoolExecutor). self.swagger_client = SwaggerClient.from_spec( self.swagger_spec.spec_dict, http_client=RequestsClient(), config=config)
def __init__(self, api_token=None, proxies=None): from neptune import ANONYMOUS, ANONYMOUS_API_TOKEN if api_token == ANONYMOUS: api_token = ANONYMOUS_API_TOKEN self.credentials = Credentials(api_token) ssl_verify = True if os.getenv("NEPTUNE_ALLOW_SELF_SIGNED_CERTIFICATE"): urllib3.disable_warnings() ssl_verify = False self._http_client = RequestsClient(ssl_verify=ssl_verify) update_session_proxies(self._http_client.session, proxies) config_api_url = self.credentials.api_url_opt or self.credentials.token_origin_address self._verify_host_resolution(config_api_url, self.credentials.token_origin_address) backend_client = self._get_swagger_client('{}/api/backend/swagger.json'.format(config_api_url)) self._client_config = self._create_client_config(self.credentials.api_token, backend_client) self._set_swagger_clients(self._client_config, config_api_url, backend_client) self.authenticator = self._create_authenticator(self.credentials.api_token, ssl_verify, proxies) self._http_client.authenticator = self.authenticator # This is not a top-level import because of circular dependencies from neptune import __version__ self.client_lib_version = __version__ user_agent = 'neptune-client/{lib_version} ({system}, python {python_version})'.format( lib_version=self.client_lib_version, system=platform.platform(), python_version=platform.python_version()) self._http_client.session.headers.update({'User-Agent': user_agent})
def __init__(self, service_url: str, token: str) -> None: """Create a Swagger API client, load the Swagger definition from the provided service url, and set the authentication token for the domain name in the url. :param service_url: The base URL for the service, e.g. 'https://mon.hausnet.io/api', without a trailing slash. :param token: The access token provided by the HausNet service. :raises: Any exceptions that were raised during the Swagger client initialization, including connection to the service. """ host = urlparse(service_url).hostname http_client = RequestsClient() http_client.set_api_key(host=host, api_key=f'Token {token}', param_in='header', param_name='Authorization') # noinspection PyBroadException try: self.client = SwaggerClient.from_url(f'{service_url}/swagger.json', http_client=http_client) log.info(f"Connected to Heartbeat client at: url={service_url}") except Exception as e: log.exception( f"Failed to connect to Heartbeat client: url={service_url}") self.client = None raise e
def from_url(cls, spec_url, http_client=None, request_headers=None, config=None): """ Build a :class:`SwaggerClient` from a url to the Swagger specification for a RESTful API. :param spec_url: url pointing at the swagger API specification :type spec_url: str :param http_client: an HTTP client used to perform requests :type http_client: :class:`bravado.http_client.HttpClient` :param request_headers: Headers to pass with http requests :type request_headers: dict :param config: bravado_core config dict. See bravado_core.spec.CONFIG_DEFAULTS """ # TODO: better way to customize the request for api calls, so we don't # have to add new kwargs for everything log.debug(u"Loading from %s" % spec_url) http_client = http_client or RequestsClient() loader = Loader(http_client, request_headers=request_headers) spec_dict = loader.load_spec(spec_url) return cls.from_spec(spec_dict, spec_url, http_client, config)
def setUpClass(cls): # start a test server print('setting UP!!!!!!!!!!') p = subprocess.Popen(['ga4gh_dos_server'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False) time.sleep(2) # print(p.poll(), p.pid) cls._server_process = p http_client = RequestsClient() # http_client.set_basic_auth( # 'localhost', 'admin', 'secret') # http_client.set_api_key( # 'localhost', 'XXX-YYY-ZZZ', param_in='header') local_client = Client(SERVER_URL, http_client=http_client) client = local_client.client models = local_client.models # setup logging root = logging.getLogger() root.setLevel(logging.ERROR) logging.captureWarnings(True) cls._models = models cls._client = client cls._local_client = local_client
def __init__(self, url, user_name=None, password=None): self.url = url self.authenticated = False # This next part pretty much just goes through the motions of what # from_url() does in the parent class. # First let's make an http client self.http_client = RequestsClient() # Now let's get the spec spec_url = urljoin(self.url, '/docs/swagger.json') loader = Loader(self.http_client) spec_dict = loader.load_spec(spec_url) # Now we can create the spec client config = {'include_missing_properties': False} # Apply bravado config defaults bravado_config = bravado_config_from_config_dict(config) # set bravado config object config['bravado'] = bravado_config_from_config_dict(config) swagger_spec = Spec.from_dict(spec_dict, origin_url=spec_url, http_client=self.http_client, config=config) # Now that we have the spec client we can init the parent class super().__init__(swagger_spec, bravado_config.also_return_response) # Go ahead and auth if we were passed creds. if user_name and password: self.authenticate(user_name, password)
def from_url(cls, spec_url, http_client=None, request_headers=None, config=None): """Build a :class:`SwaggerClient` from a url to the Swagger specification for a RESTful API. :param spec_url: url pointing at the swagger API specification :type spec_url: str :param http_client: an HTTP client used to perform requests :type http_client: :class:`bravado.http_client.HttpClient` :param request_headers: Headers to pass with http requests :type request_headers: dict :param config: Config dict for bravado and bravado_core. See CONFIG_DEFAULTS in :module:`bravado_core.spec`. See CONFIG_DEFAULTS in :module:`bravado.client`. :rtype: :class:`bravado_core.spec.Spec` """ log.debug(u"Loading from %s" % spec_url) http_client = http_client or RequestsClient() loader = Loader(http_client, request_headers=request_headers) spec_dict = loader.load_spec(spec_url) # RefResolver may have to download additional json files (remote refs) # via http. Wrap http_client's request() so that request headers are # passed along with the request transparently. Yeah, this is not ideal, # but since RefResolver has new found responsibilities, it is # functional. if request_headers is not None: http_client.request = inject_headers_for_remote_refs( http_client.request, request_headers) return cls.from_spec(spec_dict, spec_url, http_client, config)
def from_spec(cls, spec_dict, origin_url=None, http_client=None, config=None): """ Build a :class:`SwaggerClient` from a Swagger spec in dict form. :param spec_dict: a dict with a Swagger spec in json-like form :param origin_url: the url used to retrieve the spec_dict :type origin_url: str :param config: Configuration dict - see spec.CONFIG_DEFAULTS :rtype: :class:`SwaggerClient` """ http_client = http_client or RequestsClient() config = config or {} # Apply bravado config defaults bravado_config = bravado_config_from_config_dict(config) # remove bravado configs from config dict for key in set(bravado_config._fields).intersection(set(config)): del config[key] # set bravado config object config['bravado'] = bravado_config swagger_spec = Spec.from_dict( spec_dict, origin_url, http_client, config, ) return cls(swagger_spec, also_return_response=bravado_config.also_return_response)
def bitmex(test=True, config=None, api_key=None, api_secret=None): print('test status: %s' % test) if config is None: # See full config options at http://bravado.readthedocs.io/en/latest/configuration.html config = { # Don't use models (Python classes) instead of dicts for #/definitions/{models} 'use_models': False, # bravado has some issues with nullable fields 'validate_responses': False, # Returns response in 2-tuple of (body, response); if False, will only return body 'also_return_response': True, } if test: host = 'https://testnet.bitmex.com' else: host = 'https://www.bitmex.com' spec_uri = host + '/api/explorer/swagger.json' api_key = api_key api_secret = api_secret if api_key and api_secret: request_client = RequestsClient() request_client.authenticator = APIKeyAuthenticator( host, api_key, api_secret) return SwaggerClient.from_url(spec_uri, config=config, http_client=request_client) else: return SwaggerClient.from_url(spec_uri, config=config)
def from_spec(cls, spec_dict, origin_url=None, http_client=None, config=None): """ Build a :class:`SwaggerClient` from a Swagger spec in dict form. :param spec_dict: a dict with a Swagger spec in json-like form :param origin_url: the url used to retrieve the spec_dict :type origin_url: str :param config: Configuration dict - see spec.CONFIG_DEFAULTS :rtype: :class:`bravado_core.spec.Spec` """ http_client = http_client or RequestsClient() # Apply bravado config defaults config = dict(CONFIG_DEFAULTS, **(config or {})) also_return_response = config.pop('also_return_response', False) swagger_spec = Spec.from_dict( spec_dict, origin_url, http_client, config, ) return cls(swagger_spec, also_return_response=also_return_response)
def client_factory(args): """ create a connection """ global _CLIENT if not _CLIENT: config = { 'validate_requests': False, 'validate_responses': False } http_client = RequestsClient() hostname = urlparse(args.dos_server).hostname if args.api_key: http_client.set_api_key(hostname, args.api_key, param_name=args.api_key_name, param_in='header') if args.user_pass: (user, passwd) = args.user_pass.split(':') http_client.set_basic_auth(hostname, user, passwd) local_client = Client(args.dos_server, config=config, http_client=http_client) class C(object): def __init__(self, local_client): self.client = local_client.client self.models = local_client.models _CLIENT = C(local_client) return _CLIENT
def init_client( url, disable_fallback_results=False, validate_responses=False, validate_requests=False, validate_swagger_spec=False, use_models=False, ssl_verify=True, ): """Init client""" # Create a new Requests client instance http_client = RequestsClient() http_client.session.verify = ssl_verify swagger_client = SwaggerClient.from_url( url, http_client=http_client, config={ 'disable_fallback_results': disable_fallback_results, 'validate_responses': validate_responses, 'validate_requests': validate_requests, 'validate_swagger_spec': validate_swagger_spec, 'use_models': use_models, # 'also_return_response': True, # 'force_fallback_result': True }) return swagger_client
def bybit(test=True, config=None, api_key=None, api_secret=None): if test: host = 'https://api-testnet.bybit.com' else: host = 'https://api.bybit.com' if config is None: # See full config options at http://bravado.readthedocs.io/en/latest/configuration.html config = { # Don't use models (Python classes) instead of dicts for #/definitions/{models} 'use_models': False, # bravado has some issues with nullable fields 'validate_responses': False, # Returns response in 2-tuple of (body, response); if False, will only return body 'also_return_response': True, "host": host } api_key = api_key api_secret = api_secret spec_uri = host + "/doc/swagger/v_0_2_6.txt" if api_key and api_secret: request_client = RequestsClient() request_client.authenticator = APIKeyAuthenticator(host, api_key, api_secret) return SwaggerClient.from_url(spec_uri, config=config, http_client=request_client) else: return SwaggerClient.from_url(spec_uri, config=config)
def bitmex(test=True, config=None, api_key=None, api_secret=None): # config options at http://bravado.readthedocs.io/en/latest/configuration.html if not config: config = { # Don't use models (Python classes) instead of dicts for #/definitions/{models} 'use_models': False, # bravado has some issues with nullable fields 'validate_responses': False, # Returns response in 2-tuple of (body, response); if False, will only return body 'also_return_response': True, # 'validate_swagger_spec': True, # 'validate_requests': True, # 'formats': [], } host = 'https://www.bitmex.com' if test: host = 'https://testnet.bitmex.com' spec_uri = host + '/api/explorer/swagger.json' spec_dict = get_swagger_json(spec_uri, exclude_formats=EXCLUDE_SWG_FORMATS) if api_key and api_secret: request_client = RequestsClient() request_client.authenticator = APIKeyAuthenticator(host, api_key, api_secret) return SwaggerClient.from_spec(spec_dict, origin_url=spec_uri, http_client=request_client, config=config) else: return SwaggerClient.from_spec(spec_dict, origin_url=spec_uri, http_client=None, config=config)
def __init__(self, api_address, api_token): self.api_address = api_address self.api_token = api_token self._http_client = RequestsClient() self.backend_swagger_client = SwaggerClient.from_url( '{}/api/backend/swagger.json'.format(self.api_address), config=dict(validate_swagger_spec=False, validate_requests=False, validate_responses=False, formats=[uuid_format]), http_client=self._http_client) self.leaderboard_swagger_client = SwaggerClient.from_url( '{}/api/leaderboard/swagger.json'.format(self.api_address), config=dict(validate_swagger_spec=False, validate_requests=False, validate_responses=False, formats=[uuid_format]), http_client=self._http_client) self.authenticator = NeptuneAuthenticator( self.backend_swagger_client.api.exchangeApiToken( X_Neptune_Api_Token=api_token).response().result) self._http_client.authenticator = self.authenticator
def test_load_trs_client_from_spec(mock_trs_config, monkeypatch): monkeypatch.setattr('wfinterop.trs.client._get_trs_opts', lambda x: mock_trs_config['mock_trs']) mock_http_client = RequestsClient() test_trs_client = load_trs_client(service_id='mock_trs', http_client=mock_http_client) assert isinstance(test_trs_client, ResourceDecorator)
def invoke(op, *args, **kwargs): if op.http_method != 'get': clickclick.action('Invoking..') request = construct_request(op, {}, **kwargs) c = RequestsClient() future = c.request(request) future.result() clickclick.ok()
def get_spec_json(): http_client = RequestsClient() http_client.session.verify = False http_client.session.headers = headers client = SwaggerClient.from_url(hostname + '/apispec/ngfw.json', http_client=http_client, config={'validate_responses': False}) return client
def get_client(token=None): if token == None: return SwaggerClient.from_spec(load_file(swagger_json_file)) # Set up client http_client = RequestsClient() return SwaggerClient.from_spec(load_file(swagger_json_file), http_client=http_client)
def __init__(self, name, context): #swagger_endpoint_url): super(SwaggerEndpoint, self).__init__(name, context) http_client = RequestsClient () self.client = SwaggerClient.from_url( self.url, #swagger_endpoint_url, http_client=http_client, config={ 'use_models': False })
def get_spec_json(host=FDM.get("host"), port=FDM.get("port"), username=FDM.get("username"), password=FDM.get("password"),): http_client = RequestsClient() http_client.session.verify = False http_client.session.headers = headers url = f"https://{host}:{port}/apispec/ngfw.json" client = SwaggerClient.from_url(url, http_client=http_client, config={'validate_responses':False}) return client
def annotate_scarif_video(uuid, start, end, label, assignmentId): """annotates a video in scarif using bravado. args: uuid: scarif video uuid (string) start: start frame (int) end: end frame (int) label: string assignmentId: AMT assignmentId (string) return: response: response of swagger client http://scarif-api.staging.wearkinetic.com/swagger.json side effects: a new entry is placed in the videoAnnotations table in scarif """ # query to check that we've not already uploaded this annotation query = 'SELECT * FROM \"videoAnnotation\" WHERE label ~\'{:s}\''.format( assignmentId) import pg8000 from os import environ as env conn = pg8000.connect(user=env['SCARIF_USER'], host=env['SCARIF_HOST'], database='scarif', password=env['SCARIF_PASS']) cursor = conn.cursor() cursor.execute(query) response = cursor.fetchone() cursor.close() conn.close() if response != None: print('annotation is already present', file=sys.stderr) return response[0] #EXAMPLE CURL COMMAND #curl -X POST "http://scarif-api.wearkinetic.com/v1/annotate/video" -H "accept: application/json" -H "api_key: asdasd" -H "Content-Type: application/json" -d "{ \"end_time\": 0, \"label\": \"string\", \"start_time\": 1, \"target_uuid\": \"b607a3c1-87d1-417d-afa5-972ab2ce694f\"}" from bravado.requests_client import RequestsClient from bravado.client import SwaggerClient import json http_client = RequestsClient() http_client.set_api_key('scarif-api.wearkinetic.com', 'LITERALLY ANY STRING', param_name='api_key', param_in='header') client = SwaggerClient.from_url( 'http://scarif-api.wearkinetic.com/swagger.json', http_client=http_client) label_aug = json.dumps({'label': label, 'assignmentId': assignmentId}) ann = client.get_model('Annotation')(label=label_aug, start_time=start, end_time=end, target_uuid=uuid) return client.annotations.annotate(type='video', body=ann).result()
def get_request_client(): global _request_client if not _request_client: rc = RequestsClient() rc.authenticator = APIKeyAuthenticator( HOST, config.settings.bitmex_api_key, config.settings.bitmex_secret_key) _request_client = SwaggerClient.from_url(SPEC_URI, config=api_config, http_client=rc) return _request_client
def get_client(auth_token=AUTH_TOKEN): with open(os.getenv("SWAGGER_SCHEMA", "swagger.yml")) as f: spec = yaml.load(f) http_client = RequestsClient() http_client.set_api_key( HOSTNAME, auth_token, param_name='X-API-Key', param_in='header' ) _client = SwaggerClient.from_spec(spec, http_client=http_client) _client.swagger_spec.api_url = HOST return _client
def __init__(self, url, jwt=None, config=DEFAULT_CONFIG): swagger_path = "{url}/swagger.json".format(url=url.rstrip("/")) if jwt: http_client = RequestsClient() http_client.set_api_key(host=urlparse(url).netloc, api_key=f"Bearer {jwt}", param_name="Authorization", param_in="header") else: http_client = None self.models = SwaggerClient.from_url(swagger_path, config=config) self.client = self.models.TaskService