The sample consists of an EMS package that loads a new resource into the EMS Server provided with RAD Studio. The demo uses FireDAC components to access the Interbase database employee.gdb. The connection parameters are defined in the TFDConnection component. If you experience problems with the connection verify these parameters.
It requires InterBase to be installed on the machine or to connect to a remote server. Make sure that the server is running before you run the example. InterBase is used to store the information about the server connections. You can use the RAD Server Console to display the statistics.
Table of Contents
Location
You can find the APIDocAttributes sample project at:
- Start | Programs | Embarcadero RAD Studio Sydney | Samples and then navigate to either:
- Object PascalDataBaseEMSAPIDocAttributes
- CPPDatabaseEMSAPIDocAttributes
- Subversion Repository:
- You can find Delphi and C++ code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
How to Use the Sample
Run the EMS Package
When you run the APIDocAttributes project, the EMSDevServer starts automatically. If the configuration file is missing, a wizard launches.
- Navigate to the location given above and open:
- Delphi: APIDocDelphiAttributes.dpk
- C++: APIDocCppAttributes.cbproj
- Press Shift+Ctrl+F9 or choose Run > Run Without Debugging.
- The EMS Development Server opens. If it is the first time you are using EMS on the machine, you need to run the configuration wizard in order to create the EMS server configuration file.
- When the Confirm dialog opens, click Yes.
- Leave all the parameters by default and go through the different windows clicking Next.
- Click Finish to close the wizard.
- An Information window appears. Click OK.
- The server starts automatically. Minimize it.
API Doc Attributes
The new EMS package loads a new resource in the EMS Server. The new resource contains four endpoints with their corresponding API documentation in YAML and JSON format.
To access the API documentation:
http://localhost:8080/api/apidoc.yaml
http://localhost:8080/api/apidoc.json
To access the API documentation only for the new resource uploaded:
- Delphi:
http://localhost:8080/api/sampleattributesdelphi/apidoc.yaml
http://localhost:8080/api/sampleattributesdelphi/apidoc.json
- C++:
http://localhost:8080/api/sampleattributescpp/apidoc.yaml
http://localhost:8080/api/sampleattributescpp/apidoc.json
To see the result of the SQL query in JSON format in the browser:
http://localhost:8080/sampleattributesdelphi
http://localhost:8080/sampleattributescpp
Note’: This response is obtained using the Get endpoint method of the new resource.
The structure of the result provided by the server is also provided in the API documentation, in the [EndPointObjectsYAMLDefinitions(Definition_schema)] and [EndPointObjectsJSONDefinitions(Definition_schema)].
Use GetItem to see one specific item of the employee table. To see how to use each specific method, you can access the API documentation provided.
http://localhost:8080/sampleattributesdelphi/{item}
http://localhost:8080/sampleattributescpp/{item}
Note: The GetItem method generates a new SQL query with the item FIRST_NAME provided.
SELECT * FROM EMPLOYEE WHERE FIRST NAME = {item}
For example:
http://localhost:8080/sampleattributesdelphi/Robert
{"FDBS":{"Version":15,"Manager":{"TableList":[{"class":"Table","Name":"EmployeeTable","SourceName":"EMPLOYEE","SourceID":1,"RowList":[{"RowID":0,"Original":{"EMP_NO":2,"FIRST_NAME":"Robert","LAST_NAME":"Nelson","PHONE_EXT":"250","HIRE_DATE":"20071229T000000","DEPT_NO":"600","JOB_CODE":"VP","JOB_GRADE":2,"JOB_COUNTRY":"USA","SALARY":105900,"FULL_NAME":"Nelson, Robert"}}]}]}}}
There are two more methods provided in the sample, Post and PutItem.
Use Post to update an item, and PutItem to add a new item into the employee table.
Implementation
EMS Package
The Resource Module has code to register a REST API resource. The resource is called “sampleattributesdelphi” or “sampleattributescpp”. The resource defines four methods: Get, GetItem, Post, and PutItem.
Once you run the package and have the EMS server running, as a test, you can invoke the GET method using the browser http://localhost:8080/sampleattributesdelphi
.
The TDataModule contains the FireDAC components:
- FireDAC.Comp.Client.TFDConnection establishes the connection to the Interbase database.
- FireDAC.Comp.Client.TFDQuery contains an SQL query: “SELECT * FROM EMPLOYEE”.
- TFDSchemaAdapter is responsible for saving query results to JSON and for loading a JSON delta and applying inserts, updates, and deletions to the database.
Here is some sample source code showing the attributes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
type [ResourceName('SampleAttributesDelphi')] [EndPointObjectsYAMLDefinitions(cYamlDefinitions)] [EndPointObjectsJSONDefinitions(cJSONDefinitions)] TSampleAttributesDelphiResource1 = class(TDataModule) EmployeeConnection: TFDConnection; EmployeeTable: TFDQuery; FDSchemaAdapterEmployees: TFDSchemaAdapter; FDGUIxWaitCursor1: TFDGUIxWaitCursor; FDPhysIBDriverLink1: TFDPhysIBDriverLink; FDStanStorageJSONLink1: TFDStanStorageJSONLink; procedure DataModuleCreate(Sender: TObject); published [EndPointRequestSummary('Sample Tag', 'Summary Title', 'Get Method Description', 'application/json', '')] [EndPointRequestParameter(TAPIDocParameter.TParameterIn.Query, 'skip', 'Query Parameter Description', false, TAPIDoc.TPrimitiveType.spNumber, TAPIDoc.TPrimitiveFormat.Int32, TAPIDoc.TPrimitiveType.spNull, '', '')] [EndPointResponseDetails(200, 'Ok', TAPIDoc.TPrimitiveType.spObject, TAPIDoc.TPrimitiveFormat.None, '', '#/definitions/EmployeeTable')] procedure Get(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse); [ResourceSuffix('{item}')] [EndPointRequestSummary('Sample Tag', 'Summary Title', 'GetItem Method Description', 'application/json', '')] [EndPointRequestParameter(TAPIDocParameter.TParameterIn.Path, 'item', 'Path Parameter item Description', true, TAPIDoc.TPrimitiveType.spString, TAPIDoc.TPrimitiveFormat.None, TAPIDoc.TPrimitiveType.spString, '', '')] [EndPointResponseDetails(200, 'Ok', TAPIDoc.TPrimitiveType.spObject, TAPIDoc.TPrimitiveFormat.None, '', '#/definitions/EmployeeTable')] [EndPointResponseDetails(404, 'Not Found', TAPIDoc.TPrimitiveType.spNull, TAPIDoc.TPrimitiveFormat.None, '', '')] procedure GetItem(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse); [EndPointRequestSummary('Sample Tag', 'Summary Title', 'Post Method Description', 'application/json', 'application/json')] [EndPointRequestParameter(TAPIDocParameter.TParameterIn.Body, 'body', 'Body Param Description', true, TAPIDoc.TPrimitiveType.spObject, TAPIDoc.TPrimitiveFormat.None, TAPIDoc.TPrimitiveType.spObject, '', '#/definitions/PostObject')] [EndPointResponseDetails(200, 'Ok', TAPIDoc.TPrimitiveType.spNull, TAPIDoc.TPrimitiveFormat.None, '', '#/definitions/ItemPostedResponseObject')] [EndPointResponseDetails(409, ' Item Exist', TAPIDoc.TPrimitiveType.spNull, TAPIDoc.TPrimitiveFormat.None, '', '')] procedure Post(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse); [ResourceSuffix('{item}')] [EndPointRequestSummary('Sample Tag', 'Summary Title', 'PutItem Method Description', 'application/json', 'application/json')] [EndPointRequestParameter(TAPIDocParameter.TParameterIn.Path, 'item', 'Path Parameter item Description', true, TAPIDoc.TPrimitiveType.spString, TAPIDoc.TPrimitiveFormat.None, TAPIDoc.TPrimitiveType.spString, '', '')] [EndPointRequestParameter(TAPIDocParameter.TParameterIn.Body, 'body', 'Body Param Description', true, TAPIDoc.TPrimitiveType.spObject, TAPIDoc.TPrimitiveFormat.None, TAPIDoc.TPrimitiveType.spObject, '', '#/definitions/PutObject')] [EndPointResponseDetails(200, 'Ok', TAPIDoc.TPrimitiveType.spObject, TAPIDoc.TPrimitiveFormat.None, '', '#/definitions/ItemPutResponseObject')] [EndPointResponseDetails(404, 'Not Found', TAPIDoc.TPrimitiveType.spNull, TAPIDoc.TPrimitiveFormat.None, '', '')] procedure PutItem(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse); end; |
To get more detailed information or links to other information, please visit the original post of Embarcadero:
http://docwiki.embarcadero.com/CodeExamples/Sydney/en/EMS.APIDocAttributes_Sample
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition