Creating Power Automate / OpenAPI Extensions for Azure Functions (Fusion Development)

So, what is this “Fusion Development” you ask? Fusion development allows your business to build better applications, faster, by bringing together professional developers with amateur or hobbyist developers. If you’re not familiar with fusion development, have a look at the Microsoft Learning Path  – Transform your business applications with fusion development.

In this post, we will explore how professional developers and amateur developers can work together to address a real life scenario with Power Automate.

Scenario –

In this post we will consume the Muhimbi SOAP service in an Azure Microsoft .NET Core function (as it can easily handle those SOAP webservices through WCF by creating service references) that can further be invoked using a Custom Connector in Power Automate, PowerApps.

Why would you want to add all this complexity to your life, especially as a perfectly good Online REST based API is available at little to no cost?  Well, in this post, I aim to show you the flexibility of The Muhimbi PDF Converter and the numerous, perhaps not obvious, ways it can be used.  As the IT sphere becomes more complex, with a mix of on-premise and online solutions all working together, a basic cookie-cutter solution just doesn’t cut it anymore.  Solutions need to be flexible and able to adapt to the unique environment that exists around each deployment.

One of the reasons you may want to take this approach is for regulatory compliance, as in some industries, using 3rd party web services violates compliance requirements. The approach in this blog post allows you to control the entire process, software and hardware, end-to-end.

The end-to-end application architecture looks like:

Lets try to understand the diagram from Right to Left:

Back End –

Virtual Machine Configuration –

  • Install and configure two Windows Server or Virtual Machines.
  • Download the fully functional free trial of The Muhimbi PDF Converter Services here.
  • Please make sure The Muhimbi PDF Converter Services  is installed exactly as described in chapter 2 of the Administration Guide.  Please follow that guide to the letter and make sure you pay particular good attention to the section about ‘dependencies’.  The Administration Guide is included in the download and available on-line here.

Configuring the Load Balancer –

Creating Azure Functions OpenAPI Extension from SOAP Web Services –

Front End –

Custom Connector and Power Automate –

  • A custom connector is generated by the OpenAPI document.
  • Through the custom connector, the Power Automate will send the conversion request to the  backend system.
  • The backend system Converts the document to PDF and send the response back to Power Automate.

Now that you understand the Application Architecture let’s improve the API –

In my post, Creating Azure Functions OpenAPI Extension from SOAP Web Services we exposed our OpenAPI(Swagger UI).

The Swagger UI URL would be https://<function_app_name>.azurewebsites.net/api/swagger/ui

[FunctionName("ConvertToPDF")]
[OpenApiOperation(operationId: "ConvertToPDF")]
[OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "code", In = OpenApiSecurityLocationType.Query)]
[OpenApiRequestBody("application/json", typeof(RequestBodyModel), Description = "JSON request body containing { FileName, FileContent}")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(ResponseBodyModel), Description = "JSON OK response")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.BadRequest, contentType: "application/json", bodyType: typeof(ResponseBodyModel), Description = "JSON BAD response")]

  • On line 3 we use the “OpenApiSecurity” as “function_key” which would help us protect Backend API.
  • On line 5  and 6 we use “OpenApiRequestBody” and “OpenApiResponseWithBody” of contentType “application/json” and bodyType “object”. Note: In Power Automate Microsoft recommends passing the “File Content” as byte[] but as of now the OpenApiRequestBody does not have “byte” type properties (issue link) . 

We will work around the problem by editing the Swagger manually in the step below. You find the blog and download the code at link.

Editing the swagger to accommodate “format”: “byte”:

Now this step is a workaround because of an “Open issue (link)” as “OpenApiRequestBody does not have “byte” type properties”.

The workaround would be download the Swagger file from URL ” https://<function_app_name>.azurewebsites.net/api/openapi/v2.json”  and to manually add “format”: “byte” in the “definitions” section and “Save” the file.

Note: Currently, the custom connector only supports the OpenAPI spec v2.

Create a custom connector from an OpenAPI definition:

  • Import the OpenAPI definition for Power Automate and Power Apps
  • Go to  flow.microsoft.com.
  • In the navigation pane, select Data > Custom connectors.
  • Select custom connector.
  • Select New custom connector, then choose Import your OpenAPI file .
  • Import an OpenAPI file.
  • Enter a name for the custom connector, then navigate to the OpenAPI definition that you downloaded or created, and choose Continue.

On the General page, review the information that was imported from the OpenAPI definition, including the API host and the base URL for the API. The connector uses the API host and the base URL to determine how to call the API.

  • On the Security page, review authentication type. Our APIs use API key(Function) authentication, so that’s what’s specified in the OpenAPI definition.
  • The Definition page of the custom connector wizard gives you a lot of options for defining how your connector functions, and how it is exposed in logic apps, flows, and apps. Note: You can also Edit your Swagger using the “Swagger Editor” but as we have already edited the “Swagger” click on next.
  • Skip the Code Review Step.
  • Now that you’ve created the connector, test it to make sure it’s working properly. Note: When using an API key, we recommend not testing the connector immediately after you create it. It can take a few minutes until the connector is ready to connect to the API. You can access your function API Key by following the steps mentioned at “Access Azure Function App host key“.
  • On the Test page, choose New connection.
  • Enter the API key for the Azure, then choose Create connection.
  • Return to the Test page, for the Operations set the “ConvertToPDF to “Raw Body On”, paste the JSON below, and click on ‘Test operation’.
  • You will get a JSON response that looks like the following example:

Accessing Custom Connector in Power Automate to Convert Documents to PDF using our Customer connector:

Step 1 : Open the Power Automate / Flow designer, create a Flow, find the “When a file is created or modified (properties only)” trigger. 

  • This trigger will allow you to start the Flow by adding a file into a folder or when modifying a property of a file already in the folder. Naturally other triggers can be used as well (email attachments, drop box, OneDrive, you name it).

Step 2: Add the SharePoint “Get file content” action to the Flow Canvas and configure it with reference to the details below:

  • Site Address: Specify the path to the SharePoint Online site-collection which holds the file to be converted.
  • File Identifier: “Identifier” the output from the “When a file is created or modified (properties only)” action.

Step 3: Add our Custom “Convert Files to PDF” to the Flow Canvas.

  • fileName:  “File name with Extension” the output of the “When a file is created or modified (properties only)” action.
  • fileContent: “File Content” the output of the “Get File Content” action.

Step 4: Use the “Create file” SharePoint action to write the generated PDF document to the output document library.

  • File name: Use the “Name“.pdf the output of  “When a file is created or modified (properties only)” action .
  • File Content: “Processed file content” is the output variable of the  Custom “Convert Files to PDF” action.

That’s it- you’re done!  Publish your Flow and upload a supported file type to the folder that is monitored by the configured SharePoint trigger. After a short wait, you can find the PDF in the output folder.

Over the two blog post we have seen how both amateur developers and professional developers can work together within a fusion team –

  • Amateur developer makes a Power Automate, and Automate day to functionalities
  • Professional developer can provide discoverable APIs.

Although in this example we are using Muhimbi’s SOAP based web service, the concept is exactly the same for solutions from other vendors.

Subscribe to this blog for the latest updates about SharePoint Online, Power Automate (Microsoft Flow), Power Apps and document conversion and manipulation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s