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) ⇒ 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) ⇒ RestClient
Returns a new instance of RestClient.
15 16 17 18 19 20 21 22 23 24 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 15 def initialize(tenant, sub, tenant_alias, sub_alias, sub_domain) @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 end |
Instance Method Details
#createBody ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 34 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
26 27 28 29 30 31 32 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 26 def createExternalSession(msa,backUrl,subUrl,subDomain) body = createBody() body["backUrl"] = backUrl body["subUrl"] = subUrl body["subDomain"] = subDomain || @sub_domain || "" return proxy(body,POST,"/#{msa}/external/session",nil,@token) end |
#decideDomain ⇒ Object
154 155 156 157 158 159 160 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 154 def decideDomain() if "prod" == @landscape then return "#{@application}.#{APEX_DOMAIN}" else return "#{@application}.#{@landscape}.#{APEX_DOMAIN}" end end |
#delete(requestUrl, params) ⇒ Object
75 76 77 78 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 75 def delete(requestUrl, params) json = getUserToken() return proxy(nil, DELETE,requestUrl,params,json["token"]); end |
#get(requestUrl, params) ⇒ Object
70 71 72 73 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 70 def get(requestUrl, params) json = getUserToken() return proxy(nil, GET,requestUrl,params,json["token"]); end |
#getUserToken ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 47 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
65 66 67 68 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 65 def getUserTokenImpl(msa) body = createBody() response = proxy(body,POST,"/#{msa}/external/token",nil,@token); end |
#post(requestUrl, body) ⇒ Object
85 86 87 88 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 85 def post(requestUrl,body) json = getUserToken() return proxy(body , POST,requestUrl,nil,json["token"]); end |
#proxy(body, method, requestUrl, params, token) ⇒ Object
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 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 90 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
80 81 82 83 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 80 def put(requestUrl,body) json = getUserToken() return proxy(body , PUT,requestUrl,nil,json["token"]); end |