Package: opvoeden-api-client
A client for thestichtingopvoeden.nlAPI version 2.UsageImport the client class fromopvoeden_api.clientand instantiate
it with an API key:client = Client(MY_API_KEY)The client methods map to the following set of endpoints:MethodEndpointcontentset_list/rest/v2/contentsetcontentset(contentset_id)/rest/v2/contentset/{id}article(external_reference)/rest/v2/article/{externalReference}image(image_id)/rest/v2/image/{id}NoteThecontentsetmethod does not support thechangedAfterorarticleparameters that are documented in the API docs.Data typesEach client method returns a different data type.ContentSetThecontentset_listmethod returns alistofContentSetobjects.AContentSetobject has of the following properties:contentset_idnamedescriptionis_defaultRefer to theAPI data types docsfor more information on
these fields.ArticleNodeThecontentset(contentset_id)method returns a singleArticleNode.
This node represents the root of a tree ofArticles.AnArticleNodehas the following properties:articleTheArticleinstance associated with this nodechildrenAlistofArticleNodeinstances of which the current node
is the parent.It is possible to iterate over an article node. This will traverse the
entire tree in a depth first order. So, for example, to get a flat list
of all the articles in a tree one could do this:tree = client.contentset(1)
articles = [node.article for node in tree if node.article]To recurse over the tree use the children property:def visit(node, parent=None):
    """
    Visit each node in the tree.
    """
    # do something with node, then recurse
    for child in node.children:
        visit(child, node)

tree = client.contentset(1)
visit(tree)ArticleThearticle(external_reference)method returns a singleArticleinstance. Another way to get anArticleobject is by accessing
thearticleproperty of anArticleNode.AnArticlehas the following properties:external_referenceshort_titletitlearticle_textparent_referencepositionlast_change_datecanonicaltagtagsRefer to theAPI data types docsfor more information on
these fields.NoteThearticle_textofArticlesretrieved from thecontentsetendpoint can contain several placeholder strings.
This library provides a number ofutilitiesto deal with those.In addition to these fields theArticleobject also
provides these properties:pathThe url of the article. This is identical to thecanonicaltagbut theschema://domainprefix is stripped.slugThe last element of the path. i.e. ifpathis'/foo/bar/'thenslugwill be'bar'.ImageTheimage(image_id)method returns a singleImageinstance.image_iddatacontent_typenamecreation_dateRefer to theAPI data types docsfor more information on
these fields.Converting image data to binaryTheImageobject also provides anas_binarymethod.This method converts to base64 encoded value of thedataproperty to binary. The return value of this method can be used
to store images on a file system.Article utilsThearticle_textofArticlesreturned by thecontentsetmethod can contain a number special placeholder strings.opvoeden_api.article_utilsprovides functions to deal with
these placeholders.Replace JGZ placeholdersTo replaceJGZ placeholderswith appropriate strings usereplace_jgz.By default these are the substitutions:PlaceholderSubstitutionjgzcentrum voor Jeugd en Gezin (CJG)JgzCentrum voor Jeugd en Gezin (CJG)jgzsCJG’sJgzsCJG’sde jgzsde CJG’sDe jgzsDe CJG’shet jgzhet Centrum voor Jeugd en Gezin (CJG)Het jgzHet Centrum voor Jeugd en Gezin (CJG)To override any of the substitutions use the optionalsubstitutionsargument toreplace_jgzi.e.:replace_jgz(article_text, substitutions={
    'jgz': 'centrum voor Jeugd en Gezin'
})Replace internal link placeholdersTo replaceinternal link placeholdersusereplace_linkswith a replacement callback.The replacement callback is called with theexternal_idandlink_textfor each placeholder in the article text.If the replacement callback returns anything other thanNonethe link is replaced with the return value.For example:external_id_to_href = {
    '1': '/example/',
    '2': '/example/more/'
}

def get_link(external_id, link_text):
    """
    Get the url for an article and return an HTML snippet
    that links to this url with the given text.

    """
    href = external_id_to_href.get(external_id, None)
    if href:
        return '<a href="{}">{}</a>'.format(href, link_text)

replace_links(article_text, get_link)Replace image placeholdersTo replaceimage placeholdersusereplace_imageswith a replacement callback.The replacement callback is called with theimage_idfor each placeholder in the article text.If the replacement callback returns anything other thanNonethe placeholder is replaced with the return value.For example:image_id_to_src = {
    '1': '/media/1.gif',
    '2': '/media/2.gif'
}

def get_image_tag(image_id):
    src = image_id_to_src.get(image_id, None)
    if src:
        return '<img src="{}">'.format(src)HintThe replacement callback is an excellent place call the
image endpoint of the API.Replace video placeholdersTo replaceYouTube video placeholdersusereplace_videoswith a replacement callback.The replacement callback is called with thevideo_id,embed_urlandexternal_urlfor each placeholder in the article text.If the replacement callback returns anything other thanNonethe placeholder is replaced with the return value.Some examples:def get_video_embed(video_id, embed_url, external_url):
    """Create an iframe to embed the video"""
    return '<iframe src="{}">'.format(embed_url)


def get_video_link(video_id, embed_url, external_url):
    """Create a link to the video player on opvoeden.nl"""
    return '<a href="{}" target="_blank">Watch the video</a>'.format(
        external_url)Changesv2.1.0Expose article tagsv2.0.1Fix YouTube url regexv2.0.0Use API version 2v1.0.0Initial release