content: sub init()
      m.top.observeField("validateToken", "onValidateToken")
      m.top.observeField("apiCall", "onApiCall")
  end sub
  
  sub onValidateToken(event as Object)
      if event.getData() = true and m.top.accessToken <> invalid
          validateAccessToken()
      end if
  end sub
  
  sub onApiCall(event as Object)
      apiCall = event.getData()
      if apiCall <> invalid
          makeApiCall(apiCall)
      end if
  end sub
  
  sub validateAccessToken()
      request = CreateObject("roUrlTransfer")
      request.setUrl("https://api.put.io/v2/account/info")
      request.addHeader("Authorization", "Bearer " + m.top.accessToken)
      
      port = CreateObject("roMessagePort")
      request.setMessagePort(port)
      
      if request.asyncGetToString()
          msg = wait(5000, port)
          if msg <> invalid and type(msg) = "roUrlEvent"
              responseCode = msg.getResponseCode()
              
              response = {
                  success: (responseCode = 200),
                  code: responseCode
              }
              
              if responseCode = 200
                  responseString = msg.getString()
                  jsonResponse = ParseJson(responseString)
                  if jsonResponse <> invalid
                      response.data = jsonResponse
                  end if
              end if
              
              m.top.response = response
          end if
      end if
  end sub
  
  sub makeApiCall(apiCall as Object)
      request = CreateObject("roUrlTransfer")
      request.setUrl(apiCall.url)
      
      if m.top.accessToken <> invalid
          request.addHeader("Authorization", "Bearer " + m.top.accessToken)
      end if
      
      if apiCall.headers <> invalid
          for each header in apiCall.headers
              request.addHeader(header.key, header.value)
          end for
      end if
      
      port = CreateObject("roMessagePort")
      request.setMessagePort(port)
      
      success = false
      
      if apiCall.method = "POST"
          if apiCall.data <> invalid
              success = request.asyncPostFromString(apiCall.data)
          else
              success = request.asyncPostFromString("")
          end if
      else
          success = request.asyncGetToString()
      end if
      
      if success
          msg = wait(10000, port)
          if msg <> invalid and type(msg) = "roUrlEvent"
              responseCode = msg.getResponseCode()
              responseString = msg.getString()
              
              response = {
                  success: (responseCode = 200),
                  code: responseCode,
                  data: responseString
              }
              
              if responseCode = 200
                  jsonResponse = ParseJson(responseString)
                  if jsonResponse <> invalid
                      response.data = jsonResponse
                  end if
              end if
              
              m.top.response = response
          end if
      end if
  end sub
  