-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from forza-mor-rotterdam/develop
develop -> main
- Loading branch information
Showing
15 changed files
with
187 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import logging | ||
from urllib.parse import urlencode | ||
|
||
import requests | ||
from django.core.cache import cache | ||
from requests import Request, Response | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class BasisService: | ||
_api_base_url = None | ||
_timeout: tuple[int, ...] = (5, 10) | ||
_api_path: str = "/api/v1" | ||
|
||
class BasisUrlFout(Exception): | ||
... | ||
|
||
class AntwoordFout(Exception): | ||
... | ||
|
||
class DataOphalenFout(Exception): | ||
... | ||
|
||
class NaarJsonFout(Exception): | ||
... | ||
|
||
def get_url(self, url): | ||
return url | ||
|
||
def get_headers(self): | ||
return {} | ||
|
||
def naar_json(self, response): | ||
try: | ||
return response.json() | ||
except Exception: | ||
raise BasisService.NaarJsonFout( | ||
f"Json antwoord verwacht: url={response.url}, status code={response.status_code}, tekst={response.text}" | ||
) | ||
|
||
def do_request( | ||
self, url, method="get", data={}, params={}, raw_response=True, cache_timeout=0 | ||
) -> Response | dict: | ||
action: Request = getattr(requests, method) | ||
url = self.get_url(url) | ||
action_params: dict = { | ||
"url": url, | ||
"headers": self.get_headers(), | ||
"json": data, | ||
"params": params, | ||
"timeout": self._timeout, | ||
} | ||
|
||
if cache_timeout and method == "get": | ||
cache_key = f"{url}?{urlencode(params)}" | ||
response = cache.get(cache_key) | ||
if not response: | ||
response: Response = action(**action_params) | ||
if int(response.status_code) == 200: | ||
cache.set(cache_key, response, cache_timeout) | ||
else: | ||
response: Response = action(**action_params) | ||
|
||
if raw_response: | ||
return response | ||
return self.naar_json(response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import logging | ||
|
||
from apps.services.basis import BasisService | ||
from django.template.loader import get_template | ||
from django.utils.safestring import mark_safe | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def render_onderwerp(onderwerp_url, standaar_naam=None): | ||
onderwerp = OnderwerpenService().get_onderwerp(onderwerp_url) | ||
standaard_naam = onderwerp.get( | ||
"name", "Niet gevonden!" if not standaar_naam else standaar_naam | ||
) | ||
if onderwerp.get("priority") == "high": | ||
spoed_badge = get_template("badges/spoed.html") | ||
return mark_safe(f"{standaard_naam}{spoed_badge.render()}") | ||
return standaard_naam | ||
|
||
|
||
class OnderwerpenService(BasisService): | ||
def get_onderwerp(self, url) -> dict: | ||
return self.do_request(url, cache_timeout=60 * 10, raw_response=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters