Commit fb396a85 authored by Sigmund Augdal's avatar Sigmund Augdal

Added support for getting subresources inlined in requests.

This allows a significant speedup of for instance getting VMs by MAC address
parent aa74e7e2
...@@ -65,7 +65,7 @@ class Client(object): ...@@ -65,7 +65,7 @@ class Client(object):
self.auth = HTTPBasicAuth(username, password) self.auth = HTTPBasicAuth(username, password)
self.session = requests.Session() self.session = requests.Session()
def _format_url(self, resource, query=None, args=None, subresource=None): def _format_url(self, resource, query=None, args=None, subresource=None, expand=None):
url = self.baseurl url = self.baseurl
fragments = {} fragments = {}
if resource: if resource:
...@@ -78,6 +78,8 @@ class Client(object): ...@@ -78,6 +78,8 @@ class Client(object):
if query: if query:
argstr = " and ".join(("{key} eq {value}".format(key=key, value=type_encode_argvalue(query[key])) for key in query)) argstr = " and ".join(("{key} eq {value}".format(key=key, value=type_encode_argvalue(query[key])) for key in query))
fragments["$filter"] = argstr fragments["$filter"] = argstr
if expand:
fragments["$expand"] = expand
if fragments: if fragments:
url += "?" + urlencode(fragments) url += "?" + urlencode(fragments)
return url return url
...@@ -121,8 +123,8 @@ class Client(object): ...@@ -121,8 +123,8 @@ class Client(object):
collections = self._do_request(url) collections = self._do_request(url)
return (collection for collection in collections.xpath('/b:service/b:workspace/b:collection/atom:title/text()', namespaces=namespaces)) return (collection for collection in collections.xpath('/b:service/b:workspace/b:collection/atom:title/text()', namespaces=namespaces))
def query_collection(self, collection, query=None, **kw_args): def query_collection(self, collection, query=None, expand=None, **kw_args):
url = self._format_url(collection, query, kw_args) url = self._format_url(collection, query, kw_args, expand=expand)
collection = self._do_request(url) collection = self._do_request(url)
result = [] result = []
for entry in collection.xpath('/atom:feed/atom:entry', namespaces=namespaces): for entry in collection.xpath('/atom:feed/atom:entry', namespaces=namespaces):
...@@ -165,6 +167,7 @@ class Entry(object): ...@@ -165,6 +167,7 @@ class Entry(object):
def __init__(self, element, vmm_client): def __init__(self, element, vmm_client):
self.vmm_client = vmm_client self.vmm_client = vmm_client
self.element = element self.element = element
self.inline_resources = {}
self.properties = self.parse_properties(element) self.properties = self.parse_properties(element)
self.resources = self.parse_resources(element) self.resources = self.parse_resources(element)
...@@ -180,6 +183,12 @@ class Entry(object): ...@@ -180,6 +183,12 @@ class Entry(object):
title = link.get('title') title = link.get('title')
href = link.get('href') href = link.get('href')
resources[title] = href resources[title] = href
inline_resources = []
for entry in link.xpath('m:inline/atom:feed/atom:entry', namespaces=namespaces):
inline_resources.append(Entry(entry, self))
if inline_resources:
self.inline_resources[title] = inline_resources
return resources return resources
def parse_property(self, prop): def parse_property(self, prop):
......
...@@ -9,7 +9,7 @@ requires = [ ...@@ -9,7 +9,7 @@ requires = [
] ]
setup(name='scvmm-client-py', setup(name='scvmm-client-py',
version='0.0', version='0.1',
description='', description='',
author='', author='',
author_email='', author_email='',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment