GlInet API
- class pyglinet.GlInet(url: str = 'https://192.168.8.1/rpc', username: str = 'root', password: Optional[str] = None, protocol_version: str = '2.0', keep_alive: bool = True, keep_alive_intervall: float = 30, verify_ssl_certificate: bool = False, update_api_reference_cache: bool = False, api_reference_url: str = 'https://dev.gl-inet.cn/docs/api_docs_api/', cache_folder: Optional[str] = None)
This class manages the connection to a GL-Inet router and provides basic routines to send and receive data.
Important: Only works with firmware version >=4.0. The api has changed from REST api to json-rpc with the 4.0, so older firmware versions won’t work.
Before you can start making requests, you need to call the
login()
methodThe api calls can either be made via the GlInetApi object, which can be constructed via the get_api_client method, or via the request method directly.
- Parameters
url – url to router rpc api
username – username, default is root.
password – password, if left empty, a prompt will ask you when login() is called. For security reasons, you should never pass your password here.
protocol_version – default 2.0
keep_alive – if set to True, a background thread will be started to keep the connection alive
keep_alive_intervall – intervall in which the background thread sends requests to the router
verify_ssl_certificate – either True/False or path to certificate.
update_api_reference_cache – if True, data is loaded from the web, otherwise application tries first to load data from cache.
api_reference_url – url to api description
cache_folder – folder where data is persisted. If left empty, default is $home/.python-pyglinet
- __challenge_login()
Request cryptographic parameters to compute login hash. This is the first step in the login sequence.
- Returns
challence
- __create_object(json_data, method, params)
Create recursive object from json api response
Json data is stored in a convenience container, such that elements can be accessed as class attributes via ‘.’
- Parameters
json_data – json data
method – api method call
params – params
- Returns
ResultContainer
- __dump_to_file(obj, file)
Dump pickle data to file.
- Parameters
obj – object to dump
file – path to file
- Returns
None
- __generate_login_hash(challenge)
Generate final authentication hash
- Parameters
challenge – dict with nonce, salt and algo type
- Returns
authentication hash
- __generate_query_id() int
Generate json-rpc query id
- Returns
query id
- __generate_request(method: str, params: Union[Dict, List[str], str]) dict
Generate json for rpc api call
- Parameters
method – rpc method
params – params
- Returns
json
- __generate_unix_passwd_hash(password: str, alg: str, salt: str) str
Generate unix style hash with given algo and salt
- Parameters
alg – algorithm
salt – salt
- Returns
hash
- __keep_alive() None
Keep connection alive
Function is started in background thread (see login() for more details). Send in fixed intervall requests to api. If not successful, try to connect again.
- Returns
None
- __load_api_description(update: bool = False)
Load api description in json format
- Parameters
update – if true, the api description is loaded from the web. If false, the program first tries to load the data from the cache and in case this fails from the web.
- Returns
api description
- __load_if_exist(file: str)
Load pickle file if it exists.
- Parameters
file – path to file
- Returns
None if file doesn’t exist, else Data
- __request(method: str, params: Union[Dict, List[str], str]) ResultContainer
Send request to router without considering the current login state. This may lead to misleading error messages.
- Parameters
method – rpc method
params – parameter
- Returns
ResultContainer
- __request_with_sid(method: str, params: Union[Dict, List[str], str]) ResultContainer
Request which requires prior login
- Parameters
method – api method call
params – params
- Returns
ResultContainer
- __request_without_sid(method: str, params: Union[Dict, List[str], str]) ResultContainer
Request which requires to be logged out
- Parameters
method – api method call
params – params
- Returns
ResultContainer
- __update_login_and_cache(challenge, update_password=False)
Generates the login struct containing username, hash, salt and alg type. If data is diverging from persisted set, old data will be deleted and new data will be persisted to file.
- Parameters
challenge – challenge as received containing nonce, salt and algo
update_password – if True, the user will be requested to enter the password
- _start_keep_alive_thread()
Starts keep alive background thread which calls
__keep_alive()
in the configured interval.- Returns
- _stop_keep_alive_thread()
Stop keep alive thread
- get_api_client() GlInetApi
Create GlInetApi object client to access api functions
- Returns
api client
- is_alive() bool
Check if connection is alive.
- Returns
True if alive, else False
- login()
Login and start background thread for keep_alive is configured. If password was set in constructor, cached values will be ignored. If password was not set (default) in
GlInet()
, you will be asked to enter the password the first time this function is called. If login was successful, the password hash is cashed.- Returns
True
- logout() bool
Logout and stop keep alive thread
- Returns
True
- request(method: str, params: Union[Dict, List[str], str]) ResultContainer
Send request. Function checks if method requires login and chooses the respective request wrapper. see
__request_with_sid()
and__request()
- Parameters
method – api method call
params – params
- Returns
ResultContainer