Skip to content

Instantly share code, notes, and snippets.

@mallain
Created September 11, 2011 18:24
Show Gist options
  • Save mallain/1209934 to your computer and use it in GitHub Desktop.
Save mallain/1209934 to your computer and use it in GitHub Desktop.
YammerConnector for QVSource
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IndustrialCodeBox.APIConnector;
using IndustrialCodeBox.APIConnector.Interfaces;
using System.Data;
using System.Collections.Specialized;
using System.Windows.Forms;
namespace YammerConnector
{
class BusinessDecision_YammerConnector : APIConnectorBase
{
/// <summary>
/// Here we declare references to individual datatables our connector returns.
/// </summary>
private DataTable _dt1, _dt2;
private IOAuthHttpHelper _oauth;
private ConnectorConfigCtl _ctl;
public override List<object> GetTables()
{
return new List<object>()
{
"Messages",
"Table 2"
};
}
public override void OnRequestTable(object table, IProgress prog, eEnvironment environent, System.Collections.Specialized.NameValueCollection additionalVals, ITableWriter tblWriter)
{
string username = "";
if (additionalVals != null && additionalVals["username"] != null)
{
username = additionalVals["username"];
}
else
{
username = Settings.GetSetting("username", "").ToString();
}
string password = "";
if (additionalVals != null && additionalVals["password"] != null)
{
password = additionalVals["password"];
}
else
{
password = Settings.GetSetting("password", "").ToString();
}
if (!string.IsNullOrEmpty(password))
{
password = Encryptor.Decrypt(password);
}
if (table.ToString() == "Messages")
{
writeTable1(prog, tblWriter);
}
}
/*******************************************************************************
* MANDATORY IF A custom control is used to gather additional input.
* *****************************************************************************/
/// <summary>
/// Returns a reference to the custom control to gather additional
/// input from the user.
/// </summary>
public override Control GetControl()
{
if (_ctl == null)
{
_ctl = new ConnectorConfigCtl();
_ctl.Setup(this.Settings, this.Encryptor);
}
return _ctl;
}
/*******************************************************************************
* OPTIONAL but recommended.
* *****************************************************************************/
/// <summary>
/// This is fired when the user presses the clear cache button in the UI and
/// causes any data cached (e.g. saved to disk) by the QVSource caching mechanism
/// to be deleted. It is a good idea to clear any cached data in memory here too.
/// </summary>
public override void OnCachedCleared()
{
_dt1 = null;
_dt2 = null;
}
/*******************************************************************************
* OPTIONAL but recommended if any initialisation is needed.
* *****************************************************************************/
/// <summary>
/// This is fired when the connector has been initialsed. It should usually be
/// used instead of a constructor on this class.
/// </summary>
protected override void OnInitialised()
{
_oauth = this.GetOAuthConnector();
_oauth.ConsumerKey = "My ConsumerKey";
_oauth.ConsumerSecret = "My ConsumerSecret";
_oauth.REQUEST_TOKEN = "https://www.yammer.com/oauth/request_token";
_oauth.AUTHORIZE = "https://www.yammer.com/oauth/authorize";
_oauth.ACCESS_TOKEN = "https://www.yammer.com/oauth/access_token";
//_oauth.CALLBACK = "http://CALLBACK TO YOUR OWN DOMAIN";
_oauth.Token = Host.GetSetting("Token", "").ToString();
_oauth.TokenSecret = Host.GetSetting("TokenSecret", "").ToString();
_oauth.Verifier = Host.GetSetting("Verifier", "").ToString();
//_oauth.Get("https://www.yammer.com/api/v1/messages.json");
}
void _ctl_ConnectionReqested(object sender, EventArgs e)
{
try
{
if (Settings.AreAllPresent("Token", "TokenSecret", "Verifier"))
{
_oauth.Token = Host.GetSetting("Token").ToString();
_oauth.TokenSecret = Host.GetSetting("TokenSecret").ToString();
_oauth.Verifier = Host.GetSetting("Verifier").ToString();
}
else
{
_oauth.Verifier = null;
_oauth.TokenSecret = null;
_oauth.Token = null;
_oauth.GetAndAuthoriseToken();
String accessToken = _oauth.getAccessToken();
Host.SaveSetting("Token", _oauth.Token);
Host.SaveSetting("TokenSecret", _oauth.TokenSecret);
Host.SaveSetting("Verifier", _oauth.Verifier);
//_ctl.Token = _oauth.Token;
}
}
catch (Exception excp)
{
// Error handling
}
}
/*******************************************************************************
* REQUIRED if additional input from user is needed when running from QlikView.
* *****************************************************************************/
/// <summary>
/// If you want to have additional data stored into the load statement connection
/// string return them here. They will be passed back in when the OnRequestTable
/// is called. For example if you API requires a username and password this could
/// return both of these then in the OnRequestTable they can be extracted back out.
/// Note that passwords etc. should always be encrypted before returning here.
/// </summary>
/// <param name="table">The table which the user is currently looking at.</param>
public override NameValueCollection GetAdditionalParameters(object table)
{
NameValueCollection vals = new NameValueCollection();
string username = Settings.GetSetting("username", "").ToString();
if (!string.IsNullOrEmpty(username))
{
vals.Add("username", username);
}
string password = Settings.GetSetting("password", "").ToString();
if (!string.IsNullOrEmpty(password))
{
vals.Add("password", Encryptor.Encrypt(password));
}
return vals;
}
/*******************************************************************************
* IT IS HERE WE WOULD ACTUALLY HIT THE API OVER HTTP FOR DATA
* AT PRESENT DATA IS HARDCODED - NOTES ON CONENCTING TO APIS WILL BE ADDED SOON
* *****************************************************************************/
private DataTable writeTable1(IProgress prog, ITableWriter tblWriter)
{
if (_dt1 == null)
{
const int NO_ROWS = 5;
DataTable dt = null;
dt = new DataTable();
dt.Columns.Add("created_at");
dt.Columns.Add("Column 2");
//String a = _oauth.Get("https://www.yammer.com/api/v1/messages.json");
for (int r = 0; r < NO_ROWS; r++)
{
dt.Rows.Add("Column 1:" + r, "Column 2:" + r);
prog.Set(NO_ROWS, r, "Downloading data " + r + " of " + NO_ROWS);
// Check if the user has cancelled the operation and if
// so abort.
if (prog.Cancelled) break;
}
_dt1 = dt;
}
tblWriter.WriteTable(_dt1);
return _dt1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment