Class: Raas::Client::Rails::RestClient
- Inherits:
-
Object
- Object
- Raas::Client::Rails::RestClient
- Defined in:
- app/controllers/raas/client/rails/rest_client.rb
Overview
Rest Client for RaaS
Constant Summary collapse
- GET =
"get"- POST =
"post"- PUT =
"put"- DELETE =
"delete"- APEX_DOMAIN =
"functions.asaservice.inc"
Instance Method Summary collapse
- #createBody ⇒ Object
- #createExternalSession(msa, backUrl, subUrl, subDomain) ⇒ Object
- #decideDomain ⇒ Object
- #delete(requestUrl, params) ⇒ Object
- #get(requestUrl, params) ⇒ Object
- #getUserToken ⇒ Object
-
#getUserTokenImpl(msa) ⇒ Object
fetch token from RaaS because no cache.
-
#initialize(tenant, sub, tenant_alias, sub_alias, sub_domain, scope = nil) ⇒ RestClient
constructor
A new instance of RestClient.
- #post(requestUrl, body) ⇒ Object
- #proxy(body, method, requestUrl, params, token) ⇒ Object
- #put(requestUrl, body) ⇒ Object
Constructor Details
#initialize(tenant, sub, tenant_alias, sub_alias, sub_domain, scope = nil) ⇒ RestClient
Returns a new instance of RestClient.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 15 def initialize(tenant, sub, tenant_alias, sub_alias, sub_domain, scope = nil) @application = Raas::Client::Rails::Engine.config.raas_client_rails.application @landscape = Raas::Client::Rails::Engine.config.raas_client_rails.landscape @token = Raas::Client::Rails::Engine.config.raas_client_rails.token @tenant = tenant @sub = sub @tenant_alias = tenant_alias @sub_alias = sub_alias @sub_domain = sub_domain @scope = scope end |
Instance Method Details
#createBody ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 36 def createBody() body = Hash.new body["tenant"] = @tenant body["sub"] = @sub if @tenant_alias body["tenantAlias"] = @tenant_alias end if @sub_alias body["subAlias"] = @sub_alias end return body end |
#createExternalSession(msa, backUrl, subUrl, subDomain) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 27 def createExternalSession(msa,backUrl,subUrl,subDomain) body = createBody() body["backUrl"] = backUrl body["subUrl"] = subUrl body["subDomain"] = subDomain || @sub_domain || "" body["scope"] = @scope if @scope return proxy(body,POST,"/#{msa}/external/session",nil,@token) end |
#decideDomain ⇒ Object
156 157 158 159 160 161 162 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 156 def decideDomain() if "prod" == @landscape then return "#{@application}.#{APEX_DOMAIN}" else return "#{@application}.#{@landscape}.#{APEX_DOMAIN}" end end |
#delete(requestUrl, params) ⇒ Object
77 78 79 80 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 77 def delete(requestUrl, params) json = getUserToken() return proxy(nil, DELETE,requestUrl,params,json["token"]); end |
#get(requestUrl, params) ⇒ Object
72 73 74 75 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 72 def get(requestUrl, params) json = getUserToken() return proxy(nil, GET,requestUrl,params,json["token"]); end |
#getUserToken ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 49 def getUserToken() #No trace level... so that adding [TRACE] for filtering Rails.logger.debug("[TRACE] getUserToken is started"); #Use Rails default cache system, this simply use cache otherwise fetch and store Rails.logger.debug("Attempt to recycle token"); cacheName = "#{@tenant}+++#{@sub}" Rails.logger.debug("Cache name is #{cacheName}") # p cacheName return token = Rails.cache.fetch(cacheName) do Rails.logger.debug("No cache found, fetch it by api"); Rails.logger.debug("tenant #=> #{@tenant} / sub #=> #{@sub}") response = getUserTokenImpl("report"); Rails.logger.debug(response.body) return JSON.parse(response.body); end end |
#getUserTokenImpl(msa) ⇒ Object
fetch token from RaaS because no cache
67 68 69 70 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 67 def getUserTokenImpl(msa) body = createBody() response = proxy(body,POST,"/#{msa}/external/token",nil,@token); end |
#post(requestUrl, body) ⇒ Object
87 88 89 90 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 87 def post(requestUrl,body) json = getUserToken() return proxy(body , POST,requestUrl,nil,json["token"]); end |
#proxy(body, method, requestUrl, params, token) ⇒ Object
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 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 92 def proxy(body,method,requestUrl,params,token) Rails.logger.warn("method:#{method} url:#{requestUrl} token:#{token} params:#{params}" ); #TraceID seems to be different... is it OK!? traceId = SecureRandom.uuid; Rails.logger.debug("[TRACE] traceId=#{traceId}") # p "token: #{token}" domain = decideDomain(); uri = "https://#{domain}#{requestUrl}" url = URI.parse(uri) unless params.nil? #should I do like this!? parameters_hash = params.to_unsafe_h url.query = URI.encode_www_form(parameters_hash) unless params.nil? end # replacing context path form urI to match actual gateway URI headers = { 'TRACE' => traceId, 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => "Bearer #{token}" } #restTemplate.exchange alternative case method when GET httpEntity = Net::HTTP::Get.new(url.to_s,headers) when POST httpEntity = Net::HTTP::Post.new(url.to_s,headers) when DELETE httpEntity = Net::HTTP::Delete.new(url.to_s,headers) when PUT httpEntity = Net::HTTP::Put.new(url.to_s,headers) else puts "method is not be defined..." end httpEntity.body = body.to_json if body retry_count = 0 max_retries = 4 host = URI(url.to_s).host port = URI(url.to_s).port begin Rails.logger.debug(url.to_s); Rails.logger.debug(body); Rails.logger.debug(httpEntity); http = Net::HTTP.new(host,port) http.use_ssl = true # p url.to_s response = http.request(httpEntity) Rails.logger.debug(response); return response; rescue => e retry_count += 1 if retry_count <= max_retries Rails.logger.debug("Retry attempt #{retry_count}") sleep(4) #wait 4 seconds retry else Rails.logger.error("retry method for the following url #{uri} has failed #{e}"); Rails.logger.error(e.backtrace); raise e end end end |
#put(requestUrl, body) ⇒ Object
82 83 84 85 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 82 def put(requestUrl,body) json = getUserToken() return proxy(body , PUT,requestUrl,nil,json["token"]); end |