Commit 8167ceb4 authored by Stefane Fermigier's avatar Stefane Fermigier

chore: CI config.

parent ef13f531
name: CI
on:
push:
branches: [main, master]
pull_request:
branches: [main, master, dev]
jobs:
test:
runs-on: ${{ matrix.os }}-latest
strategy:
fail-fast: false
matrix:
os: [ubuntu, macos]
python-version: ['3.8', '3.9']
exclude:
- os: windows
python-version: pypy3
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- run: pip install -U pip setuptools wheel tox tox-gh-actions poetry
- id: pip-cache
run: echo "::set-output name=dir::$(pip cache dir)"
- uses: actions/cache@v2
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: ${{ runner.os }}-pip-${{ matrix.python-version }}-${{ hashFiles('poetry.lock') }}
restore-keys: |
${{ runner.os }}-pip-${{ matrix.python-version }}-
${{ runner.os }}-pip-
- run: tox
- if: ${{ matrix.os == 'ubuntu' && matrix.python-version == '3.8' }}
run: |
pip install codecov
codecov
......@@ -11,7 +11,7 @@ all: test lint
# Run
#
.PHONY:
run:
run:
gunicorn -b localhost:3000 \
--pid server.pid \
--keyfile ssl/httpd.key \
......@@ -64,7 +64,7 @@ test-randomly:
.PHONY:
test-with-coverage:
@echo "--> Running Python tests"
py.test --cov $(PKG)
pytest --cov $(PKG)
@echo ""
.PHONY:
......@@ -78,14 +78,11 @@ test-with-typeguard:
# Various Checkers
#
.PHONY:
lint: lint-py lint-mypy lint-rst lint-doc
lint: lint-py lint-mypy lint-bandit
.PHONY:
lint-ci: lint
.PHONY:
lint-all: lint lint-bandit
.PHONY:
lint-py:
@echo "--> Linting Python files /w flake8"
......@@ -99,23 +96,9 @@ lint-mypy:
@echo ""
.PHONY:
lint-travis:
@echo "--> Linting .travis.yml files"
travis lint --no-interactive
@echo ""
.PHONY:
lint-rst:
@echo "--> Linting .rst files"
rst-lint *.rst
@echo ""
.PHONY:
lint-doc:
@echo "--> Linting doc"
@echo "TODO"
#sphinx-build -W -b dummy docs/ docs/_build/
#sphinx-build -b dummy docs/ docs/_build/
lint-bandit:
@echo "--> Security audit w/ Bandit"
bandit -q src/*.py
@echo ""
......
[isort]
profile = black
[mypy]
ignore_missing_imports = True
warn_redundant_casts = True
warn_no_return = True
warn_unused_ignores = True
# TODO: reenable when ready
# disallow_untyped_defs = True
strict_optional = True
import traceback
from typing import Dict, Mapping, Tuple
from __future__ import annotations
from typing import Mapping, Any
import httpx
from starlette.applications import Starlette
......@@ -8,8 +9,7 @@ from starlette.requests import Request
from starlette.responses import PlainTextResponse, Response
from starlette.routing import Route
# Extremely aggressive and hardcoded value
TIMEOUT = 10
TIMEOUT = 60
DEFAULT_ACCESS_URL = "https://mynij.app.officejs.com"
......@@ -25,11 +25,10 @@ class ProxyEndPoint(HTTPEndpoint):
self.url = request.query_params["url"]
status_code, content, new_headers = await self.fetch_content()
# debug(status_code, content, new_headers)
response = Response(content, status_code, new_headers)
return response
async def fetch_content(self) -> Tuple[int, bytes, dict]:
async def fetch_content(self) -> tuple[int, bytes, dict]:
proxy_query_header = self.make_query_headers(self.headers)
body = b""
response_headers = {}
......@@ -47,23 +46,21 @@ class ProxyEndPoint(HTTPEndpoint):
# # Invalid SSL Certificate
# status = 526
except TimeoutError:
traceback.print_exc()
# Gateway Timeout
status = 504
except httpx.TransportError:
traceback.print_exc()
# Service Unavailable
status = 503
except httpx.HTTPError:
traceback.print_exc()
# Internal Server Error
status = 500
return status, body, response_headers
def make_query_headers(self, headers: Mapping) -> Mapping:
@staticmethod
def make_query_headers(headers: Mapping[str, Any]) -> dict[str, str]:
request_headers = {}
HEADERS = [
keep_headers = [
"Content-Type",
"Accept",
"Accept-Language",
......@@ -71,7 +68,7 @@ class ProxyEndPoint(HTTPEndpoint):
"If-Modified-Since",
"If-None-Match",
]
for k in HEADERS:
for k in keep_headers:
v = headers.get(k)
if v:
request_headers[k] = str(v)
......@@ -81,9 +78,9 @@ class ProxyEndPoint(HTTPEndpoint):
def get_access_url(self):
return self.headers.get("Origin", DEFAULT_ACCESS_URL)
def filter_response_headers(self, proxy_response) -> Dict[str, str]:
def filter_response_headers(self, proxy_response: Response) -> dict[str, str]:
headers = {}
HEADERS = [
keep_headers = [
"Content-Disposition",
"Content-Type",
"Date",
......@@ -96,7 +93,7 @@ class ProxyEndPoint(HTTPEndpoint):
]
for k, v in proxy_response.headers.items():
k = k.title()
if k in HEADERS:
if k in keep_headers:
headers[k] = v
return headers
......
[tox]
envlist = py38, py39, lint
skipsdist = True
[testenv]
skip_install = true
whitelist_externals =
poetry
sh
make
yarn
commands_pre =
poetry install -q
commands =
make test
[testenv:lint]
commands =
make lint
safety check
[gh-actions]
python =
3.8: py38
3.9: py39, lint
Markdown is supported
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