Skip to content

Commit

Permalink
docs: Update README, add section Request ID
Browse files Browse the repository at this point in the history
  • Loading branch information
John Doe committed Oct 15, 2023
1 parent b469963 commit d9be4d0
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -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
51 changes: 51 additions & 0 deletions spec/cassettes/RongCloud_Client/HTTP_options/request_id.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions spec/rongcloud/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d9be4d0

Please sign in to comment.