Skip to content

Commit

Permalink
Add Linter
Browse files Browse the repository at this point in the history
This patch adds `flake8` and `bandit` as linter and security analysis tools.
It also integrates them into a CI pipeline being run on pull requests
and push events.
  • Loading branch information
lkiesow committed May 11, 2023
1 parent b116216 commit 2adb506
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
Binary file added .gitlab/workflows/.lint.yml.swp
Binary file not shown.
27 changes: 27 additions & 0 deletions .gitlab/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: lint

on:
- push
- pull_request

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: set up python
uses: actions/setup-python@v2
with:
python-version: '3.10'

- name: install Python dependencies
run: |
pip install -r requirements.txt
pip install bandit flake8
- name: run flake8
run: flake8

- name: run bandit
run: bandit -r deterrersapi
21 changes: 17 additions & 4 deletions deterrersapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ class Deterrers:
__base_url = None
__token = None

timeout = 30
'''Request timeout in seconds'''

def __init__(self, base_url: str, token: str) -> None:
'''Initialize DETERRERS client.
Expand Down Expand Up @@ -45,7 +48,8 @@ def __get(self, path: str, **params):
'''
response = requests.get(self.__url(path),
headers=self.__header(),
params=params)
params=params,
timeout=timeout)
return response.json() if response.status_code == 200 else None

def __patch(self, path: str, data: dict) -> None:
Expand All @@ -58,7 +62,10 @@ def __patch(self, path: str, data: dict) -> None:
:raises RuntimeError: if patch did not succeed
'''
url: str = self.__url(path)
response = requests.patch(url, headers=self.__header(), json=data)
response = requests.patch(url,
headers=self.__header(),
json=data,
timeout=timeout)
if response.status_code not in [200]:
raise RuntimeError(f'Error updating {data}. Response: {response}')

Expand All @@ -72,7 +79,10 @@ def __post(self, path: str, data: dict) -> None:
:raises RuntimeError: if adding data did not succeed
'''
url: str = self.__url(path)
response = requests.post(url, headers=self.__header(), json=data)
response = requests.post(url,
headers=self.__header(),
json=data,
timeout=timeout)
if response.status_code not in [200]:
raise RuntimeError(f'Error adding {data}. Response: {response}')

Expand All @@ -86,7 +96,10 @@ def __delete(self, path: str, data: dict) -> None:
:raises RuntimeError: if removal did not succeed
'''
url: str = self.__url(path)
response = requests.delete(url, headers=self.__header(), json=data)
response = requests.delete(url,
headers=self.__header(),
json=data,
timeout=timeout)
if response.status_code not in [200, 404]:
raise RuntimeError(f'Error deleting {data}. Response: {response}')

Expand Down

0 comments on commit 2adb506

Please sign in to comment.