Class: TestdroidAPI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/testdroid_api/client.rb

Direct Known Subclasses

ApikeyClient

Constant Summary collapse

API_VERSION =
'api/v2'
CLOUD_ENDPOINT =
'https://cloud.bitbar.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, cloud_url = CLOUD_ENDPOINT, logger = nil) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/testdroid_api/client.rb', line 4

def config
  @config
end

#loggerObject

Returns the value of attribute logger.



5
6
7
# File 'lib/testdroid_api/client.rb', line 5

def logger
  @logger
end

#tokenObject (readonly)

Returns the value of attribute token.



6
7
8
# File 'lib/testdroid_api/client.rb', line 6

def token
  @token
end

Instance Method Details

#adminObject

admin only



156
157
158
# File 'lib/testdroid_api/client.rb', line 156

def admin
  TestdroidAPI::Admin.new("/#{API_VERSION}/admin", self)
end

#authorizeObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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_connectionsObject



150
151
152
# File 'lib/testdroid_api/client.rb', line 150

def device_session_connections
  TestdroidAPI::DeviceSessionConnections.new("/#{API_VERSION}/device-session-connections", self)
end

#devicesObject



132
133
134
# File 'lib/testdroid_api/client.rb', line 132

def devices
  TestdroidAPI::Devices.new("/#{API_VERSION}/devices", self)
end

#download(uri, file_name) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
# 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 = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# 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

#infoObject

public read-only



128
129
130
# File 'lib/testdroid_api/client.rb', line 128

def info
  TestdroidAPI::CloudResource.new("/#{API_VERSION}/info", self, "info")
end

#label_groupsObject



136
137
138
# File 'lib/testdroid_api/client.rb', line 136

def label_groups
  TestdroidAPI::LabelGroups.new("/#{API_VERSION}/label-groups", self)
end

#meObject

user read-write



142
143
144
# File 'lib/testdroid_api/client.rb', line 142

def me
  TestdroidAPI::User.new("/#{API_VERSION}/me", self).load
end

#mime_for(path) ⇒ Object



43
44
45
46
# 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 = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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

#propertiesObject



146
147
148
# File 'lib/testdroid_api/client.rb', line 146

def properties
  TestdroidAPI::Properties.new("/#{API_VERSION}/properties", self)
end

#upload(uri, filename) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# 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