api_class
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
api_class [2020/09/04 05:41] – sahl | api_class [2021/02/11 13:35] (current) – API query and handling files sahl | ||
---|---|---|---|
Line 47: | Line 47: | ||
...so be sure that those are implemented properly. | ...so be sure that those are implemented properly. | ||
+ | |||
+ | ===== Endpoint security ===== | ||
+ | |||
+ | By default an endpoint requires a valid access token to use. This access token should be passed in a cookie named '' | ||
+ | |||
+ | This security can be removed with the '' | ||
+ | |||
+ | <code php> | ||
+ | // This disables the endpoint security. | ||
+ | $api_endpoint-> | ||
+ | </ | ||
+ | |||
+ | ===== Custom functionality ===== | ||
+ | |||
+ | Custom functionality can be added to an api endpoint, so it can be used to do more than manipulate Datarecord objects. In order to do so, one can subclass the '' | ||
+ | |||
+ | In order to make API answers, the function '' | ||
+ | |||
+ | In this example we construct an object called // | ||
+ | |||
+ | <code php> | ||
+ | public function customerHandlerBeforeSecurity($object_name, | ||
+ | // Only react on correct object | ||
+ | if ($object_name == ' | ||
+ | // Check if other parameters are correct | ||
+ | if ($object_id) $this-> | ||
+ | if ($method != ' | ||
+ | // Validate get input | ||
+ | if (! $get[' | ||
+ | if (! $get[' | ||
+ | // Try to make a login | ||
+ | $accesstoken = User:: | ||
+ | // Fail if the login was invalid | ||
+ | if ($accesstoken === false) $this-> | ||
+ | // Create a response | ||
+ | $result = array( | ||
+ | ' | ||
+ | ); | ||
+ | // Respond | ||
+ | self:: | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Handling file fields in the API ===== | ||
+ | During normal operation the API will just return the ID of the file in file fields, but by passing the // | ||
+ | |||
+ | ^filename|The filename of the file| | ||
+ | ^mimetype|The mime type of the file| | ||
+ | ^binary|The file binary data as base64| | ||
+ | |||
+ | Please be aware that this can produce substantial output in some cases, and is therefore only recommended for using when querying for a specific object. | ||
+ | |||
+ | A similar JSON structure can be used when posting to the API to update files. Just add another field to the JSON structure: | ||
+ | |||
+ | ^action|add to add a file and remove to remove the file currently in place.| | ||
+ | |||
+ | If //remove// is passed the other fields are not necessary. | ||
+ | |||
+ | ===== Querying the API ===== | ||
+ | The API endpoints can be queried by passing a JSON-structure in the //query// GET parameter. The query can consist of either a comparison statement or logical statement. | ||
+ | |||
+ | ==== Comparison statement ==== | ||
+ | |||
+ | A comparison statement always consists of three fields: //type//, // | ||
+ | |||
+ | The following comparison types exists: | ||
+ | |||
+ | ^Type^Meaning^ | ||
+ | |Greater|Return everything where // | ||
+ | |GreaterEqual|Return everything where // | ||
+ | |Lesser|Return everything where // | ||
+ | |LesserEqual|Return everything where // | ||
+ | |Like|Return everything where //value// is contained in // | ||
+ | |Match|Return everything where // | ||
+ | |OneOf|Return everything where // | ||
+ | |||
+ | ==== Logical statement ==== | ||
+ | |||
+ | Logical statements are used to combine or negate other statements. They consists of the field: //type// and some additional fields depending on the type. | ||
+ | |||
+ | ^type^additional fields^Meaning^ | ||
+ | |AND|// | ||
+ | |OR|// | ||
+ | |NOT|// | ||
+ | |||
+ | ==== Examples ==== | ||
+ | Find all data where age is equal or greater than 18 | ||
+ | <code javascript> | ||
+ | {" | ||
+ | </ | ||
+ | |||
+ | Find all data where age is less than 40 and gender is male | ||
+ | <code javascript> | ||
+ | {" | ||
+ | </ | ||
+ | |||
+ | Find all data where firstname contains the letter a | ||
+ | <code javascript> | ||
+ | {" | ||
+ | </ | ||
+ | |||
+ | Find all data changed since a specific time stamp | ||
+ | <code javascript> | ||
+ | {" | ||
+ | </ |
api_class.1599198067.txt.gz · Last modified: 2020/09/04 05:41 by sahl