models.py 文件源码

python
阅读 16 收藏 0 点赞 0 评论 0

项目:pyfc4 作者: ghukill 项目源码 文件源码
def get_resource(self, uri, resource_type=None, response_format=None):

        '''
        return appropriate Resource-type instance
            - issue HEAD request, determine Resource type (BasicContainer, DirectContainer, IndirectContainer, or NonRDFSource (Binary))
            - issue GET request and retrieve resource metadata at uri/fcr:metadata

        Args:
            uri (rdflib.term.URIRef,str): input URI
            resource_type (): resource class e.g. BasicContainer, NonRDFSource, or extensions thereof
            response_format (str): expects mimetype / Content-Type header such as 'application/rdf+xml', 'text/turtle', etc.

        Returns:
            Resource
        '''

        # handle uri
        uri = self.parse_uri(uri)

        # remove fcr:metadata if included, as handled below
        if uri.toPython().endswith('/fcr:metadata'):
            uri = rdflib.term.URIRef(uri.toPython().rstrip('/fcr:metadata'))

        # HEAD request to detect resource type
        head_response = self.api.http_request('HEAD', uri)

        # 404, item does not exist, return False
        if head_response.status_code == 404:
            logger.debug('resource uri %s not found, returning False' % uri)
            return False

        # assume exists, parse headers for resource type and return instance
        elif head_response.status_code == 200:

            # if resource_type not provided
            if not resource_type:
                # parse LDP resource type from headers
                resource_type = self.api.parse_resource_type(head_response)
            logger.debug('using resource type: %s' % resource_type)

            # fire GET request
            get_response = self.api.http_request(
                'GET',
                "%s/fcr:metadata" % uri,
                response_format=response_format)

            # return resource
            return resource_type(self,
                uri,
                response=get_response)

        else:
            raise Exception('HTTP %s, error retrieving resource uri %s' % (head_response.status_code, uri))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号