content: '********************************************************************
  '**  http-request.brs
  '********************************************************************
  '**  Example:
  '**  req = HttpRequest({
  '**      url: "http://www.apiserver.com/login",
  '**      method: "POST",
  '**      headers: { "Content-Type": "application/json" },
  '**      data: { user: "johndoe", password: "12345" }
  '**  })
  '**  req.send()
  '********************************************************************
  
  function HttpRequest(params = invalid as Dynamic) as Object
      url = invalid
      method = invalid
      headers = invalid
      data = invalid
      timeout = 0
      retries = 1
      interval = 500
  
      if params <> invalid then
          if params.url <> invalid then url = params.url
          if params.method <> invalid then method = params.method
          if params.headers <> invalid then headers = params.headers
          if params.data <> invalid then data = params.data
          if params.timeout <> invalid then timeout = params.timeout
          if params.retries <> invalid then retries = params.retries
          if params.interval <> invalid then interval = params.interval
      end if
  
      obj = {
          _timeout: timeout
          _retries: retries
          _interval: interval
          _deviceInfo: createObject("roDeviceInfo")
          _url: url
          _method: method
          _requestHeaders: headers
          _data: data
          _http: invalid
          _isAborted: false
  
          _isProtocolSecure: function(url as String) as Boolean
              return left(url, 6) = "https:"
          end function
  
          _createHttpRequest: function() as Object
              request = createObject("roUrlTransfer")
              request.setPort(createObject("roMessagePort"))
              request.setUrl(m._url)
              request.retainBodyOnError(true)
              request.enableCookies()
              if m._requestHeaders <> invalid then request.setHeaders(m._requestHeaders)
              if m._method <> invalid then request.setRequest(m._method)
  
              'Checks if URL protocol is secured, and adds appropriate parameters if needed
              if m._isProtocolSecure(m._url) then
                  request.setCertificatesFile("common:/certs/ca-bundle.crt")
                  request.addHeader("X-Roku-Reserved-Dev-Id", "")
                  request.initClientCertificates()
              end if
  
              return request
          end function
  
          getPort: function()
              if m._http <> invalid then
                  return m._http.getPort()
              else
                  return invalid
              end if
          end function
  
          getCookies: function(domain as String, path as String) as Object
              if m._http <> invalid then
                  return m._http.getCookies(domain, path)
              else
                  return invalid
              end if
          end function
  
          send: function(data = invalid as Dynamic) as Dynamic
              timeout = m._timeout
              retries = m._retries
              response = invalid
  
              if data <> invalid then m._data = data
  
              if m._data <> invalid and getInterface(m._data, "ifString") = invalid then
                  m._data = formatJson(m._data)
              end if
  
              while retries > 0 and m._deviceInfo.getLinkStatus()
                  if m._sendHttpRequest(m._data) then
                      event = m._http.getPort().waitMessage(timeout)
  
                      if m._isAborted then
                          m._isAborted = false
                          m._http.asyncCancel()
                          exit while
                      else if type(event) = "roUrlEvent" then
                          response = event
                          exit while
                      end if
  
                      m._http.asyncCancel()
                      timeout *= 2
                      sleep(m._interval)
                  end if
  
                  retries--
              end while
  
              return response
          end function
  
          _sendHttpRequest: function(data = invalid as Dynamic) as Dynamic
              m._http = m._createHttpRequest()
  
              if data <> invalid then
                  return m._http.asyncPostFromString(data)
              else
                  return m._http.asyncGetToString()
              end if
          end function
  
          abort: function()
              m._isAborted = true
          end function
  
      }
  
      return obj
  end function
  