Update API

j 2024-06-22 08:16:30 +00:00
parent 017c028c7d
commit 75b6118b9f

301
API.md

@ -1,155 +1,148 @@
## API ## API
[Documents, pandora*, 0xdb2*)]TOC(heading=Design [Documents, pandora*, 0xdb2*)]TOC(heading=Design
### Documentation ### Documentation
Each instance of Pan.do/ra comes with its build in API documentation. You find it by navigation to /api/ with a browser. 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/> I.e. current API can be found at <http://0xdb.org/api/>
### Using the API ### Using the API
To use the API you can use Ox.App if you use Ox.js, or look at [pandora_client](https://wiki.0x2620.org/browser/pandora_client/pandora_client/*init*.py#L331) for how this can be used from python. Eventually we hope to describe this section in more detail. To use the API you can use Ox.App if you use Ox.js, or look at [pandora_client](https://wiki.0x2620.org/browser/pandora_client/pandora_client/*init*.py#L331) for how this can be used from python. Eventually we hope to describe this section in more detail.
### Examples ### Examples
#### PHP #### PHP
``` ```
<?php <?php
class API { class API {
function __construct($url) { function __construct($url) {
$this->url = $url; $this->url = $url;
} }
public function _request($action, $data) { public function _request($action, $data) {
$content = array( $content = array(
'action' => $action, 'action' => $action,
'data' => json_encode($data) 'data' => json_encode($data)
); );
$options = array( $options = array(
'http' => array( 'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST', 'method' => 'POST',
'content' => http_build_query($content), 'content' => http_build_query($content),
), ),
); );
$context = stream_context_create($options); $context = stream_context_create($options);
$result = json_decode(file_get_contents($this->url, false, $context)); $result = json_decode(file_get_contents($this->url, false, $context));
return $result; return $result;
} }
} }
l l
$api = new API('http://archive.arabdigitalexpression.org/api/'); $api = new API('http://archive.arabdigitalexpression.org/api/');
$result = $api->_request('find', array( $result = $api->_request('find', array(
"query"=>array( "query"=>array(
"conditions"=>array(array("key"=>"*", "value"=>"paris", "operator"=>'=')) "conditions"=>array(array("key"=>"*", "value"=>"paris", "operator"=>'='))
), ),
"keys"=>array("title", 'id'), "keys"=>array("title", 'id'),
"range"=> array(0, 10), "range"=> array(0, 10),
"sort"=>array(array("key"=>"title", "operator"=>"+")) "sort"=>array(array("key"=>"title", "operator"=>"+"))
)); ));
var_dump($result); var_dump($result);
``` ```
#### JavaScript #### JavaScript
``` ```
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<script> <script>
function pandora_api(action, data, callback) { function pandora_api(action, data, callback) {
var url = 'http://archive.arabdigitalexpression.org/api/'; var url = 'http://archive.arabdigitalexpression.org/api/';
$.ajax({ var xhr = new XMLHttpRequest();
type: "POST", xhr.addEventListener('load', function (evt) {
url: url, var data = JSON.parse(evt.target.responseText);
data: {action: action, data: JSON.stringify(data)}, callback(data);
success: callback, });
dataType: 'json' var formData = new FormData();
}); formData.append('action', action);
} formData.append('data', JSON.stringify(data));
xhr.open('POST', url);
function pandora_api(action, data, callback) { xhr.send(formData);
var url = 'http://archive.arabdigitalexpression.org/api/'; }
var xhr = new XMLHttpRequest();
xhr.addEventListener('load', function (evt) { pandora_api('find', {query: ....}, function(result) {
var data = JSON.parse(evt.target.responseText); console.log(result)
callback(data); });
}); </script>
var formData = new FormData(); </head>
formData.append('action', action); <body>
formData.append('data', JSON.stringify(data)); </body>
xhr.open('POST', url); </html>
xhr.send(formData); ```
}
Or using OxJS
pandora_api('find', {query: ....}, function(result) {console.log(result)}); ```
</script> <!DOCTYPE html>
</head> <html>
<body> <head>
</body> <script src="https://oxjs.org/build/Ox.js"></script>
</html> <script>
``` Ox.load({
UI: {
Or using OxJS loadCSS: false
``` }
<!DOCTYPE html> }, function() {
<html> Ox.API({
<head> url: "https://0xdb.org/api/"
<script src="https://oxjs.org/build/Ox.js"></script> }, function(api) {
<script> api.find( {query: ....}, function(result) {
Ox.load({ console.log(result)
UI: { });
loadCSS: false });
} });
}, function() { </script>
Ox.API({ <body>
url: "https://0xdb.org/api/" </body>
}, function(api) { </html>
console.log(api);
}); ```
});
</script> #### Python
<body>
</body> Using python-ox (install with **easy_install ox**)
</html>
```
``` import ox
#### Python api = ox.API('http://archive.arabdigitalexpression.org/api/')
Using python-ox (install with **easy_install ox**) item = api.find({'query':{conditions:[{'key': '*', 'value':'test', 'operator':'='}], 'operator':'&'}, 'keys': ['location']})
``` ```
import ox
Add all items in list 'j:Example' to groups a, b and c
api = ox.API('http://archive.arabdigitalexpression.org/api/') ```
#!/usr/bin/python
item = api.find({'query':{conditions:[{'key': '*', 'value':'test', 'operator':'='}], 'operator':'&'}, 'keys': ['location']}) import ox
``` username=ADD_USER
password=ADD_PASSWORD
Add all items in list 'j:Example' to groups a, b and c groups = ['a', 'b', 'c']
``` list_id = 'j:Example'
#!/usr/bin/python
import ox api = ox.API('http://kjc-sv030.kjc.uni-heidelberg.de/api/')
api.signin(username=username, password=password)
username=ADD_USER for i in api.find({'query': {
password=ADD_PASSWORD 'conditions': [{'key': 'list', 'value': list_id}]},
groups = ['a', 'b', 'c'] 'keys': ['id', 'groups'],
list_id = 'j:Example' 'range': [0, 10000]
})['data']['items']:
api = ox.API('http://kjc-sv030.kjc.uni-heidelberg.de/api/') if i['groups'] != groups:
api.signin(username=username, password=password) print id
for i in api.find({'query': { api.edit({'id': i['id'], 'groups': groups})
'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})
``` ```