class TestdroidAPI::Client

Constants

API_VERSION
CLOUD_ENDPOINT

Attributes

config[R]
logger[RW]
token[R]

Public Class Methods

new(username, password, cloud_url = CLOUD_ENDPOINT, logger = nil) click to toggle source
# File lib/testdroid_api/client.rb, line 11
def initialize(username, password, cloud_url = CLOUD_ENDPOINT, logger = nil)
  # Instance variables
  @username = username
  @password = password
  @cloud_url = cloud_url
  @logger = logger

  if @logger.nil?
    @logger = Logger.new(STDOUT)
    @logger.info("Logger is not defined => output to STDOUT")
  end
end

Public Instance Methods

admin() click to toggle source

admin only

# File lib/testdroid_api/client.rb, line 156
def admin
  TestdroidAPI::Admin.new("/#{API_VERSION}/admin", self)
end
authorize() click to toggle source
# File lib/testdroid_api/client.rb, line 24
def authorize
  @client = OAuth2::Client.new(
      'testdroid-cloud-api', nil, :site => @cloud_url, :token_url => 'oauth/token') do |faraday|
    faraday.request :multipart
    faraday.request :url_encoded
    faraday.response :logger, @logger
    faraday.adapter Faraday.default_adapter
  end

  @token = @client.password.get_token(@username, @password)

  if @cloud_user.nil?
    @cloud_user = TestdroidAPI::User.new("/#{API_VERSION}/me", self).refresh
    @cloud_user = TestdroidAPI::User.new("/#{API_VERSION}/users/#{@cloud_user.id}", self).refresh

  end
  @cloud_user
end
delete(uri) click to toggle source
# File lib/testdroid_api/client.rb, line 92
def delete(uri)

  @token = @token.refresh! if @token.expired?

  begin
    resp = @token.delete(@cloud_url + "#{uri}")
  rescue => e
    @logger.error "Failed to delete resource #{uri} #{e}"
    return nil
  end

  if resp.status != 204
    @logger.error "Failed to delete resource #{uri} #{e}"
  else
    @logger.info "response: #{resp.status}"
  end
end
device_session_connections() click to toggle source
# File lib/testdroid_api/client.rb, line 150
def device_session_connections
  TestdroidAPI::DeviceSessionConnections.new("/#{API_VERSION}/device-session-connections", self)
end
devices() click to toggle source
# File lib/testdroid_api/client.rb, line 132
def devices
  TestdroidAPI::Devices.new("/#{API_VERSION}/devices", self)
end
download(uri, file_name) click to toggle source
# File lib/testdroid_api/client.rb, line 110
def download(uri, file_name)
  begin
    @token = @token.refresh! if @token.expired?
    ::File.open(file_name, "w+b") do |file|
      full_uri = uri.start_with?(@cloud_url) ? uri : @cloud_url + uri
      resp = @token.get(full_uri)
      file.write(resp.body)
    end
  rescue => e
    @logger.error "Failed to get resource #{uri} #{e}"
    return nil
  end
end
get(uri, params = {}) click to toggle source
# File lib/testdroid_api/client.rb, line 79
def get(uri, params = {})

  @token = @token.refresh! if @token.expired?

  begin
    resp = @token.get(@cloud_url + "#{uri}", :params => params)
  rescue => e
    @logger.error "Failed to get resource #{uri} #{e}"
    return nil
  end
  JSON.parse(resp.body)
end
info() click to toggle source

public read-only

# File lib/testdroid_api/client.rb, line 128
def info
  TestdroidAPI::CloudResource.new("/#{API_VERSION}/info", self, "info")
end
label_groups() click to toggle source
# File lib/testdroid_api/client.rb, line 136
def label_groups
  TestdroidAPI::LabelGroups.new("/#{API_VERSION}/label-groups", self)
end
me() click to toggle source

user read-write

# File lib/testdroid_api/client.rb, line 142
def me
  TestdroidAPI::User.new("/#{API_VERSION}/me", self).load
end
mime_for(path) click to toggle source
# File lib/testdroid_api/client.rb, line 43
def mime_for(path)
  mime = MIME::Types.type_for path
  mime.empty? ? 'text/plain' : mime[0].content_type
end
post(uri, params = {}) click to toggle source
# File lib/testdroid_api/client.rb, line 61
def post(uri, params = {})

  @token = @token.refresh! if @token.expired?

  begin
    resp = @token.post("#{@cloud_url}#{uri}", params)
  rescue => e
    @logger.error "Failed to post resource #{uri} #{e}"
    return nil
  end

  if resp.body.nil? || resp.body.length == 0
    return nil
  end

  JSON.parse(resp.body)
end
properties() click to toggle source
# File lib/testdroid_api/client.rb, line 146
def properties
  TestdroidAPI::Properties.new("/#{API_VERSION}/properties", self)
end
upload(uri, filename) click to toggle source
# File lib/testdroid_api/client.rb, line 48
def upload(uri, filename)
  begin
    @token = @token.refresh! if @token.expired?
    connection = @token.client.connection
    payload = {:file => Faraday::UploadIO.new(filename, mime_for(filename))}
    response = connection.post(@cloud_url + "#{uri}", payload, @token.headers)
  rescue => e
    @logger.error e
    return nil
  end
  JSON.parse(response.body)
end