
Overview
The ApiCall
represents an API request object, allowing Perfecto Server to interact with external APIs via REST calls. It supports both sending (upload) and receiving (download) data, as well as token-based authentication.
Key Features
- Supports GET and POST/PUT requests.
- Schedules API calls using CRON expressions.
- Handles token-based authentication using Bearer tokens.
- Allows dynamic and static parameter handling.
- Supports mapping API responses to database fields.
- Includes support for sub-records in API responses.
1. Fields and Properties
1.1 General Properties
- Name:
- A required field to define the name of the API call.
- TokenCall:
- Another
ApiCall
object responsible for generating an access token if authentication is needed. Token calls must be ofDataDirection.Tokenization
.
- Another
- ScheduleType:
- Indicates if the API call is scheduled or on-demand. For scheduled tasks, a CRON expression is provided.
- CRONSchedule:
- A CRON expression defining the frequency of the API call. This is hidden if
ScheduleType
is set toOnDemand
.
- A CRON expression defining the frequency of the API call. This is hidden if
- Active:
- A boolean field determining if the API call is active and should be executed.
- Method:
- Defines the HTTP method (
GET
,POST
,PUT
, etc.) for the API call.
- Defines the HTTP method (
- Direction:
- Specifies whether the call is for downloading (receiving data from the API), uploading (sending data), or tokenization (for authentication purposes).
1.2 URL and Queries
- URL:
- The API endpoint URL (required).
- ParametersSelectionQuery:
- SQL query used to select dynamic parameters when making API calls.
- UpdateQuery:
- SQL query to update the database with the API response data.
- SubrecordsUpdateQuery:
- Similar to
UpdateQuery
, but for handling sub-records (nested records in API responses).
- Similar to
1.3 Database Mapping
- SelectSourceTable / SelectTargetTable:
- Defines the source or target tables for data interaction.
- RetreivedRecordIdField:
- Defines the primary key field used for identifying records in the database.
- Mapping:
- A collection of mappings between API fields and database fields. This includes static values, dynamic values, and queries to perform data transformations.
1.4 Result Handling
- ResultType:
- Specifies the structure of the API response, whether it’s an object, list, or string.
- ResultedFieldKey:
- Key used to extract a specific field from the API response if the result is structured.
2. Making an API Call
2.1 Reading from API (GET Request)
To read data from an API using this ApiCall
, follow these steps:
- Define the API Call:
- In the
URL
field, specify the API endpoint from which you want to retrieve data. - Set the
Method
property toGET
. - If the API requires an access token, define a
TokenCall
.
- In the
- Configure Parameters (Optional):
- If your API requires query parameters, you can add them in the
Parameters
section of the UI. - Use the
ParametersSelectionQuery
if parameters are dynamically fetched from a database.
- If your API requires query parameters, you can add them in the
- Define the Result Handling:
- Set the
ResultType
based on how the API response is structured (Object, List, String). - Provide the
ResultedFieldKey
if you need to extract a specific field from the API response (e.g., an ID field).
- Set the
- Database Mapping (Optional):
- Define how the API data should be mapped to your database tables using the
SelectSourceTable
orSelectTargetTable
. - Use the
Mapping
section to map API response fields to database columns.
- Define how the API data should be mapped to your database tables using the
- Schedule or Trigger:
- Schedule the call using
CRONSchedule
or leave it for manual execution.
- Schedule the call using
- Execution:
- When the API call is executed, it will:
- Fetch the data from the API.
- Parse the response using the provided mappings.
- Insert or update the database using the generated
UpdateQuery
.
- When the API call is executed, it will:
2.2 Sending Data to an API (POST/PUT Request)
To send data to an API:
- Define the API Call:
- Set the
URL
field to the API endpoint where the data will be sent. - Set the
Method
toPOST
orPUT
based on the API.
- Set the
- Configure Parameters:
- Use the
ParametersSelectionQuery
to dynamically generate data that will be sent to the API. - Add the parameters to the
Parameters
section. Each parameter can be a static or dynamic value.
- Use the
- Token Call (Optional):
- If authentication is required, use the
TokenCall
property to retrieve an access token.
- If authentication is required, use the
- Result and Update Handling (Optional):
- If you expect a response from the API, define the result handling and mapping similar to a GET request.
- Execution:
- When the API call is executed, it will:
- Send the data to the API endpoint.
- If the call is successful, it can store the response and update the database accordingly.
- When the API call is executed, it will:
3. Handling Tokens
If the API requires token-based authentication:
- Create a Token API Call:
- Set the
Direction
toTokenization
. - Define the URL to fetch the token.
- Set the
ResultedFieldKey
to extract the token from the API response (usually in theaccess_token
field).
- Set the
- Attach the Token Call:
- In the primary API call, reference this
TokenCall
. - The system will automatically use the token for authentication when making the request.
- In the primary API call, reference this
4. CRON Scheduling
- Use the
CRONSchedule
field to automate the API calls. For example,"0 0/30 0 ? * * *"
will trigger the call every 30 minutes. - If you want the call to be triggered manually or by an external event, set the
ScheduleType
toOnDemand
.
5. Error Handling
If the API call fails or encounters an error, the Log
field will store the error message, and you can inspect it to troubleshoot the problem.
Conclusion
The ApiCall
in your Perfecto Server module is a powerful tool for integrating external APIs. By configuring the fields for URLs, parameters, and result mappings, you can effectively communicate with APIs, automate requests, and manage the data received or sent. The combination of tokenization, CRON scheduling, and database mapping makes this system versatile for various use cases.
Example 1: Sending Data to a Well-Known API (POST Request to Slack API)
Scenario: You want to send a message to a Slack channel using Slack’s Incoming Webhook API.
Step-by-Step Guide:
- Create the Slack API Call:
- URL: Use Slack’s webhook URL for your workspace. Example:
https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
. - Method: Set the
Method
toPOST
. - Direction: Set
Direction
toUploadOnly
because you are sending data. - Parameters:
- Add a parameter for the message body. The parameter should be JSON-formatted, as Slack requires the message to be sent as JSON.
- The JSON body could look like this:jsonCopy code
{ "text": "Hello, this is a message from Perfecto Server." }
- The message can be static or dynamic, depending on how you want to configure it.
- URL: Use Slack’s webhook URL for your workspace. Example:
- Setup in the ApiCall:
- Name:
SendSlackMessage
- URL:
https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
- Method:
POST
- Parameters:
- Key:
message
- Value:jsonCopy code
{ "text": "Hello, this is a message from Perfecto Server." }
- Parameter Type:
JsonBody
- ValueType:
StaticValue
(you can change this to dynamic based on your requirements).
- Key:
- Active: Ensure this API call is active.
- Name:
- Execute the API Call:
- Once the API call is configured, you can execute it manually to send the message to your Slack channel.
- Expected Outcome:
- If successful, the message should appear in the designated Slack channel. The
Log
field in the Perfecto Server should show a success response, confirming that the API call was executed correctly.
- If successful, the message should appear in the designated Slack channel. The
Example 2: Fetching Records from a Well-Known API (GET Request to OpenWeather API)
Scenario: You want to retrieve weather data for a specific city using the OpenWeather API.
Step-by-Step Guide:
- Create the OpenWeather API Call:
- URL: Use OpenWeather’s endpoint for fetching weather data. Example:
https://api.openweathermap.org/data/2.5/weather
. - Method: Set the
Method
toGET
. - Direction: Set
Direction
toDonwloadOnly
because you are fetching data. - Parameters:
- OpenWeather requires parameters such as:
q
: The city name (e.g.,"Cairo"
).appid
: Your API key from OpenWeather.
- These parameters should be included as query parameters in the request.
- OpenWeather requires parameters such as:
- URL: Use OpenWeather’s endpoint for fetching weather data. Example:
- Setup in the ApiCall:
- Name:
FetchWeatherData
- URL:
https://api.openweathermap.org/data/2.5/weather
- Method:
GET
- Parameters:
- Key:
q
- Value:
Cairo
(You can change the city dynamically based on user input or a database). - Parameter Type:
URL
- ValueType:
StaticValue
(or dynamic if needed).
- Value:
- Key:
appid
- Value:
YourOpenWeatherAPIKey
(replace with your API key). - Parameter Type:
URL
- ValueType:
StaticValue
- Value:
- Key:
- ResultType:
Object
(because the response is expected to be a single JSON object). - ResultedFieldKey: Use this if you want to extract specific data from the response (e.g.,
main.temp
for temperature).
- Name:
- Map the Response to Database Fields (Optional):
- If you want to store the data in your database, use the
Mapping
section to map the response fields to database columns. For example:- Map the
main.temp
(temperature) field to a column in your database for weather data. - Map the
weather[0].description
field to another column for storing the weather description.
- Map the
- If you want to store the data in your database, use the
- Execute the API Call:
- After setting up, execute the API call to fetch the weather data for the selected city (Cairo in this case).
- Expected Outcome:
- If successful, the API call will return weather data in JSON format, and it will be displayed in the log. If you’ve set up database mapping, the data will also be saved in your database.
Summary of Examples for Your Documentation:
Example 1: Sending Data to Slack API (POST)
- Purpose: Send a message to a Slack channel.
- URL:
https://hooks.slack.com/services/your-webhook-url
- Method:
POST
- Parameters: JSON body with the message text.
- Expected Outcome: A message appears in the Slack channel.
Example 2: Fetching Data from OpenWeather API (GET)
- Purpose: Retrieve weather data for a city.
- URL:
https://api.openweathermap.org/data/2.5/weather
- Method:
GET
- Parameters:
q
(city),appid
(API key). - Expected Outcome: Weather data is fetched and displayed or stored.
These examples can be adapted for other APIs by adjusting the URL, parameters, and expected response structures. By following the same process, you can integrate with various APIs for sending or receiving data as needed.