class UTT_APImanagementConnection
{
Str tokenValue, contentType, requestMethod;
RecId apiToken;
url url;
KeyVaultCertificateTable keyVaultCertificateTable;
public static UTT_APImanagementConnection construct()
{
UTT_APImanagementConnection aPImanagementConnection = new UTT_APImanagementConnection();
return aPImanagementConnection;
}
/// Creates the request with the header
public System.Net.HttpWebRequest createRequestWithHeaders()
{
System.Net.HttpWebRequest request;
CLRObject clrObj;
System.Exception ex;
System.IO.Stream requestStream;
System.Net.WebHeaderCollection httpHeader;
try
{
System.Security.Cryptography.X509Certificates.X509Certificate2 certificate= KeyVaultCertificateHelper::getDigitalCertificate(this.parmAPIToken());
new InteropPermission(InteropKind::ClrInterop).assert();
httpHeader = new System.Net.WebHeaderCollection();
httpHeader.Add('KeyVault', any2Str(Certificate));
clrObj = System.Net.WebRequest::Create(this.parmURL());
request = clrObj;
request.set_KeepAlive(true);
request.set_ContentType(this.parmContentType());
request.set_Method(this.parmRequestMethod());
request.set_Headers(httpHeader);
if (this.parmRequestMethod() != 'GET')// Other than the GET method we need RequestStream to process the request
{
requestStream = request.GetRequestStream();
requestStream.Close();
}
return request;
}
catch (Exception::CLRError)
{
ex = ClrInterop::getLastException();
if (ex != null)
{
ex = ex.get_InnerException();
if (ex != null)
{
throw error(ex.ToString());
}
}
throw Exception::CLRError;
}
}
public static str readResponseData(System.Net.HttpWebResponse response)
{
int batchSize = 1024;
System.IO.Stream receiveStream;
System.IO.StreamReader readStream;
System.Text.Encoding encode;
System.Char[] read;
System.Text.StringBuilder sb;
System.String readString;
str contentEncoding;
int countRead;
if (response == null)
{
return "";
}
receiveStream = response.GetResponseStream();
contentEncoding = response.get_ContentEncoding();
if (contentEncoding)
{
encode = System.Text.Encoding::GetEncoding(contentEncoding);
}
else
{
encode = new System.Text.UTF8Encoding();
}
readStream = new System.IO.StreamReader(receiveStream, encode);
read = new System.Char[batchSize]();
countRead = readStream.Read(read, 0, batchSize);
sb = new System.Text.StringBuilder();
while (countRead > 0)
{
readString = new System.String(read, 0, countRead);
sb.Append(readString);
countRead = readStream.Read(read, 0, batchSize);
}
readStream.Close();
return sb.ToString();
}
public url parmURL(url _url = url)
{
url = _url;
return url;
}
public Str parmContentType(Str _contentType = contentType)
{
contentType = _contentType;
return contentType;
}
public Str parmRequestMethod(Str _requestMethod = requestMethod)
{
requestMethod = _requestMethod;
return requestMethod;
}
public RecId parmAPIToken(RecId _apiToken = apiToken)
{
apiToken = _apiToken;
return apiToken;
}
}
-------------------------------------------------------------------------------------------------------
class UTT_AzureKeyCustomAPICall
{
System.Reflection.TargetInvocationException ex;
System.Net.WebException ex1;
System.Net.HttpWebRequest request;
KeyVaultCertificateTable keyVaultCertificateTable;
str contentType = 'application/json';
str requestMethod = 'GET';
str responseStr;
URL url;
System.Net.HttpWebResponse response;
public static void main(Args _args)
{
UTT_AzureKeyCustomAPICall customAPICall = new UTT_AzureKeyCustomAPICall();
customAPICall.run(_args);
}
public void run(Args _args)
{
this.sendRequest(_args);
responseStr = UTT_APImanagementConnection::readResponseData(response);
if (response.StatusCode == System.Net.HttpStatusCode::OK)
{
info("Done");
}
}
public void sendRequest(Args _args)
{
try
{
select firstonly keyVaultCertificateTable
where keyVaultCertificateTable.RecId == 123456789;//Assign appropriate RecId to select the required keyvault
url = "URL Details";//Add the API URL
UTT_APImanagementConnection aPImanagementConnection= UTT_APImanagementConnection::construct();
aPImanagementConnection.parmURL(url);
aPImanagementConnection.parmContentType(contentType);
aPImanagementConnection.parmRequestMethod(requestMethod);
aPImanagementConnection.parmApiToken(keyVaultCertificateTable.RecId);
System.Net.ServicePointManager::SecurityProtocol = System.Net.SecurityProtocolType::Tls12 |System.Net.SecurityProtocolType::Tls11 | System.Net.SecurityProtocolType::Tls;
request = aPImanagementConnection.createRequestWithHeaders();
response = request.GetResponse();
}
catch (Exception::CLRError)
{
ex = ClrInterop::getLastException();
if (ex != null)
{
ex1 = ex.get_InnerException();
if (ex1 != null)
{
error(ex1.ToString());
}
}
}
}
}
No comments:
Post a Comment