Deployment REST API : Uploading Zip Files
Following on from a post in the not too distant past, where we looked sideways at the Deployment REST API and the ability to download the Zip File of a deployment snapshot programatically, this new post answers a request from one of our readers regarding the “other side of the coin” ; specifically how to take a Zip File that needs to be uploaded into an Oracle Policy Automation Hub, and get the file to load up into the Deployments, using code. I must thank the commenter on the previous article for sharing his question, since it is one that comes up quite a lot and this is a good opportunity to hopefully show how you might do it.
The starting point of the process is that you have previously downloaded a snapshot file from a Deployments page of your Hub in environment A, and you now want to copy it across to environment B.
With your Zip File in hand, what can you do to automate this process? Of course, you turn your attention to the REST API and the deployment REST API in particular. Find the page that matches your version and let’s have a look.
How to get a class out of that? If only I could find a way to turn that into something easy to use in Visual Studio, I could create my own…wait a minute! Enter JSONUtils – the perfect site for converting JSON into C# or Visual Basic Classes.
How easy is that?
- Choose your language
- Give your new Class a name
- Paste the sample from the Oracle REST API documentation
- Press this button
- There’s your class, ready to use in your Visual Studio project.
Armed with this, we are ready to continue our research. We also need to be able to POST our request to the Oracle Policy Automation Hub. Thankfully somebody else has already asked about POSTing JSON to a specific URL millions of times, and there is a good starter function on Stack Exchange. At the bottom of that question is a great example.
Checklist – REST API Deployment
We can step back a moment and review a checklist
- We have Class to model our request
- We have a function to use HTTPWebRequest send things to the Oracle Policy Automation Hub
- We have an integration user and password, and the url of the Oracle Policy Automation Hub
So what else do we need? We need to be able to turn the Zip File into an encoded string as per the example given on the Oracle Policy Automation website : something like this:
Public Function ConvertFileToBase64(ByVal fileName As String) As String Return Convert.ToBase64String(System.IO.File.ReadAllBytes(fileName)) End Function
In the second part of this post, we will put it all together and see the result!