Question

The Rackspace Email API example for C# doesn't work, it is based on OLD code,

http://api-wiki.apps.rackspace.com/api-wiki/index.php/Main_Page

I answer this item below, posted this question for others having the same issue I did.

UPDATE - Per Basti M comment below, here is the original code.

This code does not compile:

using System;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;

namespace RestApiClient
{
    public class RestApiClient
    {
        private HttpWebRequest request;
        private HttpWebResponse response;
        private string baseUrl;
        private string apiKey;
        private string secretKey;

        public RestApiClient(string baseUrl, string apiKey, string secretKey)
        {
            this.baseUrl = baseUrl;
            this.apiKey = apiKey;
            this.secretKey = secretKey;
        }

        public HttpWebResponse Get(string url, string format)
        {
            this.request = (System.Net.HttpWebRequest)HttpWebRequest.Create(this.baseUrl + url);
            request.Method = "GET";
            SignMessage();
            AssignFormat(format);
            return GetResponseContent();
        }

        public HttpWebResponse Post(string url, string data, string format)
        {
            this.request = (System.Net.HttpWebRequest)HttpWebRequest.Create(this.baseUrl + url);
            request.Method = "POST";
            SignMessage();
            AssignFormat(format);
            SendFormData(data);
            return GetResponseContent();
        }

        public HttpWebResponse Put(string url, string data, string format)
        {
            this.request = (System.Net.HttpWebRequest)HttpWebRequest.Create(this.baseUrl + url);
            request.Method = "PUT";
            SignMessage();
            AssignFormat(format);
            SendFormData(data);
            return GetResponseContent();
        }

        public HttpWebResponse Delete(string url, string format)
        {
            this.request = (System.Net.HttpWebRequest)HttpWebRequest.Create(this.baseUrl + url);
            request.Method = "DELETE";
            SignMessage();
            AssignFormat(format);
            return GetResponseContent();
        }

        private void SendFormData(string data)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] byteData = encoding.GetBytes(data);
            this.request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteData.Length;
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(byteData, 0, byteData.Length);
            requestStream.Close();
        }

        private HttpWebResponse GetResponseContent()
        {
            try
            {
                return (HttpWebResponse)request.GetResponse();
            }
            catch (WebException e)
            {
                return (HttpWebResponse)e.Response;
            }

        }

        private void SignMessage()
        {
            var userAgent = "C# Client Library";
            this.request.UserAgent = userAgent;
            var dateTime = DateTime.UtcNow.ToString("yyyyMMddHHmmss");
            var dataToSign = apiKey + userAgent + dateTime + secretKey;
            var hash = SHA1.Create();
            var signedBytes = hash.ComputeHash(Encoding.UTF8.GetBytes(dataToSign));
            var signature = Convert.ToBase64String(signedBytes);

            request.Headers["X-Api-User-Signature"] = apiKey + ":" + dateTime + ":" + signature;
        }

        private void AssignFormat(string format)
        {
            this.request.Accept = format;
        }
    }
}
Was it helpful?

Solution

I just wanted to post this working example for everyone: http://www.solutionevangelist.com/post/8

Just a few slight adjustments, copied here in case link ever dies:

UPDATE: I added additional PUT & DELETE methods.

using System;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Text;
using System.Net;
static void Main()
{
    // see: http://api-wiki.apps.rackspace.com/api-wiki/index.php/Main_Page
    var apiKey = "";
    var secretKey = "";
    var wm = new WebMethods(new System.Net.WebClient(), "https://api.emailsrvr.com/v0/", apiKey, secretKey);
    Console.Write(wm.Get("domains")); // Get list of Domains
    Console.Write(wm.Get("domains/example.com")); // Get summary of services in Domain
    Console.Write(wm.Get("domains/example.com/ex/mailboxes")); // Get Rackpsace Email Mailboxes for Domain
    Console.Write(wm.Get("domains/example.com/rs/mailboxes")); // Get Microsoft Exchange Mailboxes for Domain
}
public class WebMethods
{
    private WebClient client;
    private string baseUrl;
    private string apiKey;
    private string secretKey;
    private string format;

    public WebMethods(WebClient client, string baseUrl, string apiKey, string secretKey, string format = "text/xml")
    {
        this.client = client;
        this.baseUrl = baseUrl;
        this.apiKey = apiKey;
        this.secretKey = secretKey;
        this.format = format;
    }

    public virtual string Get(string url)
    {
        return MakeRemoteCall((client) =>
        {
            return client.DownloadString(baseUrl + url);
        },
        format);
    }

    public virtual string Post(string url, System.Collections.Specialized.NameValueCollection data)
    {
        return MakeRemoteCall((client) =>
        {
            var bytes = client.UploadValues(baseUrl + url, data);
            return Encoding.UTF8.GetString(bytes);
        },
        format);
    }

    public virtual string Put( string url, NameValueCollection data )
    {
        return MakeRemoteCall( ( client ) =>
        {
            var bytes = client.UploadValues( baseUrl + url, "PUT", data );
            return Encoding.UTF8.GetString( bytes );
        },
          format );
    }
    public virtual string Delete( string url )
    {
        return MakeRemoteCall( ( client ) =>
        {
            return client.UploadString( baseUrl + url, "DELETE", "" );
        },
          format );
    }

    private void SignMessage()
    {
        var userAgent = "C# Client Library";
        client.Headers["User-Agent"] = userAgent;

        var dateTime = DateTime.UtcNow.ToString("yyyyMMddHHmmss");

        var dataToSign = apiKey + userAgent + dateTime + secretKey;
        var hash = System.Security.Cryptography.SHA1.Create();
        var signedBytes = hash.ComputeHash(Encoding.UTF8.GetBytes(dataToSign));
        var signature = Convert.ToBase64String(signedBytes);

        client.Headers["X-Api-Signature"] = apiKey + ":" + dateTime + ":" + signature;
    }

    private void AssignFormat(string format)
    {
        client.Headers["Accept"] = format;
    }

    private string MakeRemoteCall(Func remoteCall, string format)
    {
        SignMessage();
        AssignFormat(format);
        return remoteCall.Invoke(client);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top