10 API
j edited this page 2024-06-22 10:14:56 +00:00

API

[Documents, pandora*, 0xdb2*)]TOC(heading=Design

Documentation

Each instance of Pan.do/ra comes with its build in API documentation. You find it by navigation to /api/ with a browser. I.e. current API can be found at http://0xdb.org/api/

Using the API

To use the API you can use Ox.App if you use Ox.js, or look at pandora_client for how this can be used from python. Eventually we hope to describe this section in more detail.

Examples

JavaScript

<!DOCTYPE html>
<html>
    <head>
        <script>
            async function api(action, data) {
                const url = "https://pad.ma/api/"
                var response = await fetch(url, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        action: action,
                        data: data
                    })
                })
                return await response.json();
            }


            api('find', {
                query: {
                    conditions: [
                        {key: 'title', operator: '=', value: 'A Day'}
                    ]
                },
                keys: ['id', 'title', 'summary'],
                range: [0, 10]
            }).then(result => {
                result.data.items.forEach(item => {
                    const div = document.createElement("div")
                    div.innerHTML = `<h1><a href="https://pad.ma/m/${item.id}/player">${item.title}</a></h1><div><div><img src="https://pad.ma/${item.id}/128p.jpg"></div><div>${item.summary}</div></div>`
                    document.body.appendChild(div)
                })
            })
        </script>
        <style>
            html {
              max-width: 70ch;
              padding: 3em 1em;
              margin: auto;
              line-height: 1.75;
              font-size: 1.25em;
              font-family: sans-serif;
            } 
            img {
                object-fit: contain;
            }
            div > div {
                display: flex;
                gap: 8px;
            }
            @media screen and (max-width: 799px){
                div > div {
                    flex-direction: column;
                }
            }
        </style>
    </head>
    <body>
    </body>
</html>

Or using OxJS

<!DOCTYPE html>
<html>
    <head>
<script src="https://oxjs.org/build/Ox.js"></script>
<script>
Ox.load({
    UI: {
        loadCSS: false
    }
}, function() {
	Ox.API({
	    url: "https://0xdb.org/api/"
	}, function(api) {
        api.find( {query: ....}, function(result) {
               console.log(result)
        });
	});
});
</script>
    <body>
    </body>
</html>

Python

Using python-ox (install with easy_install ox)

import ox

api = ox.API('http://archive.arabdigitalexpression.org/api/')

item = api.find({'query':{conditions:[{'key': '*', 'value':'test', 'operator':'='}], 'operator':'&'}, 'keys': ['location']})

Add all items in list 'j:Example' to groups a, b and c

#!/usr/bin/python
import ox

username=ADD_USER
password=ADD_PASSWORD
groups = ['a', 'b', 'c']
list_id = 'j:Example'

api = ox.API('http://kjc-sv030.kjc.uni-heidelberg.de/api/')
api.signin(username=username, password=password)
for i in api.find({'query': {
    'conditions': [{'key': 'list', 'value': list_id}]},
    'keys': ['id', 'groups'],
    'range': [0, 10000]
})['data']['items']:
    if i['groups'] != groups:
        print id
        api.edit({'id': i['id'], 'groups': groups}) 

PHP

<?php

class API {

    function __construct($url) {
        $this->url = $url;
    }
    public function _request($action, $data) {
        $content = array(
            'action' => $action,
            'data' => json_encode($data)
        );
        $options = array(
            'http' => array(
                'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
                'method'  => 'POST',
                'content' => http_build_query($content),
            ),
        );
        $context  = stream_context_create($options);
        $result = json_decode(file_get_contents($this->url, false, $context));
        return $result;
    }
}
l
$api = new API('http://archive.arabdigitalexpression.org/api/');

$result = $api->_request('find', array(
    "query"=>array(
        "conditions"=>array(array("key"=>"*", "value"=>"paris", "operator"=>'='))
    ),
    "keys"=>array("title", 'id'),
    "range"=> array(0, 10),
    "sort"=>array(array("key"=>"title", "operator"=>"+"))
));

var_dump($result);