Skip to content

Commit

Permalink
Merge pull request #4 from kuefmz/main
Browse files Browse the repository at this point in the history
Set up the HTTP proxy for the failover mode, add testcases, CI
  • Loading branch information
JJ-Author authored Jun 7, 2024
2 parents 0f32deb + 9f8d76d commit b276b66
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Pylint

on: [push]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10"]
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- uses: Gr1N/setup-poetry@v8 #install poetry
- name: Install parts of toolchain
run: |
python -m pip install --upgrade pip
- name: Install requirements with poetry
run: poetry install
- name: Test with pytest
run: |
poetry run pytest
Empty file added ontologytimemachine/__init__.py
Empty file.
74 changes: 74 additions & 0 deletions ontologytimemachine/proxy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from http.server import BaseHTTPRequestHandler, HTTPServer
import threading
import requests

PORT = 8080

class ProxyHandler(BaseHTTPRequestHandler):
def do_GET(self):
# static usecase to check the Proxy is working
if 'google.com' in self.path:
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(b'This is a static response for google.com')
else:
self.proxy_logic()

def proxy_logic(self):
self.failover_mode()
self.time_based_mode()
self.dependency_based_mode()

def failover_mode(self):
ontology = self.path[1:]
try:
response = requests.get(ontology)
content_type = response.headers.get('Content-Type', '')

if response.status_code == 200 and content_type in ['text/turtle']:
self.send_response(200)
self.send_header('Content-type', content_type)
self.end_headers()
self.wfile.write(response.content)
else:
self.fetch_from_dbpedia_archivo_api(ontology)

except requests.exceptions.RequestException:
self.fetch_from_dbpedia_archivo_api(ontology)

def fetch_from_dbpedia_archivo_api(self, ontology):
dbpedia_url = f'https://archivo.dbpedia.org/download?o={ontology}&f=ttl'
print(dbpedia_url)
try:
response = requests.get(dbpedia_url)
if response.status_code == 200:
self.send_response(200)
self.send_header('Content-type', 'text/turtle')
self.end_headers()
self.wfile.write(response.content)
else:
self.send_error(404, 'Resource not found')
except requests.exceptions.RequestException:
self.send_error(404, 'Resource not found')

def time_based_mode(self):
pass

def dependency_based_mode(self):
pass

def run_proxy(server_class=HTTPServer, handler_class=ProxyHandler, port=PORT):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print(f'Starting proxy on port {port}...')
httpd.serve_forever()

def start_proxy():
proxy_thread = threading.Thread(target=run_proxy)
proxy_thread.daemon = True
proxy_thread.start()
return proxy_thread

if __name__ == '__main__':
start_proxy().join()
17 changes: 17 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[tool.poetry]
name = "ontologytimemachine"
version = "0.1.0"
description = ""
authors = ["Jenifer Tabita Ciuciu-Kiss <[email protected]>"]
readme = "README.md"
packages = [{include = "ontologytimemachine"}]

[tool.poetry.dependencies]
python = "^3.10"
pytest = "^8.2.1"
requests = "^2.32.3"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Empty file added tests/__init__.py
Empty file.
30 changes: 30 additions & 0 deletions tests/test_proxy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest
import requests
from ontologytimemachine.proxy import start_proxy, PORT

PROXY_URL = f'http://localhost:{PORT}'

@pytest.fixture(scope="module", autouse=True)
def start_proxy_server():
start_proxy()
import time
time.sleep(1)

def test_google_com():
response = requests.get(PROXY_URL + '/google.com')
assert response.status_code == 200
assert response.text == 'This is a static response for google.com'

def test_linked_web_apis():
response = requests.get(PROXY_URL + '/http://linked-web-apis.fit.cvut.cz/ns/core')
assert response.status_code == 200
assert response.headers['Content-Type'] == 'text/turtle'

def test_ontologi_es():
response = requests.get(PROXY_URL + '/http%3A//ontologi.es/days%23')
assert response.status_code == 200
assert response.headers['Content-Type'] == 'text/turtle'


if __name__ == '__main__':
pytest.main()

0 comments on commit b276b66

Please sign in to comment.