Class: LazopApiClient::Client
- Inherits:
-
Object
- Object
- LazopApiClient::Client
- Defined in:
- lib/lazop_api_client.rb
Instance Method Summary collapse
- #execute(request, accessToken = nil) ⇒ Object
- #get_full_url(url, params) ⇒ Object
- #get_rest_url(url, api_name) ⇒ Object
-
#initialize(serverUrl, appkey, appSecret) ⇒ Client
constructor
A new instance of Client.
- #logApiError(requestUrl, code, message) ⇒ Object
- #perform_get(url, api_params, header_params) ⇒ Object
- #perform_post(url, api_params, file_params, header_params) ⇒ Object
- #setLogLevel(level) ⇒ Object
- #sign_api_request(sys_params, api_params, api_name) ⇒ Object
- #url_encode(str) ⇒ Object
Constructor Details
#initialize(serverUrl, appkey, appSecret) ⇒ Client
Returns a new instance of Client.
48 49 50 |
# File 'lib/lazop_api_client.rb', line 48 def initialize(serverUrl,appkey,appSecret) @serverUrl,@appkey,@appSecret = serverUrl,appkey,appSecret end |
Instance Method Details
#execute(request, accessToken = nil) ⇒ Object
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 |
# File 'lib/lazop_api_client.rb', line 52 def execute(request,accessToken = nil) sys_params = Hash.new sys_params[:app_key] = @appkey sys_params[:partner_id] = 'lazop-sdk-ruby-20180426' = request. if == nil = (Time.now.to_f * 1000).to_i end sys_params[:timestamp] = sys_params[:sign_method] = 'sha256' if @logLevel == Constants::Log_level_debug sys_params[:debug] = 'true' end if accessToken != nil sys_params[:access_token] = accessToken end sys_params[:sign] = sign_api_request(sys_params,request.api_params,request.api_name) rpcUrl = get_rest_url(@serverUrl,request.api_name) fullUrl = get_full_url(rpcUrl,sys_params) obj = nil begin if request.file_params.size() > 0 || request.http_method == 'POST' obj = perform_post(fullUrl,request.api_params,request.file_params,request.header_params) else obj = perform_get(fullUrl,request.api_params,request.header_params) end rescue Exception => e logApiError(fullUrl, "HTTP_ERROR", e.) raise end if obj['code'] != nil and obj['code'] != '0' logApiError(fullUrl, obj['code'], obj['message']) else if @logLevel == Constants::Log_level_debug or @logLevel == Constants::Log_level_info logApiError(fullUrl, '', '') end end return LazopApiClient::Response.new(obj['type'],obj['code'],obj['message'],obj['request_id'],obj) end |
#get_full_url(url, params) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/lazop_api_client.rb', line 180 def get_full_url(url,params) full_url = url param_str = '' params.each do |k,v| if param_str.length() > 0 param_str += '&' end param_str += k.to_s() param_str += '=' param_str += v.to_s() end full_url += '?' full_url += param_str return full_url end |
#get_rest_url(url, api_name) ⇒ Object
171 172 173 174 175 176 177 178 |
# File 'lib/lazop_api_client.rb', line 171 def get_rest_url(url,api_name) length = url.length() if url[length -1] == '/' return url + api_name.index('/'); end return url + api_name; end |
#logApiError(requestUrl, code, message) ⇒ Object
129 130 131 132 |
# File 'lib/lazop_api_client.rb', line 129 def logApiError requestUrl, code, localIp = IPSocket.getaddress(Socket.gethostname) $logger.error localIp + '^_^' + requestUrl + '^_^' + code + '^_^' + end |
#perform_get(url, api_params, header_params) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/lazop_api_client.rb', line 102 def perform_get url,api_params,header_params param_str = '' if api_params != nil api_params.each do |k,v| param_str += '&' param_str += k.to_s() param_str += '=' param_str += CGI.escape(v.to_s()) end end res = JSON.parse(RestClient.get(url + param_str, header_params)) return res end |
#perform_post(url, api_params, file_params, header_params) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/lazop_api_client.rb', line 134 def perform_post url, api_params,file_params,header_params all_params = api_params if file_params != nil file_params.each do |k,v| all_params[k] = File.open(v, "rb") end end res = JSON.parse(RestClient.post(url,all_params)) return res end |
#setLogLevel(level) ⇒ Object
121 122 123 |
# File 'lib/lazop_api_client.rb', line 121 def setLogLevel(level) @logLevel = level end |
#sign_api_request(sys_params, api_params, api_name) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/lazop_api_client.rb', line 148 def sign_api_request(sys_params,api_params,api_name) sort_arrays = nil if api_params != nil sort_arrays = sys_params.merge(api_params).sort_by do |k,v| k.to_s() end else sort_arrays = sys_params.sort_by do |k,v| k end end sign_str = '' sign_str += api_name sort_arrays.each do |k,v| sign_str += k.to_s() sign_str += v.to_s() end return OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), @appSecret, sign_str).upcase end |
#url_encode(str) ⇒ Object
125 126 127 |
# File 'lib/lazop_api_client.rb', line 125 def url_encode(str) return str.gsub!(/[^-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]]/n) { |x| x = format("%%%x", x[0])} end |