- Transaction Manager for Microservices Developer Guide
- Develop Applications with TCC
- Develop Python Apps with TCC
- Configure Python App as Transaction Initiator
8.7.1 Configure Python App as Transaction Initiator
You can select Flask or Django as the framework for your Python application. This section provides instructions to integrate the MicroTx library with the application code of your Python application with Flask framework.
- Open a terminal in the virtual environment that you have created for your Python application, and then run the following command to install the MicroTx library file for Python which is available in the
installation_directory/otmm-<version>/lib/python
folder.pip3 install tmmpy-<version>.whl
- Configure the property values for the MicroTx library. Create a new file and save it as
tmm.properties
. You must provide values for the following properties.The following example provides sample values for the properties. Provide the values based on your environment.
oracle.tmm.TcsUrl = http://tmm-app:9000/api/v1 oracle.tmm.PropagateTraceHeaders = true server.port = 8080 oracle.tmm.CallbackUrl = http://localhost:{server.port}
For details about each property, see Configure Library Properties.
Note down the name and location of this file as you will have to provide this later when you initialize the
tccConfig
object. - Import the MicroTx libraries and exceptions. You can use
tcclib.exception
to handle exceptions.from tcclib.tcc import TCCClient, Middleware, http_request, TCCConfig import tcclib.exception as ex
- Create a Flask instance with middleware. Middleware helps to intercept all the incoming requests received by the Flask instance.The following sample code creates a Flask instance and a middleware object.
# Create a Flask instance with the name of the current module. app = Flask(__name__) # Middleware helps to intercept all the incoming requests received by the Flask application. app.wsgi_app = middleware(app.wsgi_app)
- Add the following code to initialize the
tccConfig
object for the microservice.Syntax
tccConfig = TCCConfig(filePath=<application_properties_file_path>, timeLimitInSeconds=<integer>)
Sample
tccConfig = TCCConfig(filePath="./tmm.properties", timeLimitInSeconds=300)
Where,
./tmm.properties
is the location of the file in which you have previously defined values for the MicroTx library properties for the transaction initiator service.300
is the time limit in seconds for the transaction initiator service to reserve the resources. Specify the time period as a whole number. It is the responsibility of the application developer to provide the required code to release the resources and cancel the their part of the TCC transaction after the time limit expires. Decide the time limit based on your business requirement.
Replace these values with the values specific to your environment.
- The TCC transaction protocol relies on the basic
HTTP
verbs:POST
,PUT
, andDELETE
. You must expose the REST API endpoints for each HTTP method and map these endpoints to a specific function that executes the business logic. Your application code already contains the business logic to make a new reservation and confirm or cancel the reservation. Use theapp.route
decorator to bind a function in your application to a HTTP verb and URL path.In the following code sample for a transaction initiator service, the service exposes the REST API endpoints for the different HTTP verbs.
//Mandatory. The transaction initiator service must use the //
POST
HTTP method to create a new reservation. @app.route('/travel-agent/api/bookings/reserve', methods=['POST']) def do_trip_reserve(): //app-specific code to create a booking //Mandatory. Use the PUT HTTP method to confirm a reservation. @app.route('/travel-agent/api/confirm/<trip_booking_id>', methods=['PUT']) def do_trip_confirm(trip_booking_id): //app-specific code to confirm the specified booking ID //Mandatory. Use the DELETE HTTP method to cancel a reservation. @app.route('/travel-agent/api/cancel/<trip_booking_id>', methods=['DELETE']) def do_trip_cancel(trip_booking_id): //app-specific code to delete the specified booking IDWhere,
/travel-agent/api/bookings/reserve
,/travel-agent/api/confirm/<trip_booking_id>
, and/travel-agent/api/cancel/<trip_booking_id>
are the REST API endpoints that the transaction initiator service exposes. Ensure that these endpoints are present in the transaction initiator service and the confirm and cancel logic is implemented in the code.do_trip_reserve()
,do_trip_confirm()
, anddo_trip_cancel()
methods contain the business logic for creating a reservation, confirming a reservation, canceling a reservation respectively. Ensure that the business logic is implemented in the code of the transaction initiator service and the endpoints are present.
Parent topic: Develop Python Apps with TCC