Update API

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

301
API.md

@ -1,155 +1,148 @@
## 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](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
#### 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);
```
#### JavaScript
```
<!DOCTYPE html>
<html>
<head>
<script>
function pandora_api(action, data, callback) {
var url = 'http://archive.arabdigitalexpression.org/api/';
$.ajax({
type: "POST",
url: url,
data: {action: action, data: JSON.stringify(data)},
success: callback,
dataType: 'json'
});
}
function pandora_api(action, data, callback) {
var url = 'http://archive.arabdigitalexpression.org/api/';
var xhr = new XMLHttpRequest();
xhr.addEventListener('load', function (evt) {
var data = JSON.parse(evt.target.responseText);
callback(data);
});
var formData = new FormData();
formData.append('action', action);
formData.append('data', JSON.stringify(data));
xhr.open('POST', url);
xhr.send(formData);
}
pandora_api('find', {query: ....}, function(result) {console.log(result)});
</script>
</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) {
console.log(api);
});
});
</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})
## 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](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
#### 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);
```
#### JavaScript
```
<!DOCTYPE html>
<html>
<head>
<script>
function pandora_api(action, data, callback) {
var url = 'http://archive.arabdigitalexpression.org/api/';
var xhr = new XMLHttpRequest();
xhr.addEventListener('load', function (evt) {
var data = JSON.parse(evt.target.responseText);
callback(data);
});
var formData = new FormData();
formData.append('action', action);
formData.append('data', JSON.stringify(data));
xhr.open('POST', url);
xhr.send(formData);
}
pandora_api('find', {query: ....}, function(result) {
console.log(result)
});
</script>
</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})
```