Cloud DevOps Cookbook Part 4 — API Gateway and DirectMail with Python 3.0

By John Hanley, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

My favorite language for rapid prototyping and learning a new cloud service is Python. Python makes most tasks so simple that I can test a new API in minutes. This allows me to focus on learning and not on the details of how an API works in a particular language. However, for enterprise level code where a mistake could cost millions of dollars, I use C++.

The Alibaba SDK for DirectMail does not support Python. It does support PHP, Java and C#. A very common use case for DirectMail is delivering emails for dynamic and static websites. For dynamic websites PHP is perfect. For static websites, integrating Function Compute with DirectMail is perfect. However, Function Compute does not support PHP. This means that you develop in one language on your desktop and another in the cloud.

This led me to the idea of using the DirectMail REST API. This means that I can use Python and learn how to use the REST API and in particular how to create the Signature that is not documented very well. Nothing like picking a difficult challenge to really learn a new service.

DirectMail supports three types of interfaces (not all are available in all languages):

  1. Alibaba REST API
  2. Alibaba Cloud SDK
  3. Alibaba SMTP Interface

In the previous article, I showed an example of using the SDK (Function Compute) in Python. In this article I will focus on the DirectMail REST API and include a real working example in Python.

Alibaba Cloud DirectMail REST API

There are several reasons to use the REST API:

  1. Understand the low level api, parameters and errors.
  2. Smaller code space.
  3. Faster load and execution time.
  4. Fewer dependencies.
  5. Not require that the SDK libraries be installed on the target system.
  6. The SDK is not available in your target language.

REST API Requirements:

  1. You need to understand HTTPS, HTTP headers and HTTP bodies.
  2. Understand the differences between HTTP HEAD, DELETE, GET, POST and PUT methods.
  3. Understand how to send data for each HTTP method.
  4. Understand the API parameters that must be included with each HTTP method.
  5. Understand how to sign the HTTP request.

Alibaba Cloud Public Parameters

Link to Alibaba documentation on DirectMail Public parameters.

Comments on the above parameters:

SignatureNonce: This parameter had me guessing for a while. I assumed at first that this was a salt value used during HMAC_SHA1 signing. Turns out that this is a string that is created by uuid.uuid4() and included in the headers. If you repeat this string value in a subsequent command, the command will be rejected.

AccessKeyId: Create a new RAM user with permissions just for DirectMail. You will then need both the Access Key and the Access Key Secret for REST API.

Timestamp: Is important that your system’s date and time are correct. If this time is different from the Alibaba service, then the request will be rejected. If possible use a time service such as NTP for your system.

Alibaba Cloud DirectMail Request Parameters

Link to Alibaba documentation on DirectMail Request parameters.

Calculating the correct dates

The Host and Endpoint for this example (Singapore → ap-southeast-1)

Building the parameter list for Public parameters

Building the parameter list for Request parameters

Build the request string for the signing process

Notes on the request string:

The request string parameters must be sorted first. Then the string is url encoded. This means that each key/value pair is appended with the & character.

The final result looks like this example but much longer:

AccessKeyId=LTAIQlgy6erobert& ...

Create the Signature from the Request String

Link to Alibaba documentation on DirectMail Signature.

Complete Program to Call DirectMail REST API and Send an Email


Execute with Python 3.x: python

Alibaba Documentation

Alibaba DirectMail Product Page
Alibaba DirectMail Documentation
Alibaba DirectMail Public parameters
Alibaba DirectMail Request parameters
Alibaba DirectMail Signature


Follow me to keep abreast with the latest technology news, industry insights, and developer trends.