I am working on an API project the pulls from a API Rest using SSIS. This is a personal project and I do not have the ability to purchase a 3rd party software.
I have been working with C# to pull from the API as I know that it is capable of doing it, but I do not have the C# experience to be able to finish it.
Can anyone help me get the final touches on the code to pull back from the API? All the variables are objects, and the ExportDestination variable is just a file path for the export.
Here are the API docs if you need them: http://api.sc2replaystats.com/docs/index.html
Code :
#region Help:
Introduction to the script task
#endregion
#region Namespaces
using System;
using System.Net;
using System.Text;
using System.IO;
#endregion
namespace ST_32488ae1d3e348ca83a1017e2e2bc39f
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain :
Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public string Authorization { get; private set; }
#region Help: Using Integration Services variables and parameters in a script
#endregion
#region Help: Firing Integration Services events from a script
#endregion
#region Help: Using Integration Services connection managers in a script
#endregion
public void Main()
{
string errorLogPath = Dts.Variables["User::ErrorLog"].Value.ToString();
// TODO: Add your code here
try
{
string apiExportResponse = Dts.Variables["User::ExportDestination"].Value.ToString();
string strResponse = SC2REPLAY("http://api.sc2replaystats.com/player/search");
File.WriteAllText(apiExportResponse, strResponse);
}
catch (Exception ex)
{
ErrorLogging(ex, errorLogPath);
Dts.Variables["User::APIReadFullResponse"].Value = ex.Message.ToString();
}
Dts.TaskResult = (int)ScriptResults.Success;
}
public string SC2REPLAY(string Config)
{
var request = (HttpWebRequest)WebRequest.Create(Config);
var encoding = new UTF8Encoding();
Authorization += "8c517814830a1d19d7b39c3c3a0ac65e914bbbaf;d54f614345505a473b8ed71665666251c3061460;1560313648";//The API token specific to your REDCap project (each token is unique to each user for each project)
var postData = "&players_name=" + "PartinG";
//postData += "&players_name=" + "PartinG";
byte[] data = encoding.GetBytes(postData);
request.Method = "POST";//Supported Request Method
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
Dts.Variables["User::APIReadResponseStatusCode"].Value = response.StatusCode;
Dts.Variables["User::APIReadFullResponse"].Value = response.StatusDescription.ToString();
return new StreamReader(response.GetResponseStream()).ReadToEnd();
}
public static void ErrorLogging(Exception ex, string errorLogPath)
{
string errorLog = errorLogPath + DateTime.Today.ToString("MM-dd-yyyy");
if (!File.Exists(errorLogPath))
{
File.Create(errorLogPath).Dispose();
}
using (StreamWriter sw = File.AppendText(errorLogPath))
{
sw.WriteLine("=============Error Logging ===========");
sw.WriteLine("===========Start============= " + DateTime.Now);
sw.WriteLine("Error Message: " + ex.Message);
sw.WriteLine("Stack Trace: " + ex.StackTrace);
sw.WriteLine("===========End============= " + DateTime.Now);
sw.Flush();
sw.Close();
}
}
#region ScriptResults declaration
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
}
Edited to include error:
=============Error Logging =========== ===========Start============= 6/16/2020 12:42:09 PM Error Message: The remote server returned an error: (401) Unauthorized. Stack Trace: at System.Net.HttpWebRequest.GetResponse() at ST_32488ae1d3e348ca83a1017e2e2bc39f.ScriptMain.SC2REPLAY(String Config) at ST_32488ae1d3e348ca83a1017e2e2bc39f.ScriptMain.Main() ===========End============= 6/16/2020 12:42:09 PM