def testComplexLinks(self): xrd = XRD('9876') xrd.properties.append(('mimetype', 'text/plain')) # This tests Issue #2 lnk = Link(rel='http://spec.example.net/photo/1.0', type_='image/jpeg', href='http://photos.example.com/gpburdell.jpg') lnk.properties.append(('http://spec.example.net/created/1.0', '1970-01-01')) xrd.links.append(lnk) xrd.to_xml()
class TestXRDSerialization(unittest.TestCase): def setUp(self): self.xrd = XRD('9876') self.xrd.properties.append(('mimetype', 'text/plain')) self.xrd.properties.append('none') self.xrd.links.append(Link(template="http://google.com/{uri}")) self.doc = self.xrd.to_xml().documentElement def testxmlid(self): self.assertEqual(self.doc.getAttribute('xml:id'), '9876') def testproperty(self): prop = self.doc.getElementsByTagName('Property')[0] self.assertEqual(prop.getAttribute('type'), 'mimetype') self.assertEqual(_get_text(prop), 'text/plain') def testnilproperty(self): prop = self.doc.getElementsByTagName('Property')[1] self.assertEqual(prop.getAttribute('type'), 'none') self.assertEqual(prop.getAttribute('xsi:nil'), 'true') self.assertTrue(_get_text(prop) is None) def testlink(self): link = self.doc.getElementsByTagName('Link')[0] self.assertEqual(link.getAttribute('template'), "http://google.com/{uri}")
def get_host_meta(): """ This function services the well-known host-meta XRD data for RESTCONF API root discovery. """ if args.verbose > 0: print 'get_host_meta: entry' xrd_obj = XRD() # Add a few extra elements and links before RESTCONF to help make sure # the parsing/XPATH is correct xrd_obj.elements.append(Element('hm:Host', 'testDevice')) xrd_obj.links.append(Link(rel='license', href='http://www.apache.org/licenses/LICENSE-2.0')) xrd_obj.links.append(Link(rel='author', href='http://bcsw.net')) # Add the link for RESTCONF xrd_obj.links.append(Link(rel='restconf', href=args.root_resource)) # Add some extra links here as well xrd_obj.links.append(Link(rel='testPath', href='this/does/not/exist')) xrd_obj.links.append(Link(rel='http://oexchange.org/spec/0.8/rel/resident-target', type_='application/xrd+xml', href='http://twitter.com/oexchange.xrd')) # Convert to XML, pretty-print it to aid in debugging xrd_doc = xrd_obj.to_xml() return Response(xrd_doc.toprettyxml(indent=' '), mimetype='application/xrd+xml')
class XRDResponse(HttpResponse): def __init__(self, subject=None, **kwargs): from django.conf import settings content_type = 'text/plain' if settings.DEBUG else 'application/xrd+xml' super(XRDResponse, self).__init__(content_type=content_type, **kwargs) self._xrd = XRD() def __iter__(self): content = self._xrd.to_xml().toxml() self._iterator = iter((content),) return self
class XRDResponse(HttpResponse): def __init__(self, subject=None, **kwargs): content_type = 'application/xrd+xml' super(XRDResponse, self).__init__(content_type=content_type, **kwargs) self._xrd = XRD() def __iter__(self): content = self._xrd.to_xml() xml = content.toprettyxml(indent=' ') expr = re.compile(r'>\n\s+([^<>\s].*?)\n\s+</', re.DOTALL) content = expr.sub(r'>\g<1></', xml) self._iterator = iter((content), ) return self
class XRDResponse(HttpResponse): def __init__(self, subject=None, **kwargs): content_type = 'application/xrd+xml' super(XRDResponse, self).__init__(content_type=content_type, **kwargs) self._xrd = XRD() def __iter__(self): content = self._xrd.to_xml() xml = content.toprettyxml(indent=' ') expr = re.compile(r'>\n\s+([^<>\s].*?)\n\s+</', re.DOTALL) content = expr.sub(r'>\g<1></', xml) self._iterator = iter((content),) return self
def get_host_meta(): """ This function services the well-known host-meta XRD data for RESTCONF API root discovery. """ if args.verbose > 0: print 'get_host_meta: entry' xrd_obj = XRD() # Add a few extra elements and links before RESTCONF to help make sure # the parsing/XPATH is correct xrd_obj.elements.append(Element('hm:Host', 'testDevice')) xrd_obj.links.append( Link(rel='license', href='http://www.apache.org/licenses/LICENSE-2.0')) xrd_obj.links.append(Link(rel='author', href='http://bcsw.net')) # Add the link for RESTCONF xrd_obj.links.append(Link(rel='restconf', href=args.root_resource)) # Add some extra links here as well xrd_obj.links.append(Link(rel='testPath', href='this/does/not/exist')) xrd_obj.links.append( Link(rel='http://oexchange.org/spec/0.8/rel/resident-target', type_='application/xrd+xml', href='http://twitter.com/oexchange.xrd')) # Convert to XML, pretty-print it to aid in debugging xrd_doc = xrd_obj.to_xml() return Response(xrd_doc.toprettyxml(indent=' '), mimetype='application/xrd+xml')
class BaseHostMeta: def __init__(self, *args, **kwargs): self.xrd = XRD() def render(self): return self.xrd.to_xml().toprettyxml(indent=" ", encoding="UTF-8")
class BaseHostMeta(object): def __init__(self, *args, **kwargs): self.xrd = XRD() def render(self): return self.xrd.to_xml().toprettyxml(indent=" ", encoding="UTF-8")