From 2f9a2edb44896598cdfd18553ff6346b1c373f05 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 5 Jul 2023 11:54:58 +0200 Subject: [PATCH] make calls to manuals resouce --- ereuse_devicehub/config.py | 1 + ereuse_devicehub/modules/did/views.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/ereuse_devicehub/config.py b/ereuse_devicehub/config.py index 5766156a..f93c1b4b 100644 --- a/ereuse_devicehub/config.py +++ b/ereuse_devicehub/config.py @@ -98,6 +98,7 @@ class DevicehubConfig(Config): API_DLT = config('API_DLT', None) API_DLT_TOKEN = config('API_DLT_TOKEN', None) ID_FEDERATED = config('ID_FEDERATED', None) + URL_MANUALS = config('URL_MANUALS', None) """Definition of oauth jwt details.""" OAUTH2_JWT_ENABLED = config('OAUTH2_JWT_ENABLED', False) diff --git a/ereuse_devicehub/modules/did/views.py b/ereuse_devicehub/modules/did/views.py index 4f7bf757..42223b89 100644 --- a/ereuse_devicehub/modules/did/views.py +++ b/ereuse_devicehub/modules/did/views.py @@ -1,6 +1,7 @@ import json import flask +import requests from ereuseapi.methods import API from flask import Blueprint from flask import current_app as app @@ -183,5 +184,28 @@ class DidView(View): dpps.append(rr) return {'data': dpps} + def get_manuals(self): + self.params = "{} {}".format(self.device.manufacturer, self.model) + manuals = {'ifixit': {}, 'icecat': {}} + manuals['ifixit'] = self.request_manuals('ifixit') + manuals['icecat'] = self.request_manuals('icecat') + return manuals + + def request_manuals(self, prefix): + url = app.config('URL_MANUALS') + if not url: + return {} + + res = requests.post(url + "/" + prefix, self.params) + if res.status_code > 299: + return {} + + try: + response = res.json() + except Exception: + response = {} + + return response + did.add_url_rule('/', view_func=DidView.as_view('did'))