Example #1
0
    def _render_single_obj(self, obj):
        """Render a single `DataObject`.
        """
        # Category headers
        category_headers = self.category_headers(obj)
        [self.headers.append(('Category', h)) for h in category_headers.headers()]

        # Link headers
        link_headers = HttpLinkHeaders()
        for link in obj.links + obj.actions:
            params = []
            params.append(('rel',  ' '.join([str(cat) for cat in link.target_categories])))
            params.append(('title',  link.target_title or ''))
            if link.link_location:
                params.append(('self',  link.link_location))
                if link.link_categories and link.link_attributes:
                    params.append(('category', ' '.join([str(cat) for cat in link.link_categories])))
                    for attr, value in link.link_attributes:
                        params.append((attr,  value))

            link_headers.add(link.target_location, params)
        [self.headers.append(('Link', h)) for h in link_headers.headers()]

        # Attribute headers
        attribute_headers = HttpAttributeHeaders()
        [attribute_headers.add(attr, value) for attr, value in obj.attributes]
        [self.headers.append(('X-OCCI-Attribute', h)) for h in attribute_headers.headers()]
Example #2
0
    def _render_single_obj(self, obj):
        """Render a single `DataObject`.
        """
        # Category headers
        category_headers = self.category_headers(obj)
        [
            self.headers.append(('Category', h))
            for h in category_headers.headers()
        ]

        # Link headers
        link_headers = HttpLinkHeaders()
        for link in obj.links + obj.actions:
            params = []
            params.append(
                ('rel', ' '.join([str(cat)
                                  for cat in link.target_categories])))
            params.append(('title', link.target_title or ''))
            if link.link_location:
                params.append(('self', link.link_location))
                if link.link_categories and link.link_attributes:
                    params.append(
                        ('category',
                         ' '.join([str(cat) for cat in link.link_categories])))
                    for attr, value in link.link_attributes:
                        params.append((attr, value))

            link_headers.add(link.target_location, params)
        [self.headers.append(('Link', h)) for h in link_headers.headers()]

        # Attribute headers
        attribute_headers = HttpAttributeHeaders()
        [attribute_headers.add(attr, value) for attr, value in obj.attributes]
        [
            self.headers.append(('X-OCCI-Attribute', h))
            for h in attribute_headers.headers()
        ]
Example #3
0
    def parse(self, headers=None, body=None):
        categories = []
        links = []
        attributes = []
        locations = []

        # Walk list of HTTP header name-value pairs
        for name, value in headers or ():
            name = name.lower()

            if name == 'accept':
                self._parse_accept_header(value)
            elif name == 'category':
                categories.extend(self._parse_category_header(value))
            elif name == 'link':
                # FIXME - not allowing Link create/update using POST/PUT yet
                pass
            elif name == 'x-occi-attribute':
                attribute_headers = HttpAttributeHeaders()
                attribute_headers.parse(value)
                attributes.extend(attribute_headers.all())
            elif name == 'x-occi-location':
                location_headers = HttpHeadersBase()
                location_headers.parse(value)
                locations.extend(location_headers.all())

        # Only possible to represent one "full" data object with HTTP Headers.
        # Multiple data objects can only be represented with a location.
        locations = locations or [None]
        self.objects.append(DataObject(
            categories=categories,
            links=links,
            attributes=attributes,
            location=locations[0]))
        for loc in locations[1:]:
            self.objects.append(DataObject(location=loc))