VB.NET

Collapse All

Common Functions

  Private Const ApiKey As String = "Your APIkey"
  Private Const PublicKey As String = "Your PublicKey"
  Private Const PrivateKey As String = "Your privateKey"
  Private Const Format = "xml" 'use "xml" or "json"

  Private Function GetResponse(ByVal method As String, ByVal requestUri As String) As String

    Return GetResponseHelper(method, requestUri, String.Empty, True, False, String.Empty)

  End Function

  Private Function GetResponse(ByVal method As String, ByVal requestUri As String, ByVal data As String) As String

    Return GetResponseHelper(method, requestUri, data, True, False, String.Empty)

  End Function

  Private Function GetResponseWithoutAuthorization(ByVal method As String, ByVal requestUri As String, ByVal data As String) As String

    Return GetResponseHelper(method, requestUri, data, False, False, String.Empty)

  End Function

  Private Function GetResponseSecure(ByVal method As String, ByVal requestUri As String, ByVal pass As String) As String

    Return GetResponseHelper(method, requestUri, String.Empty, False, True, pass)

  End Function

  Private Function GetResponseHelper(ByVal method As String, ByVal requestUri As String, ByVal data As String, ByVal authorization As Boolean, ByVal secure As Boolean, ByVal pass As String) As String
    Dim apiUrl As String
    Const url = "apidev.fonts.com"
    If secure Then
      apiUrl = "https://" & url
    Else
      apiUrl = "http://" & url
    End If
    Dim enc = Encoding.UTF8
    Dim response As HttpWebResponse = Nothing
    Dim request As HttpWebRequest = DirectCast(WebRequest.Create(apiUrl & requestUri), HttpWebRequest)
    request.Method = method
    If pass <> "" Then request.Headers.Add("Password", pass)
    If authorization = True Then request.Headers.Add("Authorization", GetAuthorizationHeader(requestUri))
    request.Headers.Add("AppKey", ApiKey)
    If method.Equals("POST") OrElse method.Equals("PUT") Then
      If data Is Nothing Then data = String.Empty
      Dim dataBytes = enc.GetBytes(data)
      request.ContentLength = dataBytes.Length
      Using rs = request.GetRequestStream()
        rs.Write(dataBytes, 0, dataBytes.Length)
      End Using
    End If

    Try
      response = DirectCast(request.GetResponse(), HttpWebResponse)
    Catch ex As WebException
      response = DirectCast(ex.Response, HttpWebResponse)
    Catch ex As Exception
    End Try

    Dim responseBody As String
    If response IsNot Nothing Then
      Using responseStream = response.GetResponseStream()
        Using readStream = New StreamReader(responseStream, enc)
          responseBody = readStream.ReadToEnd()
        End Using
      End Using

      Return responseBody
    Else
      Return "The response is empty"
    End If

  End Function

  Private Function GetAuthorizationHeader(ByVal requestUri As String) As String

    Dim enc = Encoding.UTF8
    Dim secretBytes = enc.GetBytes(PrivateKey)

    Using crypto = New HMACMD5(secretBytes)
      Dim dataBytes = enc.GetBytes(PublicKey & "|" & requestUri)
      Dim hash = crypto.ComputeHash(dataBytes)
      Dim signature = Convert.ToBase64String(hash)
      Return HttpUtility.UrlEncode(PublicKey & ":" & signature)
    End Using

  End Function