Class: TestdroidAPI::ApikeyClient

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, cloud_url = CLOUD_ENDPOINT, logger = nil) ⇒ ApikeyClient

Returns a new instance of ApikeyClient.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/testdroid_api/apikey_client.rb', line 11

def initialize(api_key, cloud_url = CLOUD_ENDPOINT, logger = nil)
  # Instance variables
  @api_key = api_key
  @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.



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

def config
  @config
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

Instance Method Details

#adminObject

admin only



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

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

#authorizeObject



23
24
25
26
27
28
29
30
# File 'lib/testdroid_api/apikey_client.rb', line 23

def authorize

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

  end
  @cloud_user
end

#delete(uri) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/testdroid_api/apikey_client.rb', line 85

def delete(uri)
  uri = @cloud_url + uri
  begin
    request = self.request_factory(:delete, uri)
    resp = request.execute
  rescue => e
    @logger.error "Failed to delete resource #{uri} #{e}"
    return nil
  end

  if resp.code != 204
    @logger.error "Failed to delete resource #{uri} #{e}"
  else
    @logger.info "response: #{resp.code}"
  end
end

#device_session_connectionsObject



150
151
152
# File 'lib/testdroid_api/apikey_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/apikey_client.rb', line 132

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

#download(uri, file_name, params = {}, http_params = {}) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/testdroid_api/apikey_client.rb', line 109

def download(uri, file_name, params = {}, http_params = {})
  begin
    ::File.open(file_name, "w+b") do |file|
      full_uri = uri.start_with?(@cloud_url) ? uri : @cloud_url + uri
      http_params = http_params.deep_merge({:headers => {:params => params}})
      request = self.request_factory(:get, full_uri, http_params)
      resp = request.execute
      file.write(resp.body)
    end
  rescue => e
    @logger.error "Failed to get resource #{uri} #{e}"
    return nil
  end
end

#get(uri, params = {}, http_params = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/testdroid_api/apikey_client.rb', line 52

def get(uri, params = {}, http_params = {})
  uri = @cloud_url + uri
  begin
    http_params = http_params.deep_merge({:headers => {:params => params}})
    request = self.request_factory(:get, uri, http_params)
    resp = request.execute
  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/apikey_client.rb', line 128

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

#label_groupsObject



136
137
138
# File 'lib/testdroid_api/apikey_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/apikey_client.rb', line 142

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

#mime_for(path) ⇒ Object



32
33
34
35
# File 'lib/testdroid_api/apikey_client.rb', line 32

def mime_for(path)
  mime = MIME::Types.type_for path
  mime.empty? ? 'text/plain' : mime[0].content_type
end

#post(uri, params = {}, http_params = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/testdroid_api/apikey_client.rb', line 65

def post(uri, params = {}, http_params = {})
  uri = @cloud_url + uri
  begin
    payload = params.fetch(:body, params)
    headers = params.fetch(:headers, {})
    http_params = http_params.deep_merge({:payload => payload})
    request = self.request_factory(:post, uri, http_params, headers)
    resp = request.execute
  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/apikey_client.rb', line 146

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

#request_factory(method, uri, http_params = {}, headers = {}) ⇒ Object

Basic methods



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/testdroid_api/apikey_client.rb', line 39

def request_factory(method, uri, http_params = {}, headers = {})
  default_http_params = {
      :method => method,
      :url => uri,
      :user => @api_key,
      :password => "",
      :headers => headers
  }
  request_http_params = default_http_params.deep_merge!(http_params)

  RestClient::Request.new(request_http_params)
end

#upload(uri, file_name) ⇒ Object



102
103
104
105
106
107
# File 'lib/testdroid_api/apikey_client.rb', line 102

def upload(uri, file_name)
  self.post(uri, {
      :multipart => true,
      :file => ::File.new(file_name, 'rb')
  })
end