forked from kynetx/Kynetx-Application-Manager-API
/
direct_api.rb
244 lines (205 loc) · 7.25 KB
/
direct_api.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
module KynetxAmApi
require 'rubygems'
require 'oauth'
require 'json'
require 'net/http/post/multipart'
class DirectApi
attr_accessor :oauth
#
# Create a new session to the KynetxApi Manager.
#
# request_token
# request_secret
# :request_token
# :request_secret
# :oauth_verifier
# :access_token
# :access_secret
#
def initialize(tokens_and_secrets = {})
@oauth = KynetxAmApi::Oauth.new(tokens_and_secrets)
end
#
def get_request_token
@oauth.get_request_token
end
#
# Provides the url to direct user to if the application has not been authorized.
#
def get_authorize_url
return get_request_token.authorize_url
end
#
# API Call From Here Down.
#
#
#
def get_applist
return get_response("applist", :json)
end
def get_appcreate
return get_response("appcreate", :json)
end
def post_app_delete(application_id)
return post_response("app/#{application_id}/delete", {})
end
def post_remove_user(application_id, user_id)
return post_response("app/#{application_id}/removeuser", {"userid" => user_id})
end
def post_app_inviteuser(application_id, application_name, email, message)
return post_response("app/#{application_id}/inviteuser", {"email" => email, "message" => message, "appname" => application_name.to_s})
end
def post_app_invitecancel(application_id, invite_id)
return post_response("app/#{application_id}/invitecancel",{"inviteid" => invite_id})
end
def post_app_transferownershiprequest(application_id,application_name,user_id,message)
return post_response("app/#{application_id}/ownershiptransferrequest", {"userid" => user_id, "message" => message, "appname" => application_name})
end
def post_app_ownershiptransfercancel(application_id,transfer_id)
return post_response("app/#{application_id}/ownershiptransfercancel", {"ownershiptransferrequestid" => transfer_id})
end
#
# type is one of ff = firefox, ie = internet explorer, cr = google chrome
#
def post_app_generate(application_id, type, opts={})
#
# default_options = {
# "name" => "",
# "author" => "",
# "description" => "",
# "guid" => "",
# "datasets" => "",
# "env" => "prod",
# "image_url" => "http://appresource.s3.amazonaws.com/apiappimages/missing_icard.jpg",
# "runtime" => ""
# }
# options = default_options.merge(opts)
return post_response("app/#{application_id}/generate/#{type}", opts,:json)
end
def get_app_source(application_id, version, format = :krl)
data = get_response("app/#{application_id}/source/#{version}/#{format}", format)
return data
end
def get_app_info(application_id)
return get_response("app/#{application_id}/info", :json)
end
def get_app_details(application_id)
return get_response("app/#{application_id}/details", :json)
end
def post_app_source(application_id, source, type = "krl")
data = ""
if type == "krl"
data = {:krl => source.to_s}
else
data = {:json => source.to_json}
end
return post_response("app/#{application_id}/source", data)
end
def post_app_setproductversion(application_id, version)
return post_response("app/#{application_id}/setproductionversion", {"version" => version})
end
def post_app_setversionnote(application_id, version, note)
return post_response("app/#{application_id}/setversionnote", {"version" => version, "note" => note}, :json)
end
def post_app_updateappimage(application_id, filename, content_type, image_data)
# TODO: Make this go through the APIß
response = ""
url = URI.parse('https://accounts.kynetx.com/api/0.1/updateAppInfo')
StringIO.open(image_data) do |i|
req = Net::HTTP::Post::Multipart.new url.path,
"image" => UploadIO.new(i, content_type, filename),
"appid" => application_id
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
response = http.start { |h| h.request(req) }
end
return response
end
def ping
return get_response("ping")
end
def get_user_info
user = @oauth.user
if user.username.to_s.empty?
user_info = get_response("userinfo", :json)
user.username = user_info["username"]
user.userid = user_info["userid"]
user.name = user_info["name"]
end
return user
end
def get_app_stats_kpis(application_id, range)
return get_response("app/#{application_id}/stats/kpis/#{range}", :json)
end
def get_stats_query(k,d,c=nil,r=nil)
q_params = QueryParams.new.merge({:k => k, :d => d})
if c
# c are the conditions and they are supplied like so:
# [ {:field => "fieldname", :value => "value"}, ...]
c.each do |condition|
q_params["where_#{condition[:field]}".to_sym] = condition[:value]
end
end
q_params[:r] = r if r
return get_response("stats/query" + q_params.to_params, :json)
end
def get_stats_interface
return get_response("stats/interface", :json)
end
def get_stats_logging(application_id, range)
return get_response("app/#{application_id}/stats/logging/#{range}", :json)
end
private
def get_response(api_method, format = nil)
if format == :json
headers = {'Accept'=>'application/json'}
end
api_call = "/0.1/#{api_method}"
puts "---------GET---------------" if $DEBUG
puts api_call if $DEBUG
puts "___________________________" if $DEBUG
response = @oauth.get_access_token.get(api_call, headers).body
puts response.inspect if $DEBUG
puts "___________________________" if $DEBUG
begin
response = JSON.parse(response) if format == :json
rescue
puts $! if $DEBUG
raise "Unexpected response from the api: (#{api_method}) :: #{response}"
end
return response
end
# Probably a better way to do this. Make it a little more DRY
def post_response(api_method, data, format=nil, additional_headers=nil)
if format == :json
headers = {'Accept'=>'application/json'}
end
if additional_headers
headers.merge!(additional_headers)
end
api_call = "/0.1/#{api_method}"
puts "---------POST--------------" if $DEBUG
puts api_call if $DEBUG
puts data.inspect if $DEBUG
puts "___________________________" if $DEBUG
response = @oauth.get_access_token.post(api_call, data, headers).body
puts response.inspect if $DEBUG
puts "---------------------------" if $DEBUG
begin
response = JSON.parse(response) if format == :json
rescue
puts $! if $DEBUG
raise "Unexpected response from the api: (#{api_method}) :: #{response}"
end
return response
end
end
class QueryParams < Hash
def to_params
return "" if self.empty?
p = "?"
self.each {|k,v| p += "#{CGI::escape k.to_s}=#{CGI::escape v.to_s}&"}
return p.chop
end
end
end