Class: TestdroidAPI::Client

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

Constant Summary collapse

API_VERSION =
'api/v2'
CLOUD_ENDPOINT =
'https://cloud.testdroid.com'
ACCEPT_HEADERS =
{'Accept' => 'application/json'}

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.



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

#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

#authorizeObject



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 authorize

	@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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/testdroid_api/client.rb', line 97

def delete(uri)

	@logger.error "token expired" if @token.expired?

	@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

#devicesObject



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



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/testdroid_api/client.rb', line 117

def download(uri, file_name)
	begin
		@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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/testdroid_api/client.rb', line 83

def get(uri, params={})

	@logger.error "token expired" if @token.expired?

	@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

#label_groupsObject



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

#post(uri, params) ⇒ Object



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

def post(uri, params)

	@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

#upload(uri, filename, file_type) ⇒ Object



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

def upload(uri, filename, file_type)
	begin
		@token.refresh!(:headers => ACCEPT_HEADERS) if @token.expired?
		connection = @token.client.connection
		payload = {:file  => Faraday::UploadIO.new(filename, file_type) }
		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