def __getitem__(self, key): sysQueryOptions = {} if self.filter is not None: sysQueryOptions[core.SystemQueryOption.filter] = "%s and %s" % ( core.ODataURI.key_dict_to_query( self.entity_set.key_dict(key)), unicode(self.filter)) entityURL = str(self.baseURI) else: entityURL = ( str(self.baseURI) + core.ODataURI.FormatKeyDict(self.entity_set.GetKeyDict(key))) if self.expand is not None: sysQueryOptions[core.SystemQueryOption.expand] = core.FormatExpand( self.expand) if self.select is not None: sysQueryOptions[core.SystemQueryOption.select] = core.FormatSelect( self.select) if sysQueryOptions: entityURL = uri.URI.from_octets( entityURL + "?" + core.ODataURI.FormatSysQueryOptions(sysQueryOptions)) request = http.ClientRequest(str(entityURL)) if self.filter: request.set_header('Accept', 'application/atom+xml') else: request.set_header('Accept', 'application/atom+xml;type=entry') self.client.process_request(request) if request.status == 404: raise KeyError(key) elif request.status != 200: raise UnexpectedHTTPResponse( "%i %s" % (request.status, request.response.reason)) doc = core.Document(baseURI=entityURL) doc.Read(request.res_body) if isinstance(doc.root, atom.Entry): entity = core.Entity(self.entity_set) entity.exists = True doc.root.GetValue(entity) return entity elif isinstance(doc.root, atom.Feed): nresults = len(doc.root.Entry) if nresults == 0: raise KeyError(key) elif nresults == 1: e = doc.root.Entry[0] entity = core.Entity(self.entity_set) entity.exists = True e.GetValue(entity) return entity else: raise UnexpectedHTTPResponse( "%i entities returned from %s" % nresults, entityURL) elif isinstance(doc.root, core.Error): raise KeyError(key) else: raise core.InvalidEntryDocument(str(entityURL))
def __len__(self): if self.isCollection: return super(NavigationCollection, self).__len__() else: # This is clumsy as we grab the entity itself entityURL = str(self.baseURI) sysQueryOptions = {} if self.filter is not None: sysQueryOptions[core.SystemQueryOption.filter] = unicode( self.filter) if sysQueryOptions: entityURL = uri.URI.from_octets( entityURL + "?" + core.ODataURI.FormatSysQueryOptions(sysQueryOptions)) request = http.ClientRequest(str(entityURL)) request.set_header('Accept', 'application/atom+xml;type=entry') self.client.process_request(request) if request.status == 404: # if we got a 404 from the underlying system we're done return 0 elif request.status != 200: raise UnexpectedHTTPResponse( "%i %s" % (request.status, request.response.reason)) doc = core.Document(baseURI=entityURL) doc.Read(request.res_body) if isinstance(doc.root, atom.Entry): entity = core.Entity(self.entity_set) entity.exists = True doc.root.GetValue(entity) return 1 else: raise core.InvalidEntryDocument(str(entityURL))
def entity_generator(self): feedURL = self.baseURI sysQueryOptions = {} if self.filter is not None: sysQueryOptions[ core.SystemQueryOption.filter] = unicode(self.filter) if self.expand is not None: sysQueryOptions[ core.SystemQueryOption.expand] = core.FormatExpand(self.expand) if self.select is not None: sysQueryOptions[ core.SystemQueryOption.select] = core.FormatSelect(self.select) if self.orderby is not None: sysQueryOptions[ core.SystemQueryOption.orderby] = core.CommonExpression.OrderByToString( self.orderby) if sysQueryOptions: feedURL = uri.URIFactory.URI( str(feedURL) + "?" + core.ODataURI.FormatSysQueryOptions(sysQueryOptions)) while True: request = http.HTTPRequest(str(feedURL)) request.SetHeader('Accept', 'application/atom+xml') self.client.ProcessRequest(request) if request.status != 200: raise UnexpectedHTTPResponse( "%i %s" % (request.status, request.response.reason)) doc = core.Document(baseURI=feedURL) doc.Read(request.resBody) if isinstance(doc.root, atom.Feed): if len(doc.root.Entry): for e in doc.root.Entry: entity = core.Entity(self.entity_set) entity.exists = True e.GetValue(entity) yield entity else: break else: raise core.InvalidFeedDocument(str(feedURL)) feedURL = None for link in doc.root.Link: if link.rel == "next": feedURL = link.ResolveURI(link.href) break if feedURL is None: break
def __getitem__(self, key): if self.isCollection: return super(NavigationCollection, self).__getitem__(key) else: # The baseURI points to a single entity already, we must not add # the key entityURL = str(self.baseURI) sysQueryOptions = {} if self.filter is not None: sysQueryOptions[ core.SystemQueryOption.filter] = unicode(self.filter) if self.expand is not None: sysQueryOptions[ core.SystemQueryOption.expand] = core.FormatExpand( self.expand) if self.select is not None: sysQueryOptions[ core.SystemQueryOption.select] = core.FormatSelect( self.select) if sysQueryOptions: entityURL = uri.URIFactory.URI( entityURL + "?" + core.ODataURI.FormatSysQueryOptions(sysQueryOptions)) request = http.HTTPRequest(str(entityURL)) request.SetHeader('Accept', 'application/atom+xml;type=entry') self.client.ProcessRequest(request) if request.status == 404: raise KeyError(key) elif request.status != 200: raise UnexpectedHTTPResponse( "%i %s" % (request.status, request.response.reason)) doc = core.Document(baseURI=entityURL) doc.Read(request.resBody) if isinstance(doc.root, atom.Entry): entity = core.Entity(self.entity_set) entity.exists = True doc.root.GetValue(entity) if entity.Key() == key: return entity else: raise KeyError(key) elif isinstance(doc.root, core.Error): raise KeyError(key) else: raise core.InvalidEntryDocument(str(entityURL))
def entity_generator(self): if self.isCollection: for entity in super(NavigationCollection, self).entity_generator(): yield entity else: # The baseURI points to a single entity already, we must not add # the key entityURL = str(self.baseURI) sysQueryOptions = {} if self.filter is not None: sysQueryOptions[core.SystemQueryOption.filter] = unicode( self.filter) if self.expand is not None: sysQueryOptions[ core.SystemQueryOption.expand] = core.FormatExpand( self.expand) if self.select is not None: sysQueryOptions[ core.SystemQueryOption.select] = core.FormatSelect( self.select) if sysQueryOptions: entityURL = uri.URI.from_octets( entityURL + "?" + core.ODataURI.FormatSysQueryOptions(sysQueryOptions)) request = http.ClientRequest(str(entityURL)) request.set_header('Accept', 'application/atom+xml;type=entry') self.client.process_request(request) if request.status == 404: return elif request.status != 200: raise UnexpectedHTTPResponse( "%i %s" % (request.status, request.response.reason)) doc = core.Document(baseURI=entityURL) doc.Read(request.res_body) if isinstance(doc.root, atom.Entry): entity = core.Entity(self.entity_set) entity.exists = True doc.root.GetValue(entity) yield entity else: raise core.InvalidEntryDocument(str(entityURL))
def iterpage(self, set_next=False): feedURL = self.baseURI sysQueryOptions = {} if self.filter is not None: sysQueryOptions[core.SystemQueryOption.filter] = unicode( self.filter) if self.expand is not None: sysQueryOptions[core.SystemQueryOption.expand] = core.FormatExpand( self.expand) if self.select is not None: sysQueryOptions[core.SystemQueryOption.select] = core.FormatSelect( self.select) if self.orderby is not None: sysQueryOptions[core.SystemQueryOption. orderby] = core.CommonExpression.OrderByToString( self.orderby) if self.top is not None: sysQueryOptions[core.SystemQueryOption.top] = unicode(self.top) if self.skip is not None: sysQueryOptions[core.SystemQueryOption.skip] = unicode(self.skip) if self.skiptoken is not None: sysQueryOptions[core.SystemQueryOption.skiptoken] = self.skiptoken if sysQueryOptions: feedURL = uri.URI.from_octets( str(feedURL) + "?" + core.ODataURI.FormatSysQueryOptions(sysQueryOptions)) request = http.ClientRequest(str(feedURL)) request.set_header('Accept', 'application/atom+xml') self.client.process_request(request) if request.status != 200: raise UnexpectedHTTPResponse( "%i %s" % (request.status, request.response.reason)) doc = core.Document(baseURI=feedURL) doc.Read(request.res_body) if isinstance(doc.root, atom.Feed): if len(doc.root.Entry): for e in doc.root.Entry: entity = core.Entity(self.entity_set) entity.exists = True e.GetValue(entity) yield entity feedURL = self.nextSkiptoken = None for link in doc.root.Link: if link.rel == "next": feedURL = link.ResolveURI(link.href) break if feedURL is not None: # extract the skiptoken from this link feedURL = core.ODataURI(feedURL, self.client.pathPrefix) self.nextSkiptoken = feedURL.sysQueryOptions.get( core.SystemQueryOption.skiptoken, None) if set_next: if self.nextSkiptoken is not None: self.skiptoken = self.nextSkiptoken self.skip = None elif self.skip is not None: self.skip += len(doc.root.Entry) else: self.skip = len(doc.root.Entry) else: raise core.InvalidFeedDocument(str(feedURL))
def NewEntity(self, autoKey=False): """Returns an OData aware instance""" if self.IsMediaLinkEntryCollection(): return MediaLinkEntry(self.entitySet, self.client) else: return core.Entity(self.entitySet)