def get_parameters(self): """Returns the current parameters as dict of str.""" p = POINTER(clib._KeyValPair)() n = c_size_t() clib.call(self._abi("GetParameters"), _REF(self._obj), _REF(p), _REF(n)) kv = {p[i].key.decode():p[i].val.decode() for i in range(n.value)} clib.free_keyval_buffer(p, n) return kv return { kv.key:kv.val for kv in kvpairs }
def set_qos(self, qos): """Sets/changes the quality-of-service. def set_qos(self, qos): qos :: int :: QOS_[] constant indicating quality-of-service returns -> bool indicating success / failure throws -> LibraryNotLoaded, CLibException """ r = c_int() clib.call(self._abi("SetQOS"), _REF(self._obj), c_int(qos), _REF(r)) return bool(r)
def __init__(self, creds, callback, connect_timeout=DEF_CONNECT_TIMEOUT, listening_timeout=DEF_LISTENING_TIMEOUT, subscribe_timeout=DEF_SUBSCRIBE_TIMEOUT): self._creds = creds self._cb_raw = callback self._cb_wrapper = self._build_callback_wrapper(callback) self._obj = _StreamingSession_C() clib.call(self._abi('Create'), _REF(creds), self._cb_wrapper, c_ulong(connect_timeout), c_ulong(listening_timeout), c_ulong(subscribe_timeout), _REF(self._obj)) self._alive = True
def request_access_token(code, client_id, redirect_uri="https://127.0.0.1"): """Returns 'Credentials' intance for accessing API. Uses an access code, and the fields set during developer/app registration, to request access and refresh tokens. The tokens are used to create a 'Credentials' instance which allows access to the API until the refresh token expires in 90 days. https://github.com/jeog/TDAmeritradeAPI#manual-approach def request_access_token(code, client_id, redirect_uri="https://127.0.0.1"): code :: str :: access code retrieve from TDAmeritrade client_id :: str :: unique id from setting up developer account redirect_uri :: str :: uri used in getting access code returns -> Credentials object throws -> LibraryNotLoaded, CLibException """ creds = Credentials() clib.call('RequestAccessToken_ABI', PCHAR(code), PCHAR(client_id), PCHAR(redirect_uri), _REF(creds)) return creds
def request_access_token(code, client_id, redirect_uri="https://127.0.0.1"): """Get a token to access the TDAmeritrade API. After setting up a developer account(developer.tdameritrade.com/content/ getting-started) retrieve an access code by: 1) using your browser and a localhost redirect host uri (default) (developer.tdameritrade.com/content/simple-auth-local-apps) -or- 2) using your own server (change redirect_uri) (developer.tdameritrade.com/content/ web-server-authentication-python-3) -or- 3) using a 3rd party OAuth service (change redirect uri) def request_access_token(code, client_id, redirect_uri="https://127.0.0.1"): code :: str :: access code retrieve from TDAmeritrade client_id :: str :: unique id from setting up developer account redirect_uri :: str :: uri used in getting access code returns -> Credentials object throws -> LibraryNotLoaded, CLibException """ creds = Credentials() clib.call('RequestAccessToken_ABI', PCHAR(code), PCHAR(client_id), PCHAR(redirect_uri), _REF(creds)) return creds
def _subscription_abi_call(self, fname, subscriptions): self._check_subs(subscriptions) l = len(subscriptions) subs = (POINTER(_StreamingSubscription_C) * l)\ (*[pointer(s._obj) for s in subscriptions]) results = (c_int * l)(*([0] * l)) clib.call(self._abi(fname), _REF(self._obj), subs, l, results) return [bool(r) for r in results]
def Create(cls, access_token, refresh_token, epoch_sec_token_expiration, client_id): """Create a new Credential instance.""" creds = Credentials() clib.call('CreateCredentials_ABI', PCHAR(access_token), PCHAR(refresh_token), c_ulonglong(epoch_sec_token_expiration), PCHAR(client_id), _REF(creds)) return creds
def __del__(self): if hasattr(self, 'access_token') and self.access_token: try: try: clib.call("CloseCredentials_ABI", _REF(self)) except clib.CLibException as e: print("CLibException in", self.__del__, ":", str(e)) except: pass
def set_parameters(self, parameters): """Sets new parameters from a dict of str:str. e.g .set_parameters( {"keys":"GOOG,AAPL", "fields":"0,1,2"} ) """ KV = clib._KeyValPair kv = [KV(PCHAR(k), PCHAR(v)) for k, v in parameters.items()] kvpairs = (KV * len(kv))(*kv) clib.call(self._abi("SetParameters"), _REF(self._obj), kvpairs, len(kv))
def __del__(self): if hasattr(self, '_alive') and self._alive: self._alive = False try: try: clib.call(self._abi('Destroy'), _REF(self._obj)) except clib.CLibException as e: print("CLibException in", self.__del__, ":", str(e)) except: pass
def refresh_access_token(creds): """Refresh an expired access token. THIS IS DONE AUTOMATICALLY. def refresh_access_token(creds): creds :: Credentials :: credentials object with token to refresh returns -> None throws -> LibraryNotLoaded, CLibException """ clib.call('RefreshAccessToken_ABI', _REF(creds))
def __init__(self, creds, callback, connect_timeout=DEF_CONNECT_TIMEOUT, listening_timeout=DEF_LISTENING_TIMEOUT, subscribe_timeout=DEF_SUBSCRIBE_TIMEOUT): self._creds = creds self._cb_raw = callback self._cb_wrapper = self._build_callback_wrapper(callback) super().__init__(_REF(creds), self._cb_wrapper, c_ulong(connect_timeout), c_ulong(listening_timeout), c_ulong(subscribe_timeout))
def store_credentials(path, password, creds): """Store a credentials object to an encrypted credentials file. def store_credentials(path, password, creds): path :: str :: path of credentials file password :: str :: password for decrypting credentials file creds :: Credentials :: credentials object to store returns -> None throws -> LibraryNotLoaded, CLibException """ clib.call('StoreCredentials_ABI', PCHAR(path), PCHAR(password), _REF(creds))
def load_credentials(path, password): """Load a credentials object from an encrypted credentials file. def load_credentials(path, password): path :: str :: path of credentials file password :: str :: password for decrypting credentials file returns -> Credentials object throws -> LibraryNotLoaded, CLibException """ creds = Credentials() clib.call('LoadCredentials_ABI', PCHAR(path), PCHAR(password), _REF(creds)) return creds
def stop(self): """Stop the session, logout, and clear subscriptions.""" clib.call(self._abi("Stop"), _REF(self._obj))
def set_fields(self, fields): """Sets fields in subscription using self.FIELD_[] constants.""" l = len(fields) array = (c_int * l)(*fields) clib.call(self._abi("SetFields"), _REF(self._obj), array, l)
def set_fields(self, fields): """Sets fields in subscription using FIELD_[] constants.""" l = len(fields) array = (c_int * l)(*fields) clib.call("TimesaleSubscriptionBase_SetFields_ABI", _REF(self._obj), array, l)
def __init__(self, *args): self._obj = _StreamingSubscription_C() args = args + (_REF(self._obj), ) clib.call(self._abi('Create'), *args) self._alive = True