Class: CiscoWebex::RestCC
- Inherits:
-
Object
- Object
- CiscoWebex::RestCC
- Defined in:
- lib/REST/RestCC.rb
Class Method Summary collapse
-
.delete(auth_token, uri, params = nil) ⇒ Object
basid REST DELETE to Webex teams.
-
.get(auth_token, uri, params = {}, limit = 50000) ⇒ Object
basid REST GET to Webex teams.
-
.head(auth_token, uri, params = nil) ⇒ Object
basid REST GET to Webex teams.
-
.parse_params(params, return_format = "json") ⇒ Object
format params for passing as needed json/urlencoded.
-
.patch(auth_token, uri, params) ⇒ Object
basic REST PUT for Webex.
-
.post(auth_token, uri, params) ⇒ Object
basic REST POST for Webex.
-
.put(auth_token, uri, params) ⇒ Object
basic REST PUT for Webex.
- .send_rest(rest, request, limit, format = "array") ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ RestCC
constructor
A new instance of RestCC.
Constructor Details
#initialize ⇒ RestCC
Returns a new instance of RestCC.
10 11 12 |
# File 'lib/REST/RestCC.rb', line 10 def initialize() puts "Initializing CiscoWebex::CC::Rest not required..." end |
Class Method Details
.delete(auth_token, uri, params = nil) ⇒ Object
basid REST DELETE to Webex teams
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/REST/RestCC.rb', line 168 def self.delete(auth_token, uri, params=nil) # set REST client headers, body etc if params.class == String uri = "#{uri}/#{params}" params = {} elsif params.to_h.has_key?("id") uri = "#{uri}/#{params.to_h['id']}" params = {} end uri = URI("#{@BASE_URL}#{uri}") uri.query = parse_params(params, "www-encoded") if params # encode and set params if required https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true request = Net::HTTP::Delete.new(uri) request["Authorization"] = "Bearer #{auth_token}" request["User-Agent"] = "CiscoWebex for Ruby by Cloverhound" return send_rest(https, request) # send REST call end |
.get(auth_token, uri, params = {}, limit = 50000) ⇒ Object
basid REST GET to Webex teams
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 |
# File 'lib/REST/RestCC.rb', line 71 def self.get(auth_token, uri, params={}, limit=50000) if params.class != Hash # fix missing params limit = params params = {} end if params.class == Hash && params.has_key?('orgId') orig_id = params['orgId'] else org_id = uri.split('/')[2] end result = { 'data'=> [], 'meta'=> {'orgId'=> org_id, 'library_added'=> true} } if ( params.class == Hash and ! params.has_key?('page') ) # add page for looping params['page'] = 0 end if ( params.class == Hash and ! params.has_key?('pageSize') ) # add page for looping params['pageSize'] = 100 end # provide looping for longer requests while true # set REST client headers, body etc api_uri = "#{uri}?#{parse_params(params, "www-encoded") if (params.class == Hash || params.class == OpenStruct)}" url = URI(@BASE_URL) https = Net::HTTP.new(url.host, url.port) https.use_ssl = true request = Net::HTTP::Get.new(api_uri) request["Authorization"] = "Bearer #{auth_token}" request["User-Agent"] = "CiscoWebex for Ruby by Cloverhound" rest_response = send_rest(https, request, limit, "array") # send REST call if rest_response == false return false else # parse return based on v1/v2 for set common format of v1 if rest_response.class == Hash && rest_response.has_key?('data') # try and check of we need to get the next page. # if response length is 0 then no need to pull again # if response length is less that the requested page size then no need # Paging not supported on all APIs so if response is larger than the requested page size then concider it done if rest_response['data'].length() == 0 || rest_response['data'].length() < params['pageSize']|| rest_response['data'].length() > params['pageSize'] result['data'] = rest_response['data'][0..(limit - 1)] result['meta'] = rest_response['meta'] return result end result['data'].concat(rest_response['data']) result['meta'] = rest_response['meta'] elsif rest_response.class == Array # try and check of we need to get the next page. # if response length is 0 then no need to pull again # if response length is less that the requested page size then no need # Paging not supported on all APIs so if response is larger than the requested page size then concider it done if rest_response.length() == 0 || rest_response.length() < params['pageSize'] || rest_response.length() > params['pageSize'] result['data'] = rest_response[0..(limit - 1)] return result end result['data'].concat(rest_response) else return result end # check if limit is met and return if so if result['data'].length() >= limit result['data'] = result['data'][0..(limit - 1)] return result end params['page'] += 1 end end end |
.head(auth_token, uri, params = nil) ⇒ Object
basid REST GET to Webex teams
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/REST/RestCC.rb', line 147 def self.head(auth_token, uri, params=nil) # set REST client headers, body etc if params.class == String uri = "#{uri}/#{params}" elsif params.has_key?("id") uri = "#{uri}/#{params['id']}" params.delete('id') end uri = URI("#{@BASE_URL}#{uri}") uri.query = parse_params(params, "www-encoded") if params # encode and set params if required https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true request = Net::HTTP::Head.new(uri) request["Authorization"] = "Bearer #{auth_token}" request["User-Agent"] = "CiscoWebex for Ruby by Cloverhound" return send_rest(https, request) # send REST call end |
.parse_params(params, return_format = "json") ⇒ Object
format params for passing as needed json/urlencoded
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/REST/RestCC.rb', line 15 def self.parse_params(params, return_format="json") return params if ( return_format == "json" && params.class == String && JSON.parse(params) ) # if already json then just send back if params.class == OpenStruct params = params.to_h end for param in params.each() if param[1] == nil || ( params[1].class == String && param[1] == "" ) || ( ( params.class == Array || params.class == Hash ) && params.length == 0 ) params.delete(param[0]) end end case return_format when "json" params = params.to_json when "www-encoded" params = URI.encode_www_form(params) end return params end |
.patch(auth_token, uri, params) ⇒ Object
basic REST PUT for Webex
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/REST/RestCC.rb', line 234 def self.patch(auth_token, uri, params) # build REST client/headers/body etc if params.class != Hash || params.length == 0 STDERR.puts "CiscoWebex::RestCC.patch(): Must supply `params` as hash for update" return False elsif params.to_h.has_key?("id") uri = "#{uri}/#{params['id']}" end uri = URI("#{@BASE_URL}#{uri}") https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true request = Net::HTTP::Patch.new(uri) request["Authorization"] = "Bearer #{auth_token}" request["Content-Type"] = "application/json" request["Accept"] = "*/*" request["User-Agent"] = "CiscoWebex for Ruby by Cloverhound" request.body = parse_params(params) # format params as json body return send_rest(https, request) # send REST call end |
.post(auth_token, uri, params) ⇒ Object
basic REST POST for Webex
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/REST/RestCC.rb', line 190 def self.post(auth_token, uri, params) # build REST client/headers/body etc if params.class != Hash || params.length == 0 STDERR.puts "CiscoWebex::RestCC.put(): Must supply `params` as hash for update" return False elsif params.to_h.has_key?("id") uri = "#{uri}/#{params['id']}" end uri = URI("#{@BASE_URL}#{uri}") https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true request = Net::HTTP::Post.new(uri) request["Authorization"] = "Bearer #{auth_token}" request["Content-Type"] = "application/json" request["Accept"] = "*/*" request["User-Agent"] = "CiscoWebex for Ruby by Cloverhound" request.body = parse_params(params) # format params as json body return send_rest(https, request) # send REST call end |
.put(auth_token, uri, params) ⇒ Object
basic REST PUT for Webex
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/REST/RestCC.rb', line 212 def self.put(auth_token, uri, params) # build REST client/headers/body etc if params.class != Hash || params.length == 0 STDERR.puts "CiscoWebex::RestCC.put(): Must supply `params` as hash for update" return False elsif params.to_h.has_key?("id") uri = "#{uri}/#{params['id']}" end uri = URI("#{@BASE_URL}#{uri}") https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true request = Net::HTTP::Put.new(uri) request["Authorization"] = "Bearer #{auth_token}" request["Content-Type"] = "application/json" request["Accept"] = "*/*" request["User-Agent"] = "CiscoWebex for Ruby by Cloverhound" request.body = parse_params(params) # format params as json body return send_rest(https, request) # send REST call end |
.send_rest(rest, request, limit, format = "array") ⇒ Object
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 |
# File 'lib/REST/RestCC.rb', line 40 def self.send_rest(rest, request, limit, format="array") # loop adjusts for throttling if needed results = [] while true response = rest.request(request) headers, response_code = [ response.each_header.to_h, response.code ] case response_code when "204" return true when "200" # if REST success then parse body to JSON response = JSON.parse(response.body) return response if format == "array" # If not an array then convert hash to object and return return OpenStruct.new(response) if format == "object" # If not an array then convert hash to object and return when "429" # sleep for throttling time puts "Throttled. Waiting #{response.each_header.to_h['retry-after']}..." sleep(response.each_header.to_h['retry-after'].to_i + 1) puts "\t... and go." else STDERR.puts "CC REST ERROR: " + response.to_s + response.body.to_s return false end end return OpenStruct.new(response) if format == "object" return response if format == "array" end |