Skip to content

Commit

Permalink
switch from Travis CI to Github Actions (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
dopstar committed Feb 7, 2021
1 parent f8a5bfc commit 943443f
Show file tree
Hide file tree
Showing 21 changed files with 391 additions and 291 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[run]
branch=true
source=huaweisms
1 change: 0 additions & 1 deletion .coveralls.yml

This file was deleted.

72 changes: 72 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: build

on: [push]

jobs:
lint:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.6, 3.7, 3.8, 3.9]
fail-fast: true

name: Lint on Python ${{ matrix.python-version }}

steps:
- uses: actions/checkout@v2

- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Lint
run: |
pip install '.[linting]'
flake8 --ignore=E501 .
test:
needs: [lint]
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [2.7, 3.6, 3.7, 3.8, 3.9]
fail-fast: true

name: Test on Python ${{ matrix.python-version }}

steps:
- uses: actions/checkout@v2

- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Test
run: |
pip install '.[testing]'
pytest -xv --cov huaweisms --cov-report term-missing tests
- name: Upload coverage report to Codecov
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
publish:
needs: [test]
name: Publish to PyPI
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: 3.9
- name: Build package
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
run: |
pip install setuptools wheel
python setup.py sdist bdist_wheel
- name: Publish package
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@master
with:
user: __token__
password: ${{ secrets.PYPI_TOKEN }}
25 changes: 0 additions & 25 deletions .travis.yml

This file was deleted.

