Created
July 8, 2011 01:09
-
-
Save writeameer/1070894 to your computer and use it in GitHub Desktop.
RightScale API sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
using System.IO; | |
namespace ConsoleApplication2 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Reading RS specific config from ENV variables. To avoid | |
// posting credentials publicly. They can be assigned however neccessary | |
var username = Environment.GetEnvironmentVariable("RS_USERNAME", EnvironmentVariableTarget.Machine); | |
var password = Environment.GetEnvironmentVariable("RS_PASSWORD", EnvironmentVariableTarget.Machine); | |
var account = Environment.GetEnvironmentVariable("RS_ACCOUNT",EnvironmentVariableTarget.Machine); | |
// Build API Url | |
var apiUrl = "https://my.rightscale.com/api/acct/" + account + "/servers.xml"; | |
// Make call | |
var response = HttpGet(apiUrl,username,password); | |
// Display output | |
Console.WriteLine(response); | |
} | |
// Code from Nate modified a little: | |
public static string HttpGet(string url, string username, string password) | |
{ | |
ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3; | |
string result = null; | |
try | |
{ | |
HttpWebRequest req = WebRequest.Create(url) | |
as HttpWebRequest; | |
req.Headers["X-API-VERSION"] = "1.0"; | |
req.PreAuthenticate = false; | |
req.Method = WebRequestMethods.Http.Get; | |
var authInfo = System.Text.Encoding.ASCII.GetBytes(username + ":" + password); | |
var authString = Convert.ToBase64String(authInfo); | |
req.Headers["Authorization"] = "Basic " + authString; | |
using (HttpWebResponse resp = req.GetResponse() | |
as HttpWebResponse) | |
{ | |
StreamReader reader = | |
new StreamReader(resp.GetResponseStream()); | |
result = reader.ReadToEnd(); | |
} | |
} | |
catch (WebException wex) | |
{ | |
result = wex.ToString(); | |
} | |
catch (Exception ex) | |
{ | |
result = ex.ToString(); | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment