├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── atxserver2 └── __init__.py ├── requirements.txt ├── setup.cfg ├── setup.py └── tests └── test_main.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - '3.6' 4 | - pypy 5 | script: echo skip test 6 | deploy: 7 | skip_existing: true 8 | provider: pypi 9 | user: codeskyblue 10 | password: 11 | secure: A14Qm25FP6+r/GcjcQeTRkmt/7T2CogtKIhW0I2eaKTGvc9dxaeaZE13KoiKQ94kgI+pTHXJ0xvwf3ylEOMFxyi2Fd8m79sflm4AivhNhVkZPbV3Aeo5rIkTJNVN2L2k9atRf706l9xZ1E8dh7EcaUUH5aI38xU4JcqWvs4sQr34TDOjOBvX5QZID97fP6cNS9rzQf3I63/+8EyYG516CtCCSpB9b4d3eV77aSE7gf77h4bbJuXlWG0NZethq5iqPetP0ESM6+SFKOTwJGTSNaLHkdEdpIEyNa7+MdhjiNnu1VfDCcZeFO9fSv5o1JtuPqaSPRIoq4DH49tUSkvg6m0hy+GVgjCk1345JXH0hbGG1Spo0MoUd9m0yimRH1am6Ot5EwB/JGl2HX46E3V0CiMkBU+y7RnbVnDHLuIe9HyazAAWA6H1cJXprl4+iMOe4eRfynBTufJdYiFoUJsiY3rNBzGcFMiKAalOK/krE4mIq+6oOJor6/TmEpJSIHrU7q0XR0N09iSpqOsZQf2qoMaRpCrxPaXNayzAf5cA4rYEddq3YcR1B5uvlf3G7kG8sX4LrLLOYoGliL7hoT2846yIhvsLeVlLaYfZnjYdeOuLunuthLLiskLC1uMxy4ydp9eQPxam5rumqiuKhbRWY1BcxoBgOO2HLPa3WCSWITQ= 12 | on: 13 | tags: true 14 | distributions: sdist bdist_wheel 15 | repo: openatx/atxserver2-python-client 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 openatx 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # atxserver2 python client 2 | Make it easy to use atxserver2 3 | 4 | ## Requirements 5 | - Python 3.6+ 6 | 7 | ## Install 8 | ``` 9 | pip install --upgrade atxserver2-python-client 10 | ``` 11 | 12 | ## Usage 13 | ```python 14 | import atxserver2 15 | 16 | client = atxserver2.Client("http://localhost:4000", "xxxx-your-token-here-xxxxx") 17 | for device in client.list_device(): 18 | print("Device", device) 19 | device.acquire() # 占用设备 20 | device.acquire(email="tt@example.com", idle_timeout=6000) # 指定用户占用设备, 时长6000秒 21 | device.release() # 释放设备 22 | 23 | print(device.atx_agent_address) # 获取设备信息 24 | -------------------------------------------------------------------------------- /atxserver2/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | # 3 | 4 | import requests 5 | import json 6 | 7 | 8 | class Error(Exception): 9 | """ basic error """ 10 | 11 | 12 | class Client(): 13 | def __init__(self, server_url: str, token: str): 14 | self._server_url = server_url 15 | self._token = token 16 | 17 | def _request(self, uri: str, method='get', **kwargs): 18 | """ 19 | Default set timeout to 10 20 | """ 21 | kwargs['headers'] = {"Authorization": "Bearer " + self._token} 22 | if 'timeout' not in kwargs: 23 | kwargs['timeout'] = 10 24 | url = self._server_url.rstrip("/") + "/" + uri.lstrip("/") 25 | r = requests.request(method, url, **kwargs) 26 | r.raise_for_status() 27 | try: 28 | return r.json() 29 | except json.decoder.JSONDecodeError: 30 | raise RuntimeError(r.text) 31 | 32 | def user_info(self) -> dict: 33 | """ 34 | Returns: 35 | { 36 | "username": "..xxx..", 37 | } 38 | """ 39 | return self._request("/api/v1/user") 40 | 41 | def list_device(self, platform=None, present:bool=True, usable:bool=None) -> list: 42 | """ 43 | Returns: 44 | list 45 | """ 46 | params = {} 47 | if usable: 48 | params["usable"] = str(usable).lower() 49 | if present: 50 | params["present"] = str(present).lower() 51 | 52 | data = self._request("/api/v1/devices", params=params) 53 | devices = [] 54 | for info in data['devices']: 55 | udid = info['udid'] 56 | d = Device(self, udid, info) 57 | devices.append(d) 58 | return devices 59 | 60 | 61 | class Device(): 62 | def __init__(self, client: Client, udid: str, info: dict): 63 | self._client = client 64 | self._udid = udid 65 | self._info = info 66 | 67 | def acquire(self, idle_timeout: float = 6000.0, email: str = None): 68 | """ 69 | Args: 70 | idle_timeout: device maxium using seconds 71 | """ 72 | try: 73 | ret = self._client._request("/api/v1/user/devices", 74 | method="post", 75 | json={"udid": self._udid, "idleTimeout": idle_timeout, "email": email}) 76 | assert ret['success'] 77 | resp = self._client._request("/api/v1/user/devices/" + self._udid) 78 | self._info = resp['device'] 79 | return self._info 80 | except requests.HTTPError as e: 81 | raise Error(e) 82 | 83 | def release(self): 84 | ret = self._client._request("/api/v1/user/devices/" + self._udid, 85 | method="delete") 86 | return ret 87 | 88 | @property 89 | def info(self): 90 | return self._info 91 | 92 | @property 93 | def atx_agent_address(self): 94 | return self._info['source']['atxAgentAddress'] 95 | 96 | @property 97 | def remote_connect_address(self): 98 | return self._info['source']['remoteConnectAddress'] 99 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | requests>=2.0 2 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | name = atxserver2 3 | license = MIT 4 | 5 | [files] 6 | packages = 7 | atxserver2 8 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | import setuptools 4 | setuptools.setup(setup_requires=['pbr'], pbr=True) 5 | -------------------------------------------------------------------------------- /tests/test_main.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | # 3 | 4 | import pytest 5 | import atxserver2 6 | 7 | 8 | @pytest.fixture 9 | def client(): 10 | return atxserver2.Client("http://localhost:4000", 11 | "8b996e7e2958491cb2b1d88823883043") 12 | 13 | 14 | def test_user(client: atxserver2.Client): 15 | info = client.user_info() 16 | assert "username" in info 17 | assert "email" in info 18 | 19 | 20 | def test_list_device(client: atxserver2.Client): 21 | for d in client.list_device(): 22 | d._udid = "xxx" 23 | d.acquire() 24 | print(d.atx_agent_address) 25 | print(d.remote_connect_address) 26 | d.release() 27 | 28 | 29 | def test_aquire_device(client: atxserver2.Client): 30 | assert len(client.list_device()) > 0, "test prepare is not ready, device list is empty" 31 | for d in client.list_device(): 32 | d.acquire(6000, "test") 33 | info = d.info 34 | d.release() 35 | print(info) 36 | assert info['idleTimeout'] == 6000 37 | assert info['using'] == True 38 | assert info['userId'] == "test" 39 | break 40 | 41 | --------------------------------------------------------------------------------