7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
[![Build Status](https://travis-ci.org/dopstar/huawei-modem-python-api-client.svg?branch=master)](https://travis-ci.org/dopstar/huawei-modem-python-api-client) [![Python Version](https://img.shields.io/pypi/pyversions/huawei-modem-api-client.svg)](https://pypi.python.org/pypi/huawei-modem-api-client) [![PyPI Status](https://img.shields.io/pypi/v/huawei-modem-api-client.svg)](https://pypi.python.org/pypi/huawei-modem-api-client) [![PyPI Downloads](https://img.shields.io/pypi/dm/huawei-modem-api-client.svg)](https://pypi.python.org/pypi/huawei-modem-api-client) [![Code Coverage](https://img.shields.io/coveralls/github/dopstar/huawei-modem-python-api-client.svg)](https://coveralls.io/github/dopstar/huawei-modem-python-api-client) [![Licence](https://img.shields.io/github/license/pablo/huawei-modem-python-api-client.svg)](https://raw.githubusercontent.com/pablo/huawei-modem-python-api-client/master/LICENSE.md)
[![Build Status](https://github.com/dopstar/huawei-modem-api-client/workflows/build/badge.svg?branch=master)](https://github.com/dopstar/huawei-modem-api-client/actions?query=workflow%3Abuild)
[![Python Version](https://img.shields.io/pypi/pyversions/huawei-modem-api-client.svg)](https://pypi.python.org/pypi/huawei-modem-api-client)
[![PyPI Status](https://img.shields.io/pypi/v/huawei-modem-api-client.svg)](https://pypi.python.org/pypi/huawei-modem-api-client)
[![PyPI Downloads](https://img.shields.io/pypi/dm/huawei-modem-api-client.svg)](https://pypi.python.org/pypi/huawei-modem-api-client)
[![Code Coverage](https://img.shields.io/coveralls/github/dopstar/huawei-modem-python-api-client.svg)](https://coveralls.io/github/dopstar/huawei-modem-python-api-client)
[![Licence](https://img.shields.io/github/license/pablo/huawei-modem-python-api-client.svg)](https://raw.githubusercontent.com/pablo/huawei-modem-python-api-client/master/LICENSE.md)

# Python HTTP API client for Huawei Modems

Expand Down
40 changes: 17 additions & 23 deletions huaweisms/api/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
from xml.dom.minidom import Element

import requests
from huaweisms.api.config import MODEM_HOST

from huaweisms.xml.util import get_child_text, parse_xml_string, get_dictionary_from_children
from huaweisms.api.config import MODEM_HOST
from huaweisms.xml.util import get_child_text, get_dictionary_from_children, parse_xml_string


logger = logging.getLogger(__name__)


class ApiCtx:

def __init__(self, modem_host=None):
# type: (...) -> None
self.session_id = None
Expand All @@ -22,10 +21,7 @@ def __init__(self, modem_host=None):
self.__modem_host = modem_host if modem_host else MODEM_HOST

def __unicode__(self):
return '<{} modem_host={}>'.format(
self.__class__.__name__,
self.__modem_host
)
return "<{} modem_host={}>".format(self.__class__.__name__, self.__modem_host)

def __repr__(self):
return self.__unicode__()
Expand All @@ -35,20 +31,18 @@ def __str__(self):

@property
def api_base_url(self):
return 'http://{}/api'.format(self.__modem_host)
return "http://{}/api".format(self.__modem_host)

@property
def token(self):
if not self.tokens:
logger.warning('You ran out of tokens. You need to login again')
logger.warning("You ran out of tokens. You need to login again")
return None
return self.tokens.pop()


def common_headers():
return {
"X-Requested-With": "XMLHttpRequest"
}
return {"X-Requested-With": "XMLHttpRequest"}


def check_error(elem):
Expand All @@ -60,8 +54,8 @@ def check_error(elem):
"type": "error",
"error": {
"code": get_child_text(elem, "code"),
"message": get_child_text(elem, "message")
}
"message": get_child_text(elem, "message"),
},
}


Expand All @@ -78,21 +72,23 @@ def api_response(r):

return {
"type": "response",
"response": get_dictionary_from_children(xmldoc.documentElement)
"response": get_dictionary_from_children(xmldoc.documentElement),
}


def check_response_headers(resp, ctx):
# type: (..., ApiCtx) -> ...
if '__RequestVerificationToken' in resp.headers:
toks = [x for x in resp.headers['__RequestVerificationToken'].split("#") if x != '']
if "__RequestVerificationToken" in resp.headers:
toks = [
x for x in resp.headers["__RequestVerificationToken"].split("#") if x != ""
]
if len(toks) > 1:
ctx.tokens = toks[2:]
elif len(toks) == 1:
ctx.tokens.append(toks[0])

if 'SessionID' in resp.cookies:
ctx.session_id = resp.cookies['SessionID']
if "SessionID" in resp.cookies:
ctx.session_id = resp.cookies["SessionID"]


def post_to_url(url, data, ctx=None, additional_headers=None):
Expand All @@ -103,7 +99,7 @@ def post_to_url(url, data, ctx=None, additional_headers=None):
if additional_headers:
headers.update(additional_headers)

data = ''.join(line.strip() for line in data.split('\n'))
data = "".join(line.strip() for line in data.split("\n"))
r = requests.post(url, data=data, headers=headers, cookies=cookies)
check_response_headers(r, ctx)
return api_response(r)
Expand All @@ -126,7 +122,5 @@ def build_cookies(ctx):
# type: (ApiCtx) -> ...
cookies = None
if ctx and ctx.session_id:
cookies = {
'SessionID': ctx.session_id
}
cookies = {"SessionID": ctx.session_id}
return cookies
14 changes: 6 additions & 8 deletions huaweisms/api/device.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
from huaweisms.api.common import (
post_to_url,
get_from_url,
ApiCtx,
)
from huaweisms.api.common import ApiCtx, get_from_url, post_to_url


def information(ctx):
Expand All @@ -23,10 +19,12 @@ def reboot(ctx):
Reboots the modem.
"""

url = '{}/device/control'.format(ctx.api_base_url)
url = "{}/device/control".format(ctx.api_base_url)
headers = {
'__RequestVerificationToken': ctx.token,
"__RequestVerificationToken": ctx.token,
}

payload = '<?xml version: "1.0" encoding="UTF-8"?><request><Control>1</Control></request>'
payload = (
'<?xml version: "1.0" encoding="UTF-8"?><request><Control>1</Control></request>'
)
return post_to_url(url, payload, ctx, additional_headers=headers)
24 changes: 12 additions & 12 deletions huaweisms/api/dialup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

XML_TEMPLATE = (
'<?xml version="1.0" encoding="UTF-8"?>'
'<request>'
'<dataswitch>{enable}</dataswitch>'
'</request>'
"<request>"
"<dataswitch>{enable}</dataswitch>"
"</request>"
)


Expand All @@ -23,20 +23,20 @@ def get_mobile_status(ctx):
# type: (huaweisms.api.common.ApiCtx) -> ...
url = "{}/dialup/mobile-dataswitch".format(ctx.api_base_url)
result = huaweisms.api.common.get_from_url(url, ctx)
if result and result.get('type') == 'response':
response = result['response']
if response and response.get('dataswitch') == '1':
return 'CONNECTED'
if response and response.get('dataswitch') == '0':
return 'DISCONNECTED'
return 'UNKNOWN'
if result and result.get("type") == "response":
response = result["response"]
if response and response.get("dataswitch") == "1":
return "CONNECTED"
if response and response.get("dataswitch") == "0":
return "DISCONNECTED"
return "UNKNOWN"


def switch_mobile_off(ctx):
# type: (huaweisms.api.common.ApiCtx) -> ...
data = XML_TEMPLATE.format(enable=0)
headers = {
'__RequestVerificationToken': ctx.token,
"__RequestVerificationToken": ctx.token,
}
url = "{}/dialup/mobile-dataswitch".format(ctx.api_base_url)
return huaweisms.api.common.post_to_url(url, data, ctx, additional_headers=headers)
Expand All @@ -46,7 +46,7 @@ def switch_mobile_on(ctx):
# type: (huaweisms.api.common.ApiCtx) -> ...
data = XML_TEMPLATE.format(enable=1)
headers = {
'__RequestVerificationToken': ctx.token,
"__RequestVerificationToken": ctx.token,
}
url = "{}/dialup/mobile-dataswitch".format(ctx.api_base_url)
return huaweisms.api.common.post_to_url(url, data, ctx, additional_headers=headers)
2 changes: 1 addition & 1 deletion huaweisms/api/monitoring.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from huaweisms.api.common import get_from_url, ApiCtx
from huaweisms.api.common import ApiCtx, get_from_url


def status(ctx):
Expand Down
Loading

0 comments on commit 943443f

Please sign in to comment.