From af0a3255b470e9e0db1fe19de680a7abb66b0641 Mon Sep 17 00:00:00 2001 From: Javier Sánchez Parra Date: Wed, 3 Aug 2022 14:52:02 +0200 Subject: Add helper function to send requests to all ogServers 'multi_request()' function make requests to ogServers defined in the configuration and aggregates it responses. This commit also makes 'get_scopes()' use it. --- ogcp/views.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'ogcp') diff --git a/ogcp/views.py b/ogcp/views.py index 1ccd39b..a26ad55 100644 --- a/ogcp/views.py +++ b/ogcp/views.py @@ -176,13 +176,33 @@ def get_allowed_scopes(scopes, allowed_scopes): else: get_allowed_scopes(scope, allowed_scopes) +def multi_request(method, uri, payload=None): + responses = [] + + for server in servers: + response = {} + + if method == 'get': + r = server.get(uri, payload) + elif method == 'post': + r = server.post(uri, payload) + else: + raise Exception('Invalid method, use get or post') + + response['server'] = server.name + response['status_code'] = r.status_code + response['json'] = r.json() + responses.append(response) + + return responses + def get_scopes(ips=set()): list_scopes = [] - for server in servers: - r = server.get('/scopes') - scopes = r.json() + responses = multi_request('get', '/scopes') + for r in responses: + scopes = r['json'] server_scope = {} - server_scope['name'] = server.name + server_scope['name'] = r['server'] server_scope.update(scopes) list_scopes.append(server_scope) all_scopes = {'scope': list_scopes} -- cgit v1.2.3-18-g5258