client_class
Differences
This shows you the differences between two versions of the page.
client_class [2021/04/26 13:14] – created sahl | client_class [2022/05/24 10:33] (current) – sahl | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Client Class ====== | ====== Client Class ====== | ||
- | The Api class exposes any Datarecord object in a RESTful API interface. This is extremely simple to setup: | ||
- | <code php> | ||
- | $api_endpoint = new Api(array(' | ||
- | $api_endpoint-> | ||
- | </ | ||
- | |||
- | ==== Preselecting an instance ==== | ||
- | |||
- | For normal use, a user using the provided API interface would have to specify an instance ID in the URL. If you don't want that (maybe because your application only consists of a single instance), you can preselect an instance like this: | ||
- | |||
- | <code php> | ||
- | $api_endpoint = new Api(array(' | ||
- | $api_endpoint-> | ||
- | $api_endpoint-> | ||
- | </ | ||
- | |||
- | |||
- | |||
- | ===== Using the provided endpoint ===== | ||
- | |||
- | The endpoint extends itself from the script containing the code, and have the following path / | ||
- | |||
- | So if you expose a class '' | ||
- | |||
- | https:// | ||
- | |||
- | **PRO TIP:** If you have preset an instance id for the endpoint, this shouldn' | ||
- | |||
- | If you want to retrieve the object with ID 24, then call the URL: | ||
- | |||
- | https:// | ||
- | |||
- | To update the object, just POST to the same URL and to create a new object POST to the general URL above. One does not have to POST a complete object. Only the fields mentioned in the POST will be updated. The other fields will be left untouched. POST data should consist of a JSON-object similar to the one received when querying the API. | ||
- | |||
- | To delete an object make a call to DELETE | ||
- | |||
- | The API makes heavy use of the following functions in Datarecord to ensure proper operation: | ||
- | |||
- | * canAccess() | ||
- | * canCreate() | ||
- | * canDelete() | ||
- | * canEdit() | ||
- | * validateObject() | ||
- | |||
- | ...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> | ||
- | {" | ||
- | </ |
client_class.1619442890.txt.gz · Last modified: 2021/04/26 13:14 by sahl