From d9be4d089189692daaba709c344122bda06e7895 Mon Sep 17 00:00:00 2001 From: John Doe Date: Sun, 15 Oct 2023 14:56:23 +0800 Subject: [PATCH] docs: Update README, add section `Request ID` --- README.md | 28 ++++++++++ bin/setup | 2 +- .../HTTP_options/request_id.yml | 51 +++++++++++++++++++ spec/rongcloud/client_spec.rb | 32 ++++++++++++ 4 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 spec/cassettes/RongCloud_Client/HTTP_options/request_id.yml diff --git a/README.md b/README.md index 6100d1a..20df7ae 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,34 @@ rongcloud = RongCloud::Client.new( ) ``` +### Request ID + +```ruby +class RequestID < HTTP::Feature + def wrap_request(req) + req.headers["X-Request-ID"] = make_request_id + req + end + + private + + def make_request_id + Thread.current[:request_id] || SecureRandom.uuid + end +end + +rongcloud = RongCloud::Client.new( + app_key: ENV["RONGCLOUD_APP_KEY"], + app_secret: ENV["RONGCLOUD_APP_SECRET"], + host: "api-cn.ronghub.com", + http: { + features: { + request_id: RequestID.new + } + } +) +``` + ### Signature Verify ```ruby diff --git a/bin/setup b/bin/setup index 81ff150..0959319 100755 --- a/bin/setup +++ b/bin/setup @@ -6,4 +6,4 @@ set -vx bundle install # Do any other automated setup that you need to do here -cp .env.defaults .env +cp -n .env.defaults .env || true diff --git a/spec/cassettes/RongCloud_Client/HTTP_options/request_id.yml b/spec/cassettes/RongCloud_Client/HTTP_options/request_id.yml new file mode 100644 index 0000000..8c271c2 --- /dev/null +++ b/spec/cassettes/RongCloud_Client/HTTP_options/request_id.yml @@ -0,0 +1,51 @@ +--- +http_interactions: +- request: + method: post + uri: https://api-cn.ronghub.com/user/getToken.json + body: + encoding: UTF-8 + string: userId=nutzer&name=John+Doe + headers: + Rc-App-Key: + - "" + Rc-Nonce: + - iAWsx1FnXzbw7uEo + Rc-Timestamp: + - '1697351338663' + Rc-Signature: + - 3b671aa26876dcd0b2ee0b9d8d2761378648c070 + Connection: + - close + Content-Type: + - application/x-www-form-urlencoded + Host: + - api-cn.ronghub.com + User-Agent: + - http.rb/5.1.0 + X-Request-Id: + - 2240ce89-d1e3-4252-84ce-372725eb0970 + response: + status: + code: 200 + message: OK + headers: + Content-Type: + - application/json;charset=UTF-8 + Transfer-Encoding: + - chunked + Connection: + - close + X-Request-Id: + - 2240ce89-d1e3-4252-84ce-372725eb0970 + Date: + - Sun, 15 Oct 2023 06:28:57 GMT + Server: + - APISIX + X-Apisix-Upstream-Status: + - '200' + body: + encoding: UTF-8 + string: '{"code":200,"userId":"nutzer","token":"35Ufb55yCBsDTweUTPFMpDDFDOTp/h6tmHhHEWfdYtk=@5r3v.cn.rongnav.com;5r3v.cn.rongcfg.com"}' + recorded_at: Sun, 15 Oct 2023 06:28:58 GMT +recorded_with: VCR 6.1.0 diff --git a/spec/rongcloud/client_spec.rb b/spec/rongcloud/client_spec.rb index d53f44b..d45009c 100644 --- a/spec/rongcloud/client_spec.rb +++ b/spec/rongcloud/client_spec.rb @@ -257,5 +257,37 @@ client.api.user.gettoken(userId: "nutzer", name: "John Doe") expect(strio.string).to include("> POST https://api-cn.ronghub.com/user/getToken.json") end + + it "request_id", :vcr do + request_id_klass = Class.new(HTTP::Feature) do + def wrap_request(req) + req.headers["X-Request-ID"] = make_request_id + req + end + + private + + def make_request_id + Thread.current[:request_id] || SecureRandom.uuid + end + end + + strio = StringIO.new + client = described_class.new( + app_key: ENV["RONGCLOUD_APP_KEY"], + app_secret: ENV["RONGCLOUD_APP_SECRET"], + host: "api-cn.ronghub.com", + http: { + features: { + request_id: request_id_klass.new, + logging: {logger: Logger.new(strio)} + } + } + ) + + Thread.current[:request_id] = "f0c525bb-4bd7-4e7a-980c-d1dfd24923ed" + client.api.user.gettoken(userId: "nutzer", name: "John Doe") + expect(strio.string).to include("X-Request-ID: f0c525bb-4bd7-4e7a-980c-d1dfd24923ed") + end end end