Class: TestdroidAPI::Client
- Inherits:
-
Object
- Object
- TestdroidAPI::Client
- Defined in:
- lib/testdroid_api/client.rb
Direct Known Subclasses
Constant Summary collapse
- API_VERSION =
'api/v2'- CLOUD_ENDPOINT =
'https://cloud.testdroid.com'- ACCEPT_HEADERS =
{'Accept' => 'application/json'}
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
-
#admin ⇒ Object
admin only.
- #authorize ⇒ Object
- #delete(uri) ⇒ Object
- #device_session_connections ⇒ Object
- #devices ⇒ Object
- #download(uri, file_name) ⇒ Object
- #get(uri, params = {}) ⇒ Object
-
#info ⇒ Object
public read-only.
-
#initialize(username, password, cloud_url = CLOUD_ENDPOINT, logger = nil) ⇒ Client
constructor
A new instance of Client.
- #label_groups ⇒ Object
-
#me ⇒ Object
user read-write.
- #mime_for(path) ⇒ Object
- #post(uri, params) ⇒ Object
- #properties ⇒ Object
- #upload(uri, filename) ⇒ Object
Constructor Details
#initialize(username, password, cloud_url = CLOUD_ENDPOINT, logger = nil) ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/testdroid_api/client.rb', line 12 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
#config ⇒ Object (readonly)
Returns the value of attribute config.
4 5 6 |
# File 'lib/testdroid_api/client.rb', line 4 def config @config end |
#logger ⇒ Object
Returns the value of attribute logger.
5 6 7 |
# File 'lib/testdroid_api/client.rb', line 5 def logger @logger end |
#token ⇒ Object (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
#admin ⇒ Object
admin only
164 165 166 |
# File 'lib/testdroid_api/client.rb', line 164 def admin TestdroidAPI::Admin.new("/#{API_VERSION}/admin", self) end |
#authorize ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/testdroid_api/client.rb', line 34 def @client = OAuth2::Client.new('testdroid-cloud-api', nil, :site => @cloud_url, :authorize_url => 'oauth/authorize', :token_url => 'oauth/token', :headers => ACCEPT_HEADERS) 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, :headers => ACCEPT_HEADERS) 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
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/testdroid_api/client.rb', line 101 def delete(uri) @token = @token.refresh!(:headers => ACCEPT_HEADERS) if @token.expired? begin resp = @token.delete(@cloud_url+"#{uri}", :headers => ACCEPT_HEADERS ) 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}" return nil else @logger.info "response: #{resp.status}" end end |
#device_session_connections ⇒ Object
158 159 160 |
# File 'lib/testdroid_api/client.rb', line 158 def device_session_connections TestdroidAPI::DeviceSessionConnections.new("/#{API_VERSION}/device-session-connections", self) end |
#devices ⇒ Object
29 30 31 32 33 |
# File 'lib/testdroid_api/client.rb', line 29 def devices devices = TestdroidAPI::Devices.new( "/#{API_VERSION}/devices", self ) devices.list devices end |
#download(uri, file_name) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/testdroid_api/client.rb', line 119 def download(uri, file_name) begin @token = @token.refresh!(:headers => ACCEPT_HEADERS) if @token.expired? File.open(file_name, "w+b") do |file| resp = @token.get("#{@cloud_url}/#{uri}", :headers => ACCEPT_HEADERS) file.write(resp.body) end rescue => e @logger.error "Failed to get resource #{uri} #{e}" return nil end end |
#get(uri, params = {}) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/testdroid_api/client.rb', line 89 def get(uri, params={}) @token = @token.refresh!(:headers => ACCEPT_HEADERS) if @token.expired? begin resp = @token.get(@cloud_url+"#{uri}", params.merge(:headers => ACCEPT_HEADERS)) rescue => e @logger.error "Failed to get resource #{uri} #{e}" return nil end JSON.parse(resp.body) end |
#info ⇒ Object
public read-only
136 137 138 |
# File 'lib/testdroid_api/client.rb', line 136 def info TestdroidAPI::CloudResource.new("/#{API_VERSION}/info", self, "info") end |
#label_groups ⇒ Object
24 25 26 27 28 |
# File 'lib/testdroid_api/client.rb', line 24 def label_groups label_groups = TestdroidAPI::LabelGroups.new( "/#{API_VERSION}/label-groups", self ) label_groups.list label_groups end |
#me ⇒ Object
user read-write
150 151 152 |
# File 'lib/testdroid_api/client.rb', line 150 def me TestdroidAPI::User.new("/#{API_VERSION}/me", self).load end |
#mime_for(path) ⇒ Object
54 55 56 57 |
# File 'lib/testdroid_api/client.rb', line 54 def mime_for(path) mime = MIME::Types.type_for path mime.empty? ? 'text/plain' : mime[0].content_type end |
#post(uri, params) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/testdroid_api/client.rb', line 72 def post(uri, params) @token = @token.refresh!(:headers => ACCEPT_HEADERS) if @token.expired? begin resp = @token.post("#{@cloud_url}#{uri}", params.merge(:headers => ACCEPT_HEADERS)) 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 ⇒ Object
154 155 156 |
# File 'lib/testdroid_api/client.rb', line 154 def properties TestdroidAPI::Properties.new("/#{API_VERSION}/properties", self) end |
#upload(uri, filename) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/testdroid_api/client.rb', line 59 def upload(uri, filename) begin @token = @token.refresh!(:headers => ACCEPT_HEADERS) if @token.expired? connection = @token.client.connection payload = {:file => Faraday::UploadIO.new(filename, mime_for(filename)) } headers = ACCEPT_HEADERS.merge(@token.headers) response = connection.post(@cloud_url+"#{uri}",payload, headers) rescue => e @logger.error e return nil end JSON.parse(response.body) end |