OPA Hub - REST API Generate Class

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.

{
    "name": "Example",
    "description": "Description of Example Project",
    "compatibilityMode": "latest",
    "services":
    [
        "interview",
        "webservice",
        "mobile",
        "javaScriptSessions"
    ],
    "collections":
    [
        "Default Collection"
    ],
    "versions":
    {
        "items":
        [
            {
                "description": "The initial version",
                "activeVersionFlag": true,
                "snapshot":
                {
                    "base64": "...base64 value..."
                }
            }
        ]
    }
}

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.

OPA Hub - Deployment REST API Generate Class

How easy is that?

  1. Choose your language
  2. Give your new Class a name
  3. Paste the sample from the Oracle REST API documentation
  4. Press this button
  5. 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

  1. We have Class to model our request
  2. We have a function to use HTTPWebRequest send things to the Oracle Policy Automation Hub
  3. 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!