Handling Responses
When you execute tasks with Fiscus, you’ll need to handle the responses that come back. Whether it's a simple success message or detailed JSON data, Fiscus has you covered.
Let’s dive into handling text and JSON responses and how to work with the FiscusResponse object to make your life easier!
Text vs. JSON Responses
Depending on the nature of your task, you might want different response formats. Fiscus allows you to choose between simple text or structured JSON responses.
Text Responses
For straightforward tasks, such as sending an email or creating a contact, a text response might be all you need.
- Python
Example: Text Response
- Execute a Simple Task
Let's say you're sending an email. The response will be a simple success message.
response = client.execute('Gmail', 'send_email', email_params)
- Check the Response
In this case, you're just looking for a simple success or failure message.
if response.success:
print(response.result) # Output: "Email sent successfully!"
else:
print(f'Error: {response.error_message}')
JSON Responses
When you need more detailed information—like API data from external services—JSON responses give you structured data to work with.
How to Set JSON Responses
Fiscus provides a handy enum FiscusResponseFormat
that you can use to specify the response format, making it super easy to switch between text and JSON.
-
Import the Enum
First, import the
FiscusResponseFormat
from the SDK:
from fiscus import FiscusResponseFormat
- Request Detailed Data
Let’s say you’re creating a new contact in your CRM and want the full details back.
response = client.execute('CRMSystem', 'create_contact', contact_params, response_format=FiscusResponseFormat.JSON)
- Handle JSON Data
You’ll get a full JSON object with all the relevant details.
if response.success:
print(response.to_json()) # Output: {'id': '12345', 'name': 'John Doe', 'email': 'john@example.com'}
else:
print(f'Error: {response.error_message}')
Pro Tip: Switch Between Formats
You can toggle between text and JSON responses easily by using the response_format
parameter when making the API call. Keep it flexible by leveraging the FiscusResponseFormat
enum!
Working with FiscusResponse Object
The FiscusResponse object is your friend. It’s packed with useful properties and methods that make handling responses a breeze.
Key Properties:
- success: Boolean value indicating if the operation was successful.
- result: The actual response data (text or JSON).
- error_message: If something goes wrong, this will hold the error details.
- status_code: The HTTP status code returned by the API (useful for debugging).
- headers: Any headers returned by the API.
- logs: Logs for deeper insights into what happened during the task.
Example: Exploring the FiscusResponse Object
Let’s see how you can use the FiscusResponse object to extract all the goodness you need.
- Execute a Task
We'll send a request to fetch some user details from an API.
response = client.execute('UserAPI', 'get_user_details', user_params)
- Access the Response Properties
Now, let’s pull out the details using the FiscusResponse object.
if response.success:
print(f"User Name: {response.result['name']}")
print(f"Email: {response.result['email']}")
else:
print(f"Error Code: {response.status_code}")
print(f"Error Message: {response.error_message}")
Common Methods in FiscusResponse
- to_json(): Converts the response to a JSON object for easy access.
- Example:
response.to_json()
- Example:
- get_header(key): Retrieve a specific header from the response.
- Example:
response.get_header('Content-Type')
- Example:
- get_logs(): Fetch logs generated during the task execution for debugging.
- Example:
response.get_logs()
- Example:
Pro Tips:
- Always check the success property before accessing the response data.
- Use logs for deep debugging when things don’t go as planned.
- FiscusResponse gives you a clear, easy-to-navigate structure to handle even the most complex responses efficiently.
With Fiscus, handling responses—whether text or JSON—is simple, structured, and efficient. Ready to handle your data like a pro? Let’s keep building awesome things together!