Skip to content

Commit

Permalink
feat: isolate platform headers (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored and stainless-ci-bot committed Feb 25, 2025
1 parent fb72692 commit 37ee661
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Steepfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ target :lib do

YAML.safe_load_file("./manifest.yaml", symbolize_names: true) => { dependencies: }
# currently these libraries lack the `*.rbs` annotations required by `steep`
stdlibs = dependencies - %w[etc net/http set stringio]
stdlibs = dependencies - %w[etc net/http rbconfig set stringio]

stdlibs.each { library(_1) }
end
1 change: 1 addition & 0 deletions lib/modern_treasury.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require "etc"
require "json"
require "net/http"
require "rbconfig"
require "securerandom"
require "set"
require "stringio"
Expand Down
29 changes: 17 additions & 12 deletions lib/modern_treasury/base_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ class BaseClient
# from whatwg fetch spec
MAX_REDIRECTS = 20

# rubocop:disable Style/MutableConstant
PLATFORM_HEADERS = {
"x-stainless-arch" => ModernTreasury::Util.arch,
"x-stainless-lang" => "ruby",
"x-stainless-os" => ModernTreasury::Util.os,
"x-stainless-package-version" => ModernTreasury::VERSION,
"x-stainless-runtime" => ::RUBY_ENGINE,
"x-stainless-runtime-version" => ::RUBY_ENGINE_VERSION
}
# rubocop:enable Style/MutableConstant

class << self
# @private
#
Expand Down Expand Up @@ -149,13 +160,10 @@ def initialize(
)
@requester = ModernTreasury::PooledNetRequester.new
@headers = ModernTreasury::Util.normalized_headers(
self.class::PLATFORM_HEADERS,
{
"X-Stainless-Lang" => "ruby",
"X-Stainless-Package-Version" => ModernTreasury::VERSION,
"X-Stainless-Runtime" => RUBY_ENGINE,
"X-Stainless-Runtime-Version" => RUBY_ENGINE_VERSION,
"Content-Type" => "application/json",
"Accept" => "application/json"
"accept" => "application/json",
"content-type" => "application/json"
},
headers
)
Expand Down Expand Up @@ -220,10 +228,7 @@ def initialize(

path = ModernTreasury::Util.interpolate_path(uninterpolated_path)

query = ModernTreasury::Util.deep_merge(
req[:query].to_h,
opts[:extra_query].to_h
)
query = ModernTreasury::Util.deep_merge(req[:query].to_h, opts[:extra_query].to_h)

headers = ModernTreasury::Util.normalized_headers(
@headers,
Expand Down Expand Up @@ -339,8 +344,8 @@ def initialize(
case status
in ..299
[response, stream]
in 300..399 if redirect_count >= MAX_REDIRECTS
message = "Failed to complete the request within #{MAX_REDIRECTS} redirects."
in 300..399 if redirect_count >= self.class::MAX_REDIRECTS
message = "Failed to complete the request within #{self.class::MAX_REDIRECTS} redirects."
raise ModernTreasury::APIConnectionError.new(url: url, message: message)
in 300..399
request = self.class.follow_redirect(request, status: status, response_headers: response)
Expand Down
42 changes: 42 additions & 0 deletions lib/modern_treasury/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,48 @@ module ModernTreasury
# @private
#
module Util
# @private
#
# @return [String]
#
def self.arch
case (arch = RbConfig::CONFIG["arch"])&.downcase
in nil
"unknown"
in /aarch64|arm64/
"arm64"
in /x86_64/
"x64"
in /arm/
"arm"
else
"other:#{arch}"
end
end

# @private
#
# @return [String]
#
def self.os
case (host = RbConfig::CONFIG["host_os"])&.downcase
in nil
"Unknown"
in /linux/
"Linux"
in /darwin/
"MacOS"
in /freebsd/
"FreeBSD"
in /openbsd/
"OpenBSD"
in /mswin|mingw|cygwin|ucrt/
"Windows"
else
"Other:#{host}"
end
end

# @private
#
# @param input [Object]
Expand Down
1 change: 1 addition & 0 deletions manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dependencies:
- etc
- json
- net/http
- rbconfig
- securerandom
- set
- stringio
Expand Down
2 changes: 2 additions & 0 deletions rbi/lib/modern_treasury/base_client.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ module ModernTreasury

MAX_REDIRECTS = 20

PLATFORM_HEADERS = T::Hash[String, String]

sig { params(req: ModernTreasury::BaseClient::RequestComponentsShape).void }
def self.validate!(req)
end
Expand Down
8 changes: 8 additions & 0 deletions rbi/lib/modern_treasury/util.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

module ModernTreasury
module Util
sig { returns(String) }
def self.arch
end

sig { returns(String) }
def self.os
end

sig { params(input: T.anything).returns(T.any(T::Boolean, T.anything)) }
def self.primitive?(input)
end
Expand Down
2 changes: 2 additions & 0 deletions sig/modern_treasury/base_client.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ module ModernTreasury

MAX_REDIRECTS: 20

PLATFORM_HEADERS: ::Hash[String, String]

def self.validate!: (
ModernTreasury::BaseClient::request_components req
) -> void
Expand Down
4 changes: 4 additions & 0 deletions sig/modern_treasury/util.rbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module ModernTreasury
module Util
def self?.arch: -> String

def self?.os: -> String

def self?.primitive?: (top input) -> (bool | top)

def self?.coerce_boolean: (top input) -> (bool | top)
Expand Down
6 changes: 2 additions & 4 deletions test/modern_treasury/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,7 @@ def test_default_headers
modern_treasury.counterparties.create(name: "name")
headers = requester.attempts.first[:headers]

refute_empty(headers["x-stainless-lang"])
refute_empty(headers["x-stainless-package-version"])
refute_empty(headers["x-stainless-runtime"])
refute_empty(headers["x-stainless-runtime-version"])
refute_empty(headers["accept"])
refute_empty(headers["content-type"])
end
end

0 comments on commit 37ee661

Please sign in to comment.