From c2383140b6f6772c9133229c64fc5b226ba4ac06 Mon Sep 17 00:00:00 2001 From: Ivan Vazhenin Date: Sun, 26 Mar 2023 15:29:17 +0300 Subject: [PATCH] Fix GetCatalog --- main.py | 124 +++++++++++++----- {requests => reqs}/__init__.py | 0 {requests => reqs}/catalog.py | 0 {requests => reqs}/classifier.py | 0 .../correcting_replication_service.py | 0 {requests => reqs}/enumerations.py | 0 {requests => reqs}/graphql.py | 14 ++ {requests => reqs}/messages.py | 0 {requests => reqs}/models.py | 0 {requests => reqs}/request_processor.py | 0 {requests => reqs}/request_xml_service.py | 2 +- requirements.txt | 3 +- 12 files changed, 109 insertions(+), 34 deletions(-) rename {requests => reqs}/__init__.py (100%) rename {requests => reqs}/catalog.py (100%) rename {requests => reqs}/classifier.py (100%) rename {requests => reqs}/correcting_replication_service.py (100%) rename {requests => reqs}/enumerations.py (100%) rename {requests => reqs}/graphql.py (66%) rename {requests => reqs}/messages.py (100%) rename {requests => reqs}/models.py (100%) rename {requests => reqs}/request_processor.py (100%) rename {requests => reqs}/request_xml_service.py (99%) diff --git a/main.py b/main.py index c3e0e27..4a19f73 100644 --- a/main.py +++ b/main.py @@ -1,14 +1,16 @@ import datetime -from uuid import uuid4 +import json +from uuid import uuid4, UUID from xmlrpc.server import SimpleXMLRPCServer from xmlrpc.client import ServerProxy import logging import os import os.path -from requests.graphql import get_catalog +import requests +from reqs.graphql import get_catalog, get_object from pygost import gost34112012256 import xml.etree.ElementTree as ET -from requests.request_xml_service import RequestXmlService +from reqs.request_xml_service import RequestXmlService import zipfile PASSWORD = 'gost_2012$a742ec53198ec2a5027086fba8814a89982a57112d1a72d02260161108f39b50' @@ -37,35 +39,92 @@ def auth_challenge(): return uuid4().hex +def restore_uuid(oid): + uuid = UUID(oid) + return str(uuid) + + +def get_catalog(params, files, url): + date = datetime.datetime.now() + rxmls = RequestXmlService() + req = ET.fromstring(params['query_data']) + req_id = rxmls.get_request_uuid(req) + res_id = uuid4().hex + res = rxmls.get_request_document(res_id, req_id) + rxmls.set_result(res, 0, '') + response_params = { + 'from': params['to'], + 'to': params['from'], + 'ts_added': date.timestamp(), + 'user_id': '1', + 'user_id_to': params['user_id'], + 'query_type': 1004, + 'query_data': ET.tostring(res, encoding='unicode', xml_declaration=True) + } + catalog = get_catalog() + filename = uuid4().hex + filepath = '/tmp/' + filename + zipf = zipfile.ZipFile(filepath, "w") + zipf.writestr('WF.CLL', catalog) + zipf.close() + response_files = [{'name': filename, 'url': filepath, 'size': os.path.getsize(filepath)}] + proxy = ServerProxy(url) + proxy.send(response_params, response_files, 'http://10.10.8.27:9000/') + + +def get_objects(params, files, url): + date = datetime.datetime.now() + rxmls = RequestXmlService() + req = ET.fromstring(params['query_data']) + req_id = rxmls.get_request_uuid(req) + res_id = uuid4().hex + res = rxmls.get_request_document(res_id, req_id) + rxmls.set_result(res, 0, '') + objs = req.find('objects') + uids = [restore_uuid(x.get('object_id')) for x in objs.findall('object')] + + rxmls.set_result(res, 0, '') + response_params = { + 'from': params['to'], + 'to': params['from'], + 'ts_added': date.timestamp(), + 'user_id': '1', + 'user_id_to': params['user_id'], + 'query_type': 1001, + 'query_data': ET.tostring(res, encoding='unicode', xml_declaration=True) + } + filename = uuid4().hex + filepath = '/tmp/' + filename + zipf = zipfile.ZipFile(filepath, "w") + main_filename = None + for uid in uids: + obj = json.loads(get_object(uid)) + print(type(obj)) + for file in obj['properties'].get('c1000', []): + if not main_filename: + main_filename = file['fileName'] + res = requests.get(f'https://gql.ivazh.ru/item/{file["key"]}') + zipf.writestr(f'{main_filename}/{file["fileName"]}', res.content) + zipf.close() + response_files = [{'name': filename, 'url': filepath, 'size': os.path.getsize(filepath)}] + print(response_files) + # proxy = ServerProxy(url) + # proxy.send(response_params, response_files, 'http://10.10.8.27:9000/') + + +def get_metadata(params, files, url): + pass + + def accept(params, files, url): print(params, files, url) print('Accept') - date = datetime.datetime.now() if params['query_type'] == 4: - rxmls = RequestXmlService() - req = ET.fromstring(params['query_data']) - req_id = rxmls.get_request_uuid(req) - res_id = uuid4().hex - res = rxmls.get_request_document(res_id, req_id) - rxmls.set_result(res, 0, '') - response_params = { - 'from': params['to'], - 'to': params['from'], - 'ts_added': date.timestamp(), - 'user_id': '1', - 'user_id_to': '3302', - 'query_type': 1004, - 'query_data': ET.tostring(res, encoding='unicode', xml_declaration=True) - } - catalog = get_catalog() - filename = uuid4().hex - filepath = '/tmp/' + filename - zipf = zipfile.ZipFile(filepath, "w") - zipf.writestr('WF.CLL', catalog) - zipf.close() - response_files = [{'name': filename, 'url': filepath, 'size': os.path.getsize(filepath)}] - proxy = ServerProxy(url) - proxy.send(response_params, response_files, 'http://10.10.8.27:9000/') + get_catalog(params, files, url) + if params['query_type'] == 1: + get_objects(params, files, url) + if params['query_type'] == 24: + get_metadata(params, files, url) return True @@ -91,12 +150,13 @@ def main(): def test(): - params = {'from': 'tcp://kptsp_vb', 'query_data': '
', 'query_type': 4, 'to': 'tcp://bnd127', 'user_id': '1', 'ts_added': 1679213998.282467} + params = {"from": "tcp://kptsp_vb", "query_data": "
", "query_type": 1, "to": "tcp://bnd127", "user_id": "3302", "ts_added": 1679825320.653038} files = [] url = 'http://127.0.0.1:7000/xmlrpc' - accept(params, files, url) + # accept(params, files, url) + get_objects(params, files, url) if __name__ == '__main__': - main() - # test() + # main() + test() diff --git a/requests/__init__.py b/reqs/__init__.py similarity index 100% rename from requests/__init__.py rename to reqs/__init__.py diff --git a/requests/catalog.py b/reqs/catalog.py similarity index 100% rename from requests/catalog.py rename to reqs/catalog.py diff --git a/requests/classifier.py b/reqs/classifier.py similarity index 100% rename from requests/classifier.py rename to reqs/classifier.py diff --git a/requests/correcting_replication_service.py b/reqs/correcting_replication_service.py similarity index 100% rename from requests/correcting_replication_service.py rename to reqs/correcting_replication_service.py diff --git a/requests/enumerations.py b/reqs/enumerations.py similarity index 100% rename from requests/enumerations.py rename to reqs/enumerations.py diff --git a/requests/graphql.py b/reqs/graphql.py similarity index 66% rename from requests/graphql.py rename to reqs/graphql.py index 40edd80..9e4ae94 100644 --- a/requests/graphql.py +++ b/reqs/graphql.py @@ -28,3 +28,17 @@ def get_catalog(): ) result = client.execute(query) return result['getCatalog'] + + +def get_object(oid: str): + client = Client(transport=transport, fetch_schema_from_transport=True) + query = gql( + """ + query getObjects($oid: String!) { + getObject(name: "ood", oid: $oid) + } + """ + ) + params = {'oid': oid} + result = client.execute(query, variable_values=params) + return result['getObject'] diff --git a/requests/messages.py b/reqs/messages.py similarity index 100% rename from requests/messages.py rename to reqs/messages.py diff --git a/requests/models.py b/reqs/models.py similarity index 100% rename from requests/models.py rename to reqs/models.py diff --git a/requests/request_processor.py b/reqs/request_processor.py similarity index 100% rename from requests/request_processor.py rename to reqs/request_processor.py diff --git a/requests/request_xml_service.py b/reqs/request_xml_service.py similarity index 99% rename from requests/request_xml_service.py rename to reqs/request_xml_service.py index f09948f..01396b1 100644 --- a/requests/request_xml_service.py +++ b/reqs/request_xml_service.py @@ -1,5 +1,5 @@ import xml.etree.ElementTree as ET -from requests.messages import Messages +from reqs.messages import Messages class RequestXmlService: diff --git a/requirements.txt b/requirements.txt index e25d023..70f74e1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ gql~=3.4.0 -pygost~=5.11 \ No newline at end of file +pygost~=5.11 +requests~=2.28.2 \ No newline at end of file