Common Functions

  private const string ApiKey = "Your APIkey";
  private const string PublicKey = "Your PublicKey";
  private const string PrivateKey = "Your privateKey";
  private const string Format = "xml"; //use 'xml' or 'json' 

  private string GetResponse(string method, string requestUri)
  {

    return GetResponseHelper(method, requestUri, string.Empty, true, false, string.Empty);
    
  }
  
  private string GetResponse(string method, string requestUri, string data)
  {

    return GetResponseHelper(method, requestUri,data, true, false, string.Empty);

  }
   
  private string GetResponseWithoutAuthorization(string method, string requestUri, string data)
  {

    return GetResponseHelper(method, requestUri, data, false, false, string.Empty);

  }
  
  private string GetResponseSecure(string method, string requestUri, string pass)
  {

    return GetResponseHelper(method, requestUri, string.Empty, false, true, pass);

  }

  private string GetResponseHelper(string method, string requestUri, string data, Boolean authorization, Boolean secure, string pass)
  {
    string apiUrl;
    const string  url = "apidev.fonts.com";
    if (secure)
    apiUrl = "https://" + url;
    else
    apiUrl = "http://" + url;
     
    var enc = Encoding.UTF8;
    HttpWebResponse response = null;
     
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiUrl + requestUri);
    request.Method = method;

    if (!string.IsNullOrEmpty(pass))
      request.Headers.Add("Password", pass);
    if (authorization==true)
      request.Headers.Add("Authorization", GetAuthorizationHeader(requestUri));

    request.Headers.Add("AppKey", ApiKey);

    if (method.Equals("POST") | method.Equals("PUT"))
    {
      if (data == null)
        data = string.Empty;
      var dataBytes = enc.GetBytes(data);
      request.ContentLength = dataBytes.Length;
      using (var rs = request.GetRequestStream())
      {
        rs.Write(dataBytes, 0, dataBytes.Length);
      }
    }

    try
    {
      response = (HttpWebResponse)request.GetResponse();
    }
    catch (WebException ex)
    {
      response = (HttpWebResponse)ex.Response;
    }
    catch (Exception ex)
    {
    }

    string responseBody = null;

    if (response != null)
    {
      using (var responseStream = response.GetResponseStream())
      {
        using (var readStream = new StreamReader(responseStream, enc))
        {
          responseBody = readStream.ReadToEnd();
        }
      }
    }

    return responseBody;

  }

  private string GetAuthorizationHeader(string requestUri)
  {
   
    var enc = Encoding.UTF8;
    var secretBytes = enc.GetBytes(PrivateKey);

    using (var crypto = new HMACMD5(secretBytes))
    {
      var dataBytes = enc.GetBytes(PublicKey + "|" + requestUri);
      var hash = crypto.ComputeHash(dataBytes);
      var signature = Convert.ToBase64String(hash);
      return HttpUtility.UrlEncode(PublicKey + ":" + signature);
    }

  }