Commit ec386388 authored by loelkes's avatar loelkes
Browse files

file encoding, typos, documentation

parent a41a0b25
......@@ -6,9 +6,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
## [1.0.0] 2019-10-4
- Initial release
## [1.1.1] 2019-10-06
### Changed
- .url and .format now have setters and getters.
- setting .url resets .last_update to 0.
- Setting .format checks if the format is supported and sets it to 'raw' if not.
- Minor code structure changes.
- Renamed .checkConnection() to check_connection()
## [1.1.0] 2019-10-5
## [1.1.0] 2019-10-05
### Added
- Run the module directly with python -m obelixtools
## [1.0.0] 2019-10-04
- Initial release
......@@ -4,6 +4,51 @@
## API
```from obelixtools import API```
This is a small wrapper I wrote around the requests library. It has some extra features like a speedtest and connectivity check.
This is a small wrapper I wrote around the reuqests library. It has some extra features like a speedtestand connectivity check.
### Basic use
```
from obelixtools import API
endpoint = API(url, 'json')
endpoint.query()
print(endpoint.content)
```
### Speedtest and connectivity check
```
% python -m obelixtools
06-Oct-19 11:28:44 - obelixtools - INFO: Performing selftest with https://1.1.1.1
06-Oct-19 11:28:44 - obelixtools - INFO: Connected to the internet.
06-Oct-19 11:28:44 - obelixtools - INFO: Performing speedtest with http://speedtest.belwue.net/100M
06-Oct-19 11:29:11 - obelixtools - INFO: Connection speed is 3MB/sµ
```
### Variables
#### .url : str
The URL of the API.
#### .format : str
Define a certain data format for the data return by the API. This can by either _json_, _xml_ or raw. Any other value will sit it to raw.
#### .content
The content of the API response after postprocessing. Postprocessing happens by setting _.format_
### Methods
#### .query(url : str, optional) -> bool
Fetched the data from the API if the age of the existing data in .content is older than *.last_update*. This timeout is ignored if _url_ is set (useful for speedtests).
#### .check_connection(url='https://1.1.1.1', timeout=5) -> bool
Fetches the given url and and returns True if the servers returns a status code 200 and False otherwise. Comprehensive log messages are passed to the loggin module.
#### .speedtest(url='http://speedtest.belwue.net/100M') -> bool
Downloads the file provided at the given url and returns the speed in bytes per second. The logging module received a log message with humand readable speed (e.g. kB/s, MB/s...)
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
import json
import requests
......@@ -89,7 +92,7 @@ class API(object):
try:
response = requests.get(self.request_url, auth=self._auth, stream=True)
except Excpetion as e:
logging.warning(e)
logger.warning(e)
pass
if response.status_code == 200:
self.content = None
......@@ -121,6 +124,8 @@ class API(object):
if response.status_code == 200:
self._handle_response(response)
self.postQuery()
self.lastUpdate = time.time()
self.status = True
else:
pass
else:
......@@ -134,8 +139,6 @@ class API(object):
self.content = ET.fromstring(response.content)
else:
self.content = response.content
self.lastUpdate = time.time()
self.status = True
def update(self):
pass
......@@ -160,6 +163,6 @@ class API(object):
return False
# Source https://stackoverflow.com/a/43750422
def human_readable(bytes, units=[' bytes','KB','MB','GB','TB', 'PB', 'EB']):
def human_readable(bytes, units=[' bytes','kB','MB','GB','TB', 'PB', 'EB']):
""" Returns a human readable string reprentation of bytes"""
return str(bytes) + units[0] if bytes < 1024 else human_readable(bytes>>10, units[1:])
#!/usr/bin/python
# -*- coding: utf-8 -*-
from . import API
import logging
......
#!/usr/bin/python
# -*- coding: utf-8 -*-
import setuptools
with open('README.md', 'r') as fh:
......@@ -5,7 +8,7 @@ with open('README.md', 'r') as fh:
setuptools.setup(
name='obelixtools',
version='1.1.0',
version='1.1.1',
author='Christian Lölkes',
author_email='christian.loelkes@gmail.com',
description='Useful tools I need in many projects.',
......@@ -15,12 +18,14 @@ setuptools.setup(
packages=setuptools.find_packages(),
classifiers=[
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 2.7',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
],
python_requires='>=3.6',
python_requires='>=2.7',
install_requires=[
'requests',
'requests[security]',
'sseclient'
],
)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment