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
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 35 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 body["scope"] = @scope if @scope return body end |
#createExternalSession(msa, backUrl, subUrl, subDomain) ⇒ Object
27 28 29 30 31 32 33 |
# 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 || "" return proxy(body,POST,"/#{msa}/external/session",nil,@token) end |
#decideDomain ⇒ Object
165 166 167 168 169 170 171 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 165 def decideDomain() if "prod" == @landscape then return "#{@application}.#{APEX_DOMAIN}" else return "#{@application}.#{@landscape}.#{APEX_DOMAIN}" end end |
#delete(requestUrl, params) ⇒ Object
85 86 87 88 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 85 def delete(requestUrl, params) json = getUserToken() return proxy(nil, DELETE,requestUrl,params,json["token"]); end |
#get(requestUrl, params) ⇒ Object
80 81 82 83 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 80 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 65 66 67 68 69 70 71 72 |
# 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"); # scope 付きトークンは scope 単位でキャッシュし、切り替え時に再取得する if @scope cacheName = "#{@tenant}+++#{@sub}+++#{@scope}" else cacheName = "#{@tenant}+++#{@sub}" end Rails.logger.debug("Cache name is #{cacheName}") # p cacheName cached = Rails.cache.read(cacheName) return cached if cached 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) json = JSON.parse(response.body); # エラー応答 (invalid_scope の 400 等) を永続キャッシュしないよう成功時のみ保存する Rails.cache.write(cacheName, json) if response.is_a?(Net::HTTPSuccess) return json end |
#getUserTokenImpl(msa) ⇒ Object
fetch token from RaaS because no cache
75 76 77 78 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 75 def getUserTokenImpl(msa) body = createBody() response = proxy(body,POST,"/#{msa}/external/token",nil,@token); end |
#post(requestUrl, body) ⇒ Object
95 96 97 98 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 95 def post(requestUrl,body) json = getUserToken() return proxy(body , POST,requestUrl,nil,json["token"]); end |
#proxy(body, method, requestUrl, params, token) ⇒ Object
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 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 100 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!? # ActionController::Parameters 以外 (素の Hash) で渡されることもある parameters_hash = params.respond_to?(:to_unsafe_h) ? params.to_unsafe_h : params 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
90 91 92 93 |
# File 'app/controllers/raas/client/rails/rest_client.rb', line 90 def put(requestUrl,body) json = getUserToken() return proxy(body , PUT,requestUrl,nil,json["token"]); end |