def _get_servers_iter(self, full_url_pattern, ooperations_iter): for path, operation, path_result in ooperations_iter: servers = path.servers or operation.servers or self.spec.servers for server in servers: server_url_pattern = full_url_pattern.rsplit( path_result.resolved, 1)[0] server_url = server.url if not server.is_absolute(): # relative to absolute url if self.base_url is not None: server_url = urljoin(self.base_url, server.url) # if no base url check only path part else: server_url_pattern = urlparse(server_url_pattern).path if server_url.endswith('/'): server_url = server_url[:-1] # simple path if server_url_pattern == server_url: server_result = TemplateResult(server.url, {}) yield ( path, operation, server, path_result, server_result, ) # template path else: result = parse(server.url, server_url_pattern) if result: server_result = TemplateResult( server.url, result.named) yield ( path, operation, server, path_result, server_result, )
def _get_paths_iter(self, full_url_pattern): for path_pattern, path in iteritems(self.spec.paths): # simple path if full_url_pattern.endswith(path_pattern): path_result = TemplateResult(path_pattern, {}) yield (path, path_result) # template path else: result = search(path_pattern, full_url_pattern) if result: path_result = TemplateResult(path_pattern, result.named) yield (path, path_result)
def test_valid(self, finder, path_2, operation_2, server): request_uri = '/keys/master/tokens' request = MockRequest('http://petstore.swagger.io', 'get', request_uri) result = finder.find(request) path_result = TemplateResult(path_2.name, {}) server_result = TemplateResult(self.server_url, {}) assert result == ( path_2, operation_2, server, path_result, server_result, )
def test_path_variable(self, finder, path, operation, server, res_id): request_uri = '/resource/{0}'.format(res_id) request = MockRequest('http://petstore.swagger.io', 'get', request_uri) result = finder.find(request) path_result = TemplateResult(self.path_name, {'resource_id': res_id}) server_result = TemplateResult(self.server_url, {}) assert result == ( path, operation, server, path_result, server_result, )
def test_variable(self, finder, path, operation, server, version): request_uri = '/{0}/resource'.format(version) request = MockRequest('http://petstore.swagger.io', 'get', request_uri) result = finder.find(request) path_result = TemplateResult(self.path_name, {}) server_result = TemplateResult(self.server_url, {'version': version}) assert result == ( path, operation, server, path_result, server_result, )
def test_simple(self, finder, path, operation, server): request_uri = '/resource' request = MockRequest('http://petstore.swagger.io', 'get', request_uri) result = finder.find(request) path_result = TemplateResult(self.path_name, {}) server_result = TemplateResult(self.server_url, {}) assert result == ( path, operation, server, path_result, server_result, )
def test_valid(self, finder, path_2, operation_2, server): token_id = '123' request_uri = '/keys/{0}/tokens'.format(token_id) request = MockRequest('http://petstore.swagger.io', 'get', request_uri) result = finder.find(request) path_result = TemplateResult(path_2.name, {'id': token_id}) server_result = TemplateResult(self.server_url, {}) assert result == ( path_2, operation_2, server, path_result, server_result, )
def _get_paths_iter(self, full_url_pattern): template_paths = [] for path_pattern, path in iteritems(self.spec.paths): # simple path. # Return right away since it is always the most concrete if full_url_pattern.endswith(path_pattern): path_result = TemplateResult(path_pattern, {}) yield (path, path_result) # template path else: result = search(path_pattern, full_url_pattern) if result: path_result = TemplateResult(path_pattern, result.named) template_paths.append((path, path_result)) # Fewer variables -> more concrete path for path in sorted(template_paths, key=lambda p: len(p[1].variables)): yield path
def _get_servers_iter(self, full_url_pattern, ooperations_iter): for path, operation, path_result in ooperations_iter: servers = (path.get("servers", None) or operation.get("servers", None) or self.spec.get("servers", [{ "url": "/" }])) for server in servers: server_url_pattern = full_url_pattern.rsplit( path_result.resolved, 1)[0] server_url = server["url"] if not is_absolute(server_url): # relative to absolute url if self.base_url is not None: server_url = urljoin(self.base_url, server["url"]) # if no base url check only path part else: server_url_pattern = urlparse(server_url_pattern).path if server_url.endswith("/"): server_url = server_url[:-1] # simple path if server_url_pattern == server_url: server_result = TemplateResult(server["url"], {}) yield ( path, operation, server, path_result, server_result, ) # template path else: result = parse(server["url"], server_url_pattern) if result: server_result = TemplateResult(server["url"], result.named) yield ( path, operation, server, path_result, server_result, )
def test_valid(self, finder, spec): request_uri = "/keys/master/tokens" method = "get" request = MockRequest("http://petstore.swagger.io", method, request_uri) result = finder.find(request.method, request.full_url_pattern) path_2 = spec / "paths" / self.path_2_name operation_2 = spec / "paths" / self.path_2_name / method server = eval(self.location) / "servers" / 0 path_result = TemplateResult(self.path_2_name, {}) server_result = TemplateResult(self.server_url, {}) assert result == ( path_2, operation_2, server, path_result, server_result, )
def test_valid(self, finder, spec): request_uri = '/keys/master/tokens' method = 'get' request = MockRequest('http://petstore.swagger.io', method, request_uri) result = finder.find(request) path_2 = spec / 'paths' / self.path_2_name operation_2 = spec / 'paths' / self.path_2_name / method server = eval(self.location) / 'servers' / 0 path_result = TemplateResult(self.path_2_name, {}) server_result = TemplateResult(self.server_url, {}) assert result == ( path_2, operation_2, server, path_result, server_result, )
def test_path_variable(self, finder, spec, res_id): request_uri = f"/resource/{res_id}" method = "get" request = MockRequest("http://petstore.swagger.io", method, request_uri) result = finder.find(request.method, request.full_url_pattern) path = spec / "paths" / self.path_name operation = spec / "paths" / self.path_name / method server = eval(self.location) / "servers" / 0 path_result = TemplateResult(self.path_name, {"resource_id": res_id}) server_result = TemplateResult(self.server_url, {}) assert result == ( path, operation, server, path_result, server_result, )
def test_path_variable(self, finder, spec, res_id): request_uri = '/resource/{0}'.format(res_id) method = 'get' request = MockRequest('http://petstore.swagger.io', method, request_uri) result = finder.find(request) path = spec / 'paths' / self.path_name operation = spec / 'paths' / self.path_name / method server = eval(self.location) / 'servers' / 0 path_result = TemplateResult(self.path_name, {'resource_id': res_id}) server_result = TemplateResult(self.server_url, {}) assert result == ( path, operation, server, path_result, server_result, )