Package: contentful.py
Python SDK forContentful’s Content Delivery API.SetupInstallation viapip:pipinstallcontentful.pyUsageCreating a ClientTheClientclass manages all your interaction with the Contentful Delivery API, creating one is as simple as:client=Client('space-id','access-token')Fetching ResourcesThe simplest form of fetching resources follows:client.fetch(Asset).all()# Returns an array of Assetsclient.fetch(Asset).first()# Returns the first Asset availableclient.fetch(Entry).all()# Returns an array of Entriesclient.fetch(Entry).first()# Returns the first Entry availableclient.fetch(ContentType).all()# Returns an array of Content Typesclient.fetch(ContentType).first()# Returns the first Content Type availableWhen used with theall()method, expect anArrayobject to be returned, which is iterable:forresourceinarray:dosomething(resource)and sliceable:forresourceinarray[2:4]:dosomething(resource)Custom QueriesCustom queries can be provided by invoking thewhere()method of aRequestobject, for example:client.fetch(Entry).where({'sys.id':'MyEntry'}).first()Defining ModelsCustom Entry classes can be declared by providing a Content Type ID value and by defining a set of fields. Consider theCatContent Type from the official demo space, which looks like this (sans some properties for brevity):{"fields":[{"id":"name","name":"Name","type":"Text"},{"id":"likes","name":"Likes","type":"Array","items":{"type":"Symbol"}},{"id":"color","name":"Color","type":"Symbol"},{"id":"bestFriend","name":"Best Friend","type":"Link","linkType":"Entry"},{"id":"birthday","name":"Birthday","type":"Date"},{"id":"lives","name":"Lives left","type":"Integer"},{"id":"image","name":"Image","type":"Link","linkType":"Asset"}],"name":"Cat","displayField":"name","description":"Meow."}A custom Entry class for this Content Type can be defined as follows:classCat(Entry):__content_type__='cat'name=Field(Text)likes=Field(List)color=Field(Text)best_friend=Field(Link,field_id='bestFriend')birthday=Field(Date)lives=Field(Number)image=Field(Link)The class also has to be registered when creating aClient:client=Client('cfexampleapi','b4c0n73n7fu1',custom_entries=[Cat])Whenever thisClientwill attempt to create anEntrywith a Content Type that matches the one defined in the class, an instance of theCatclass will be created, and it’s fields will be set accordingly, and can later be easily accessed as instance attributes:print('Name of the Cat:{0}'.format(cat.name))If at any point it is desired to fetch only resources of that type, it can be passed to thefetch()method and inferred by the client:client.fetch(Cat).all()# Fetches all the Cats!Link ResolutionUnless specified otherwise, aClientis configured to resolve links automatically when fetching resources.
Be mindful about providing theincludeparameter properly, since only if the linked resources are contained within the response they will be resolved automatically. In case a link is not resolved, expect aResourceLinkvalue, which can be resolved by invoking theresolve_resource_link()method of aClient(issues a network request per the resource ID).Automatic link resolution:array=client.fetch(Cat).all()print(array[0].best_friend)# <Cat(sys.id=nyancat)>Manual link resolution:cat=client.fetch(Cat).where({'sys.id':'nyancat','include':0}).first()print(cat.best_friend)# <contentful.cda.resources.ResourceLink object at 0x1030df390>print(client.resolve_resource_link(cat.best_friend))# <Cat(sys.id=nyancat)>LicenseCopyright (c) 2015 Contentful GmbH. SeeLICENSE.txtfor further details.