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 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 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 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): 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 __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_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_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_clients(): HOST = "https://www.bitmex.com" SPEC_URI = HOST + "/api/explorer/swagger.json" bitMEX = SwaggerClient.from_url(SPEC_URI, config=config) API_KEY = 'emjo2LdiVdhwmTqMESEcO9ut' API_SECRET = 'DspbFr4sWjnxUPY4L5yDh13b0MZ1oDGs4kr94EcdJcSH2QkR' request_client = RequestsClient() request_client.authenticator = APIKeyAuthenticator(HOST, API_KEY, API_SECRET) bitMEXAuthenticated = SwaggerClient.from_url(SPEC_URI, config=config, http_client=request_client) return bitMEX, bitMEXAuthenticated
def initialise_API(self, HOST, API_KEY, API_SECRET): SPEC_URI = HOST + "/api/explorer/swagger.json" config = { # Don't use models (Python classes) instead of dicts for #/definitions/{models} 'use_models': False, # This library 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, } # See full config options at http://bravado.readthedocs.io/en/latest/configuration.html request_client = RequestsClient() request_client.authenticator = APIKeyAuthenticator( HOST, API_KEY, API_SECRET) self.bitMEXAuth = SwaggerClient.from_url(SPEC_URI, config=config, http_client=request_client) time.sleep(2)
def __init__(self, api_address, api_token): self.api_address = api_address self.api_token = api_token http_client = RequestsClient() self.backend_swagger_client = SwaggerClient.from_url( '{}/api/backend/swagger.json'.format(self.api_address), config=dict(validate_swagger_spec=False, formats=[uuid_format]), http_client=http_client) self.leaderboard_swagger_client = SwaggerClient.from_url( '{}/api/leaderboard/swagger.json'.format(self.api_address), config=dict( validate_swagger_spec=False, validate_responses=False, # TODO!!! formats=[uuid_format]), http_client=http_client) http_client.authenticator = NeptuneAuthenticator( self.backend_swagger_client.api.exchangeApiToken( X_Neptune_Api_Token=api_token).response().result)
def create_client(origin_url=None, config=None, api_url=None, authenticator=None): """ Create the Bravado swagger client from the specified origin url and config. For the moment, the Swagger specification for Dart is actually bundled with this client since Dart does not have an endpoint that exposes it dynamically. :param origin_url: The location of the Swagger specification. If not specified, then api_url must be specified will assume that swagger.json is present at that location. :param config: An optional configuration dictionary to pass to the Bravado SwaggerClient. :param api_url: The base URL for the API endpoints. :param authenticator: An authenticator instance to use when making API requests :return: The Bravado SwaggerClient instance. """ if origin_url: spec_url = origin_url elif api_url: spec_url = api_url + '/swagger.json' else: raise RuntimeError('One of origin_url or api_url must be specified') http_client = RequestsClient() http_client.authenticator = authenticator client = SwaggerClient.from_url(spec_url=spec_url, config=config, http_client=http_client) if api_url: client.swagger_spec.api_url = api_url return client
print('\n---A basic Trade GET:---') pp.pprint(res) print('\n---Response details:---') print("Status Code: %d, headers: %s" % (http_response.status_code, http_response.headers)) # # Authenticated calls # # To do authentication, you must generate an API key. # Do so at https://testnet.bitmex.com/app/apiKeys API_KEY = api_keys.bmx_api_key API_SECRET = api_keys.bmx_api_secret request_client = RequestsClient() request_client.authenticator = APIKeyAuthenticator(HOST, API_KEY, API_SECRET) bitMEXAuthenticated = SwaggerClient.from_url( SPEC_URI, config=config, http_client=request_client) print(dir(bitMEXAuthenticated)) print(dir(bitMEXAuthenticated.Position)) # Basic authenticated call print('\n---A basic Position then funding then user deposit addr GET:---') res, http_response = bitMEXAuthenticated.Position.Position_get().result() pp.pprint(res) pp.pprint(http_response.status_code) res, http_response = bitMEXAuthenticated.Funding.Funding_get(symbol='XBTUSD', reverse=True, count=1).result() pp.pprint(res)
pp.pprint(res) print('\n---Response details:---') print("Status Code: %d, headers: %s" % (http_response.status_code, http_response.headers)) # # Authenticated calls # # To do authentication, you must generate an API key. # Do so at https://testnet.bitmex.com/app/apiKeys API_KEY = '<API_KEY_HERE>' API_SECRET = '<API_SECRET_HERE>' request_client = RequestsClient() request_client.authenticator = APIKeyAuthenticator(HOST, API_KEY, API_SECRET) bitMEXAuthenticated = SwaggerClient.from_url( SPEC_URI, config=config, http_client=request_client) # 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.') res, http_response = bitMEXAuthenticated.Position.Position_get(filter=json.dumps({'symbol': 'XBTUSD'})).result() pp.pprint(res) # Basic order placement # print(dir(bitMEXAuthenticated.Order))
def nge(host="http://trade", config=None, api_key=None, api_secret=None): """ :rtype: SwaggerClient """ if not config: # 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, 'validate_requests': True, # bravado has some issues with nullable fields 'validate_responses': False, 'include_missing_properties': False, # Returns response in 2-tuple of (body, response); # if False, will only return body 'also_return_response': True, 'formats': [ SwaggerFormat(format="guid", to_wire=lambda guid_obj: str(guid_obj), to_python=guid_deserializer, description="GUID to uuid", validate=guid_validate), SwaggerFormat(format="date-time", to_wire=datetime_serializer, to_python=datetime_deserializer, description="date-time", validate=datetime_validate) ] } spec_dir = path("@/swagger") spec_name = ("nge", "bitmex") spec_extension = ("yaml", "yml", "json") load_method = { "yaml": yaml.safe_load, "yml": yaml.safe_load, "json": json.dumps } with pushd(spec_dir): spec_file = "" for name, ext in product(spec_name, spec_extension): spec_file = ".".join([name, ext]) if os.path.isfile(spec_file): break if not spec_file: raise RuntimeError("no valid swagger api define file found.") with open(spec_file, encoding="utf-8") as f: spec_dict = load_method[ext](f.read()) if api_key and api_secret: request_client = RequestsClient() request_client.authenticator = NGEAPIKeyAuthenticator( host=host, api_key=api_key, api_secret=api_secret) return SwaggerClient.from_spec(spec_dict, origin_url=host, config=config, http_client=request_client) else: return SwaggerClient.from_spec(spec_dict, origin_url=host, config=config)