onebusaway-sdk 0.1.0.pre.alpha.205 → 0.1.0.pre.alpha.207
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +32 -20
- data/lib/onebusaway-sdk/base_client.rb +63 -91
- data/lib/onebusaway-sdk/base_model.rb +489 -514
- data/lib/onebusaway-sdk/base_page.rb +35 -15
- data/lib/onebusaway-sdk/client.rb +1 -3
- data/lib/onebusaway-sdk/errors.rb +15 -19
- data/lib/onebusaway-sdk/extern.rb +1 -4
- data/lib/onebusaway-sdk/models/references.rb +0 -21
- data/lib/onebusaway-sdk/models/report_problem_with_stop_retrieve_params.rb +0 -21
- data/lib/onebusaway-sdk/models/report_problem_with_trip_retrieve_params.rb +0 -23
- data/lib/onebusaway-sdk/pooled_net_requester.rb +53 -54
- data/lib/onebusaway-sdk/request_options.rb +4 -11
- data/lib/onebusaway-sdk/resources/agencies_with_coverage.rb +0 -2
- data/lib/onebusaway-sdk/resources/agency.rb +0 -2
- data/lib/onebusaway-sdk/resources/arrival_and_departure.rb +0 -3
- data/lib/onebusaway-sdk/resources/block.rb +0 -2
- data/lib/onebusaway-sdk/resources/config.rb +0 -2
- data/lib/onebusaway-sdk/resources/current_time.rb +0 -2
- data/lib/onebusaway-sdk/resources/report_problem_with_stop.rb +0 -2
- data/lib/onebusaway-sdk/resources/report_problem_with_trip.rb +0 -2
- data/lib/onebusaway-sdk/resources/route.rb +0 -2
- data/lib/onebusaway-sdk/resources/route_ids_for_agency.rb +0 -2
- data/lib/onebusaway-sdk/resources/routes_for_agency.rb +0 -2
- data/lib/onebusaway-sdk/resources/routes_for_location.rb +0 -2
- data/lib/onebusaway-sdk/resources/schedule_for_route.rb +0 -2
- data/lib/onebusaway-sdk/resources/schedule_for_stop.rb +0 -2
- data/lib/onebusaway-sdk/resources/search_for_route.rb +0 -2
- data/lib/onebusaway-sdk/resources/search_for_stop.rb +0 -2
- data/lib/onebusaway-sdk/resources/shape.rb +0 -2
- data/lib/onebusaway-sdk/resources/stop.rb +0 -2
- data/lib/onebusaway-sdk/resources/stop_ids_for_agency.rb +0 -2
- data/lib/onebusaway-sdk/resources/stops_for_agency.rb +0 -2
- data/lib/onebusaway-sdk/resources/stops_for_location.rb +0 -2
- data/lib/onebusaway-sdk/resources/stops_for_route.rb +0 -2
- data/lib/onebusaway-sdk/resources/trip.rb +0 -2
- data/lib/onebusaway-sdk/resources/trip_details.rb +0 -2
- data/lib/onebusaway-sdk/resources/trip_for_vehicle.rb +0 -2
- data/lib/onebusaway-sdk/resources/trips_for_location.rb +0 -2
- data/lib/onebusaway-sdk/resources/trips_for_route.rb +0 -2
- data/lib/onebusaway-sdk/resources/vehicles_for_agency.rb +0 -2
- data/lib/onebusaway-sdk/util.rb +297 -96
- data/lib/onebusaway-sdk/version.rb +1 -1
- data/lib/onebusaway-sdk.rb +1 -0
- data/manifest.yaml +1 -0
- data/rbi/lib/onebusaway-sdk/base_client.rbi +52 -35
- data/rbi/lib/onebusaway-sdk/base_model.rbi +400 -256
- data/rbi/lib/onebusaway-sdk/base_page.rbi +3 -4
- data/rbi/lib/onebusaway-sdk/client.rbi +7 -2
- data/rbi/lib/onebusaway-sdk/errors.rbi +44 -10
- data/rbi/lib/onebusaway-sdk/extern.rbi +1 -1
- data/rbi/lib/onebusaway-sdk/models/agencies_with_coverage_list_params.rbi +5 -2
- data/rbi/lib/onebusaway-sdk/models/agencies_with_coverage_list_response.rbi +9 -6
- data/rbi/lib/onebusaway-sdk/models/agency_retrieve_params.rbi +5 -2
- data/rbi/lib/onebusaway-sdk/models/agency_retrieve_response.rbi +6 -6
- data/rbi/lib/onebusaway-sdk/models/arrival_and_departure_list_params.rbi +5 -2
- data/rbi/lib/onebusaway-sdk/models/arrival_and_departure_list_response.rbi +95 -14
- data/rbi/lib/onebusaway-sdk/models/arrival_and_departure_retrieve_params.rbi +2 -9
- data/rbi/lib/onebusaway-sdk/models/arrival_and_departure_retrieve_response.rbi +93 -12
- data/rbi/lib/onebusaway-sdk/models/block_retrieve_params.rbi +5 -2
- data/rbi/lib/onebusaway-sdk/models/block_retrieve_response.rbi +14 -20
- data/rbi/lib/onebusaway-sdk/models/config_retrieve_params.rbi +5 -2
- data/rbi/lib/onebusaway-sdk/models/config_retrieve_response.rbi +8 -14
- data/rbi/lib/onebusaway-sdk/models/current_time_retrieve_params.rbi +5 -2
- data/rbi/lib/onebusaway-sdk/models/current_time_retrieve_response.rbi +6 -6
- data/rbi/lib/onebusaway-sdk/models/references.rbi +60 -43
- data/rbi/lib/onebusaway-sdk/models/report_problem_with_stop_retrieve_params.rbi +10 -6
- data/rbi/lib/onebusaway-sdk/models/report_problem_with_trip_retrieve_params.rbi +15 -6
- data/rbi/lib/onebusaway-sdk/models/response_wrapper.rbi +4 -2
- data/rbi/lib/onebusaway-sdk/models/route_ids_for_agency_list_params.rbi +5 -2
- data/rbi/lib/onebusaway-sdk/models/route_ids_for_agency_list_response.rbi +4 -4
- data/rbi/lib/onebusaway-sdk/models/route_retrieve_params.rbi +5 -2
- data/rbi/lib/onebusaway-sdk/models/route_retrieve_response.rbi +6 -6
- data/rbi/lib/onebusaway-sdk/models/routes_for_agency_list_params.rbi +5 -2
- data/rbi/lib/onebusaway-sdk/models/routes_for_agency_list_response.rbi +6 -6
- data/rbi/lib/onebusaway-sdk/models/routes_for_location_list_params.rbi +2 -2
- data/rbi/lib/onebusaway-sdk/models/routes_for_location_list_response.rbi +6 -6
- data/rbi/lib/onebusaway-sdk/models/schedule_for_route_retrieve_params.rbi +5 -8
- data/rbi/lib/onebusaway-sdk/models/schedule_for_route_retrieve_response.rbi +19 -16
- data/rbi/lib/onebusaway-sdk/models/schedule_for_stop_retrieve_params.rbi +5 -8
- data/rbi/lib/onebusaway-sdk/models/schedule_for_stop_retrieve_response.rbi +14 -14
- data/rbi/lib/onebusaway-sdk/models/search_for_route_list_params.rbi +4 -2
- data/rbi/lib/onebusaway-sdk/models/search_for_route_list_response.rbi +6 -6
- data/rbi/lib/onebusaway-sdk/models/search_for_stop_list_params.rbi +4 -2
- data/rbi/lib/onebusaway-sdk/models/search_for_stop_list_response.rbi +6 -6
- data/rbi/lib/onebusaway-sdk/models/shape_retrieve_params.rbi +5 -2
- data/rbi/lib/onebusaway-sdk/models/shape_retrieve_response.rbi +7 -6
- data/rbi/lib/onebusaway-sdk/models/stop_ids_for_agency_list_params.rbi +5 -2
- data/rbi/lib/onebusaway-sdk/models/stop_ids_for_agency_list_response.rbi +4 -4
- data/rbi/lib/onebusaway-sdk/models/stop_retrieve_params.rbi +5 -2
- data/rbi/lib/onebusaway-sdk/models/stop_retrieve_response.rbi +6 -6
- data/rbi/lib/onebusaway-sdk/models/stops_for_agency_list_params.rbi +5 -2
- data/rbi/lib/onebusaway-sdk/models/stops_for_agency_list_response.rbi +4 -4
- data/rbi/lib/onebusaway-sdk/models/stops_for_location_list_params.rbi +6 -2
- data/rbi/lib/onebusaway-sdk/models/stops_for_location_list_response.rbi +6 -6
- data/rbi/lib/onebusaway-sdk/models/stops_for_route_list_params.rbi +4 -2
- data/rbi/lib/onebusaway-sdk/models/stops_for_route_list_response.rbi +14 -14
- data/rbi/lib/onebusaway-sdk/models/trip_detail_retrieve_params.rbi +10 -2
- data/rbi/lib/onebusaway-sdk/models/trip_detail_retrieve_response.rbi +54 -23
- data/rbi/lib/onebusaway-sdk/models/trip_for_vehicle_retrieve_params.rbi +9 -8
- data/rbi/lib/onebusaway-sdk/models/trip_for_vehicle_retrieve_response.rbi +54 -23
- data/rbi/lib/onebusaway-sdk/models/trip_retrieve_params.rbi +5 -2
- data/rbi/lib/onebusaway-sdk/models/trip_retrieve_response.rbi +6 -6
- data/rbi/lib/onebusaway-sdk/models/trips_for_location_list_params.rbi +12 -12
- data/rbi/lib/onebusaway-sdk/models/trips_for_location_list_response.rbi +56 -16
- data/rbi/lib/onebusaway-sdk/models/trips_for_route_list_params.rbi +6 -2
- data/rbi/lib/onebusaway-sdk/models/trips_for_route_list_response.rbi +54 -16
- data/rbi/lib/onebusaway-sdk/models/vehicles_for_agency_list_params.rbi +3 -2
- data/rbi/lib/onebusaway-sdk/models/vehicles_for_agency_list_response.rbi +52 -14
- data/rbi/lib/onebusaway-sdk/pooled_net_requester.rbi +28 -13
- data/rbi/lib/onebusaway-sdk/request_options.rbi +25 -2
- data/rbi/lib/onebusaway-sdk/resources/agencies_with_coverage.rbi +4 -2
- data/rbi/lib/onebusaway-sdk/resources/agency.rbi +8 -3
- data/rbi/lib/onebusaway-sdk/resources/arrival_and_departure.rbi +14 -3
- data/rbi/lib/onebusaway-sdk/resources/block.rbi +8 -3
- data/rbi/lib/onebusaway-sdk/resources/config.rbi +3 -2
- data/rbi/lib/onebusaway-sdk/resources/current_time.rbi +3 -2
- data/rbi/lib/onebusaway-sdk/resources/report_problem_with_stop.rbi +9 -2
- data/rbi/lib/onebusaway-sdk/resources/report_problem_with_trip.rbi +14 -2
- data/rbi/lib/onebusaway-sdk/resources/route.rbi +8 -3
- data/rbi/lib/onebusaway-sdk/resources/route_ids_for_agency.rbi +8 -3
- data/rbi/lib/onebusaway-sdk/resources/routes_for_agency.rbi +8 -3
- data/rbi/lib/onebusaway-sdk/resources/routes_for_location.rbi +3 -2
- data/rbi/lib/onebusaway-sdk/resources/schedule_for_route.rbi +11 -3
- data/rbi/lib/onebusaway-sdk/resources/schedule_for_stop.rbi +11 -3
- data/rbi/lib/onebusaway-sdk/resources/search_for_route.rbi +10 -3
- data/rbi/lib/onebusaway-sdk/resources/search_for_stop.rbi +10 -3
- data/rbi/lib/onebusaway-sdk/resources/shape.rbi +8 -3
- data/rbi/lib/onebusaway-sdk/resources/stop.rbi +8 -3
- data/rbi/lib/onebusaway-sdk/resources/stop_ids_for_agency.rbi +8 -3
- data/rbi/lib/onebusaway-sdk/resources/stops_for_agency.rbi +8 -3
- data/rbi/lib/onebusaway-sdk/resources/stops_for_location.rbi +16 -3
- data/rbi/lib/onebusaway-sdk/resources/stops_for_route.rbi +12 -3
- data/rbi/lib/onebusaway-sdk/resources/trip.rbi +8 -3
- data/rbi/lib/onebusaway-sdk/resources/trip_details.rbi +12 -2
- data/rbi/lib/onebusaway-sdk/resources/trip_for_vehicle.rbi +11 -2
- data/rbi/lib/onebusaway-sdk/resources/trips_for_location.rbi +12 -2
- data/rbi/lib/onebusaway-sdk/resources/trips_for_route.rbi +15 -3
- data/rbi/lib/onebusaway-sdk/resources/vehicles_for_agency.rbi +10 -3
- data/rbi/lib/onebusaway-sdk/util.rbi +214 -83
- data/rbi/lib/onebusaway-sdk/version.rbi +1 -1
- data/sig/onebusaway-sdk/base_client.rbs +9 -8
- data/sig/onebusaway-sdk/base_model.rbs +8 -6
- data/sig/onebusaway-sdk/base_page.rbs +1 -1
- data/sig/onebusaway-sdk/errors.rbs +6 -5
- data/sig/onebusaway-sdk/pooled_net_requester.rbs +11 -4
- data/sig/onebusaway-sdk/request_options.rbs +4 -0
- data/sig/onebusaway-sdk/util.rbs +46 -2
- data/sig/onebusaway-sdk/version.rbs +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4afa1a6c65b03950bbef375b58bd9c893852b92f1b6a8e5d6316d41a2aede517
|
4
|
+
data.tar.gz: 655727298bb0188df296939c458ee293bead7cb75aef8bf662344606ca4104d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33a1c96f96d85f03dbc5d7100dbb859128b27108502b2ae2577a6e5ffb31ec89a1fc22cbdbdd0d6655e0bc441f85244a1684fb31f7341e812d004d23a34a233b
|
7
|
+
data.tar.gz: 044ddde2208e8ee77177a94af1003e931106a47e19e7cb51c9d7cf56e5847236d76d8e382b3285f2d3a5700e14da4d5d53d29e46c85765e687fb6e69a29a823a
|
data/README.md
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
# Onebusaway SDK Ruby API library
|
2
2
|
|
3
|
-
The Onebusaway SDK Ruby library provides convenient access to the Onebusaway SDK REST API from any Ruby 3.0.0+
|
4
|
-
application.
|
3
|
+
The Onebusaway SDK Ruby library provides convenient access to the Onebusaway SDK REST API from any Ruby 3.0.0+ application.
|
5
4
|
|
6
5
|
It is generated with [Stainless](https://www.stainless.com/).
|
7
6
|
|
8
7
|
## Documentation
|
9
8
|
|
10
|
-
Documentation for
|
9
|
+
Documentation for released of this gem can be found [on RubyDoc](https://gemdocs.org/gems/onebusaway-sdk).
|
11
10
|
|
12
11
|
The underlying REST API documentation can be found on [developer.onebusaway.org](https://developer.onebusaway.org).
|
13
12
|
|
14
13
|
## Installation
|
15
14
|
|
16
|
-
To use this gem during the beta, install directly from GitHub with Bundler by
|
17
|
-
adding the following to your application's `Gemfile`:
|
15
|
+
To use this gem during the beta, install directly from GitHub with Bundler by adding the following to your application's `Gemfile`:
|
18
16
|
|
19
17
|
```ruby
|
20
18
|
gem "onebusaway-sdk", git: "https://github.com/OneBusAway/ruby-sdk", branch: "main"
|
@@ -26,8 +24,7 @@ To fetch an initial copy of the gem:
|
|
26
24
|
bundle install
|
27
25
|
```
|
28
26
|
|
29
|
-
To update the version used by your application when updates are pushed to
|
30
|
-
GitHub:
|
27
|
+
To update the version used by your application when updates are pushed to GitHub:
|
31
28
|
|
32
29
|
```sh
|
33
30
|
bundle update onebusaway-sdk
|
@@ -44,13 +41,13 @@ onebusaway_sdk = OnebusawaySDK::Client.new(
|
|
44
41
|
)
|
45
42
|
|
46
43
|
current_time = onebusaway_sdk.current_time.retrieve
|
44
|
+
|
45
|
+
puts(current_time)
|
47
46
|
```
|
48
47
|
|
49
48
|
### Errors
|
50
49
|
|
51
|
-
When the library is unable to connect to the API, or if the API returns a
|
52
|
-
non-success status code (i.e., 4xx or 5xx response), a subclass of
|
53
|
-
`OnebusawaySDK::Error` will be thrown:
|
50
|
+
When the library is unable to connect to the API, or if the API returns a non-success status code (i.e., 4xx or 5xx response), a subclass of `OnebusawaySDK::Error` will be thrown:
|
54
51
|
|
55
52
|
```ruby
|
56
53
|
begin
|
@@ -78,10 +75,9 @@ Error codes are as followed:
|
|
78
75
|
|
79
76
|
### Retries
|
80
77
|
|
81
|
-
Certain errors will be automatically retried 2 times by default, with a short
|
82
|
-
|
83
|
-
connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, >=500 Internal errors,
|
84
|
-
and timeouts will all be retried by default.
|
78
|
+
Certain errors will be automatically retried 2 times by default, with a short exponential backoff.
|
79
|
+
|
80
|
+
Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, >=500 Internal errors, and timeouts will all be retried by default.
|
85
81
|
|
86
82
|
You can use the `max_retries` option to configure or disable this:
|
87
83
|
|
@@ -98,8 +94,8 @@ onebusaway_sdk.current_time.retrieve(request_options: {max_retries: 5})
|
|
98
94
|
### Timeouts
|
99
95
|
|
100
96
|
By default, requests will time out after 60 seconds.
|
101
|
-
|
102
|
-
so in some cases a request could wait 2\*timeout seconds before it fails.
|
97
|
+
|
98
|
+
Timeouts are applied separately to the initial connection and the overall request time, so in some cases a request could wait 2\*timeout seconds before it fails.
|
103
99
|
|
104
100
|
You can use the `timeout` option to configure or disable this:
|
105
101
|
|
@@ -113,12 +109,28 @@ onebusaway_sdk = OnebusawaySDK::Client.new(
|
|
113
109
|
onebusaway_sdk.current_time.retrieve(request_options: {timeout: 5})
|
114
110
|
```
|
115
111
|
|
112
|
+
## Sorbet Support
|
113
|
+
|
114
|
+
This library is written with [Sorbet type definitions](https://sorbet.org/docs/rbi). However, there is no runtime dependency on the `sorbet-runtime`.
|
115
|
+
|
116
|
+
What this means is that while you can use Sorbet to type check your code statically, and benefit from the [Sorbet Language Server](https://sorbet.org/docs/lsp) in your editor, there is no runtime type checking and execution overhead from Sorbet itself.
|
117
|
+
|
118
|
+
Due to limitations with the Sorbet type system, where a method otherwise can take an instance of `OnebusawaySDK::BaseModel` class, you will need to use the `**` splat operator to pass the arguments:
|
119
|
+
|
120
|
+
Please follow Sorbet's [setup guides](https://sorbet.org/docs/adopting) for best experience.
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
model = CurrentTimeRetrieveParams.new
|
124
|
+
|
125
|
+
onebusaway_sdk.current_time.retrieve(**model)
|
126
|
+
```
|
127
|
+
|
116
128
|
## Versioning
|
117
129
|
|
118
|
-
This package follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions. As the
|
119
|
-
|
120
|
-
|
130
|
+
This package follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions. As the library is in initial development and has a major version of `0`, APIs may change at any time.
|
131
|
+
|
132
|
+
This package considers improvements to the (non-runtime) `*.rbi` and `*.rbs` type definitions to be non-breaking changes.
|
121
133
|
|
122
134
|
## Requirements
|
123
135
|
|
124
|
-
Ruby 3.0 or higher.
|
136
|
+
Ruby 3.0.0 or higher.
|
@@ -1,10 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module OnebusawaySDK
|
4
|
-
# @private
|
4
|
+
# @api private
|
5
5
|
#
|
6
6
|
# @abstract
|
7
|
-
#
|
8
7
|
class BaseClient
|
9
8
|
# from whatwg fetch spec
|
10
9
|
MAX_REDIRECTS = 20
|
@@ -21,14 +20,13 @@ module OnebusawaySDK
|
|
21
20
|
# rubocop:enable Style/MutableConstant
|
22
21
|
|
23
22
|
class << self
|
24
|
-
# @private
|
23
|
+
# @api private
|
25
24
|
#
|
26
25
|
# @param req [Hash{Symbol=>Object}]
|
27
26
|
#
|
28
27
|
# @raise [ArgumentError]
|
29
|
-
#
|
30
28
|
def validate!(req)
|
31
|
-
keys = [:method, :path, :query, :headers, :body, :unwrap, :page, :model, :options]
|
29
|
+
keys = [:method, :path, :query, :headers, :body, :unwrap, :page, :stream, :model, :options]
|
32
30
|
case req
|
33
31
|
in Hash
|
34
32
|
req.each_key do |k|
|
@@ -41,13 +39,12 @@ module OnebusawaySDK
|
|
41
39
|
end
|
42
40
|
end
|
43
41
|
|
44
|
-
# @private
|
42
|
+
# @api private
|
45
43
|
#
|
46
44
|
# @param status [Integer]
|
47
45
|
# @param headers [Hash{String=>String}, Net::HTTPHeader]
|
48
46
|
#
|
49
47
|
# @return [Boolean]
|
50
|
-
#
|
51
48
|
def should_retry?(status, headers:)
|
52
49
|
coerced = OnebusawaySDK::Util.coerce_boolean(headers["x-should-retry"])
|
53
50
|
case [coerced, status]
|
@@ -65,7 +62,7 @@ module OnebusawaySDK
|
|
65
62
|
end
|
66
63
|
end
|
67
64
|
|
68
|
-
# @private
|
65
|
+
# @api private
|
69
66
|
#
|
70
67
|
# @param request [Hash{Symbol=>Object}] .
|
71
68
|
#
|
@@ -86,7 +83,6 @@ module OnebusawaySDK
|
|
86
83
|
# @param response_headers [Hash{String=>String}, Net::HTTPHeader]
|
87
84
|
#
|
88
85
|
# @return [Hash{Symbol=>Object}]
|
89
|
-
#
|
90
86
|
def follow_redirect(request, status:, response_headers:)
|
91
87
|
method, url, headers = request.fetch_values(:method, :url, :headers)
|
92
88
|
location =
|
@@ -128,14 +124,27 @@ module OnebusawaySDK
|
|
128
124
|
|
129
125
|
request
|
130
126
|
end
|
127
|
+
|
128
|
+
# @api private
|
129
|
+
#
|
130
|
+
# @param status [Integer, OnebusawaySDK::APIConnectionError]
|
131
|
+
# @param stream [Enumerable, nil]
|
132
|
+
def reap_connection!(status, stream:)
|
133
|
+
case status
|
134
|
+
in (..199) | (300..499)
|
135
|
+
stream&.each { next }
|
136
|
+
in OnebusawaySDK::APIConnectionError | (500..)
|
137
|
+
OnebusawaySDK::Util.close_fused!(stream)
|
138
|
+
else
|
139
|
+
end
|
140
|
+
end
|
131
141
|
end
|
132
142
|
|
133
|
-
# @private
|
134
|
-
#
|
143
|
+
# @api private
|
135
144
|
# @return [OnebusawaySDK::PooledNetRequester]
|
136
145
|
attr_accessor :requester
|
137
146
|
|
138
|
-
# @private
|
147
|
+
# @api private
|
139
148
|
#
|
140
149
|
# @param base_url [String]
|
141
150
|
# @param timeout [Float]
|
@@ -144,7 +153,6 @@ module OnebusawaySDK
|
|
144
153
|
# @param max_retry_delay [Float]
|
145
154
|
# @param headers [Hash{String=>String, Integer, Array<String, Integer, nil>, nil}]
|
146
155
|
# @param idempotency_header [String, nil]
|
147
|
-
#
|
148
156
|
def initialize(
|
149
157
|
base_url:,
|
150
158
|
timeout: 0.0,
|
@@ -171,19 +179,17 @@ module OnebusawaySDK
|
|
171
179
|
@max_retry_delay = max_retry_delay
|
172
180
|
end
|
173
181
|
|
174
|
-
# @private
|
182
|
+
# @api private
|
175
183
|
#
|
176
184
|
# @return [Hash{String=>String}]
|
177
|
-
#
|
178
185
|
private def auth_query = {}
|
179
186
|
|
180
|
-
# @private
|
187
|
+
# @api private
|
181
188
|
#
|
182
189
|
# @return [String]
|
183
|
-
#
|
184
190
|
private def generate_idempotency_key = "stainless-ruby-retry-#{SecureRandom.uuid}"
|
185
191
|
|
186
|
-
# @private
|
192
|
+
# @api private
|
187
193
|
#
|
188
194
|
# @param req [Hash{Symbol=>Object}] .
|
189
195
|
#
|
@@ -201,6 +207,8 @@ module OnebusawaySDK
|
|
201
207
|
#
|
202
208
|
# @option req [Class, nil] :page
|
203
209
|
#
|
210
|
+
# @option req [Class, nil] :stream
|
211
|
+
#
|
204
212
|
# @option req [OnebusawaySDK::Converter, Class, nil] :model
|
205
213
|
#
|
206
214
|
# @param opts [Hash{Symbol=>Object}] .
|
@@ -218,7 +226,6 @@ module OnebusawaySDK
|
|
218
226
|
# @option opts [Float, nil] :timeout
|
219
227
|
#
|
220
228
|
# @return [Hash{Symbol=>Object}]
|
221
|
-
#
|
222
229
|
private def build_request(req, opts)
|
223
230
|
method, uninterpolated_path = req.fetch_values(:method, :path)
|
224
231
|
|
@@ -243,8 +250,8 @@ module OnebusawaySDK
|
|
243
250
|
end
|
244
251
|
|
245
252
|
timeout = opts.fetch(:timeout, @timeout).to_f.clamp((0..))
|
246
|
-
unless headers.key?("x-stainless-
|
247
|
-
headers["x-stainless-
|
253
|
+
unless headers.key?("x-stainless-timeout") || timeout.zero?
|
254
|
+
headers["x-stainless-timeout"] = timeout.to_s
|
248
255
|
end
|
249
256
|
|
250
257
|
headers.reject! { |_, v| v.to_s.empty? }
|
@@ -268,13 +275,12 @@ module OnebusawaySDK
|
|
268
275
|
}
|
269
276
|
end
|
270
277
|
|
271
|
-
# @private
|
278
|
+
# @api private
|
272
279
|
#
|
273
280
|
# @param headers [Hash{String=>String}]
|
274
281
|
# @param retry_count [Integer]
|
275
282
|
#
|
276
283
|
# @return [Float]
|
277
|
-
#
|
278
284
|
private def retry_delay(headers, retry_count:)
|
279
285
|
# Non-standard extension
|
280
286
|
span = Float(headers["retry-after-ms"], exception: false)&.then { _1 / 1000 }
|
@@ -295,7 +301,7 @@ module OnebusawaySDK
|
|
295
301
|
(@initial_retry_delay * scale * jitter).clamp(0, @max_retry_delay)
|
296
302
|
end
|
297
303
|
|
298
|
-
# @private
|
304
|
+
# @api private
|
299
305
|
#
|
300
306
|
# @param request [Hash{Symbol=>Object}] .
|
301
307
|
#
|
@@ -318,8 +324,7 @@ module OnebusawaySDK
|
|
318
324
|
# @param send_retry_header [Boolean]
|
319
325
|
#
|
320
326
|
# @raise [OnebusawaySDK::APIError]
|
321
|
-
# @return [Array(Net::HTTPResponse, Enumerable)]
|
322
|
-
#
|
327
|
+
# @return [Array(Integer, Net::HTTPResponse, Enumerable)]
|
323
328
|
private def send_request(request, redirect_count:, retry_count:, send_retry_header:)
|
324
329
|
url, headers, max_retries, timeout = request.fetch_values(:url, :headers, :max_retries, :timeout)
|
325
330
|
input = {**request.except(:timeout), deadline: OnebusawaySDK::Util.monotonic_secs + timeout}
|
@@ -329,28 +334,23 @@ module OnebusawaySDK
|
|
329
334
|
end
|
330
335
|
|
331
336
|
begin
|
332
|
-
response, stream = @requester.execute(input)
|
333
|
-
status = Integer(response.code)
|
337
|
+
status, response, stream = @requester.execute(input)
|
334
338
|
rescue OnebusawaySDK::APIConnectionError => e
|
335
339
|
status = e
|
336
340
|
end
|
337
341
|
|
338
|
-
# normally we want to drain the response body and reuse the HTTP session by clearing the socket buffers
|
339
|
-
# unless we hit a server error
|
340
|
-
srv_fault = (500...).include?(status)
|
341
|
-
|
342
342
|
case status
|
343
343
|
in ..299
|
344
|
-
[response, stream]
|
344
|
+
[status, response, stream]
|
345
345
|
in 300..399 if redirect_count >= self.class::MAX_REDIRECTS
|
346
|
-
|
346
|
+
self.class.reap_connection!(status, stream: stream)
|
347
347
|
|
348
|
-
|
348
|
+
message = "Failed to complete the request within #{self.class::MAX_REDIRECTS} redirects."
|
349
349
|
raise OnebusawaySDK::APIConnectionError.new(url: url, message: message)
|
350
350
|
in 300..399
|
351
|
-
|
351
|
+
self.class.reap_connection!(status, stream: stream)
|
352
352
|
|
353
|
-
|
353
|
+
request = self.class.follow_redirect(request, status: status, response_headers: response)
|
354
354
|
send_request(
|
355
355
|
request,
|
356
356
|
redirect_count: redirect_count + 1,
|
@@ -359,13 +359,13 @@ module OnebusawaySDK
|
|
359
359
|
)
|
360
360
|
in OnebusawaySDK::APIConnectionError if retry_count >= max_retries
|
361
361
|
raise status
|
362
|
-
in (400..) if retry_count >= max_retries ||
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
362
|
+
in (400..) if retry_count >= max_retries || !self.class.should_retry?(status, headers: response)
|
363
|
+
decoded = Kernel.then do
|
364
|
+
OnebusawaySDK::Util.decode_content(response, stream: stream, suppress_error: true)
|
365
|
+
ensure
|
366
|
+
self.class.reap_connection!(status, stream: stream)
|
367
|
+
end
|
367
368
|
|
368
|
-
stream.each { srv_fault ? break : next }
|
369
369
|
raise OnebusawaySDK::APIStatusError.for(
|
370
370
|
url: url,
|
371
371
|
status: status,
|
@@ -374,9 +374,9 @@ module OnebusawaySDK
|
|
374
374
|
response: response
|
375
375
|
)
|
376
376
|
in (400..) | OnebusawaySDK::APIConnectionError
|
377
|
-
|
377
|
+
self.class.reap_connection!(status, stream: stream)
|
378
378
|
|
379
|
-
|
379
|
+
delay = retry_delay(response, retry_count: retry_count)
|
380
380
|
sleep(delay)
|
381
381
|
|
382
382
|
send_request(
|
@@ -388,48 +388,6 @@ module OnebusawaySDK
|
|
388
388
|
end
|
389
389
|
end
|
390
390
|
|
391
|
-
# @private
|
392
|
-
#
|
393
|
-
# @param req [Hash{Symbol=>Object}] .
|
394
|
-
#
|
395
|
-
# @option req [Symbol] :method
|
396
|
-
#
|
397
|
-
# @option req [String, Array<String>] :path
|
398
|
-
#
|
399
|
-
# @option req [Hash{String=>Array<String>, String, nil}, nil] :query
|
400
|
-
#
|
401
|
-
# @option req [Hash{String=>String, Integer, Array<String, Integer, nil>, nil}, nil] :headers
|
402
|
-
#
|
403
|
-
# @option req [Object, nil] :body
|
404
|
-
#
|
405
|
-
# @option req [Symbol, nil] :unwrap
|
406
|
-
#
|
407
|
-
# @option req [Class, nil] :page
|
408
|
-
#
|
409
|
-
# @option req [OnebusawaySDK::Converter, Class, nil] :model
|
410
|
-
#
|
411
|
-
# @option req [OnebusawaySDK::RequestOptions, Hash{Symbol=>Object}, nil] :options
|
412
|
-
#
|
413
|
-
# @param headers [Hash{String=>String}, Net::HTTPHeader]
|
414
|
-
#
|
415
|
-
# @param stream [Enumerable]
|
416
|
-
#
|
417
|
-
# @return [Object]
|
418
|
-
#
|
419
|
-
private def parse_response(req, headers:, stream:)
|
420
|
-
decoded = OnebusawaySDK::Util.decode_content(headers, stream: stream)
|
421
|
-
unwrapped = OnebusawaySDK::Util.dig(decoded, req[:unwrap])
|
422
|
-
|
423
|
-
case [req[:page], req.fetch(:model, OnebusawaySDK::Unknown)]
|
424
|
-
in [Class => page, _]
|
425
|
-
page.new(client: self, req: req, headers: headers, unwrapped: unwrapped)
|
426
|
-
in [nil, Class | OnebusawaySDK::Converter => model]
|
427
|
-
OnebusawaySDK::Converter.coerce(model, unwrapped)
|
428
|
-
in [nil, nil]
|
429
|
-
unwrapped
|
430
|
-
end
|
431
|
-
end
|
432
|
-
|
433
391
|
# Execute the request specified by `req`. This is the method that all resource
|
434
392
|
# methods call into.
|
435
393
|
#
|
@@ -449,35 +407,49 @@ module OnebusawaySDK
|
|
449
407
|
#
|
450
408
|
# @option req [Class, nil] :page
|
451
409
|
#
|
410
|
+
# @option req [Class, nil] :stream
|
411
|
+
#
|
452
412
|
# @option req [OnebusawaySDK::Converter, Class, nil] :model
|
453
413
|
#
|
454
414
|
# @option req [OnebusawaySDK::RequestOptions, Hash{Symbol=>Object}, nil] :options
|
455
415
|
#
|
456
416
|
# @raise [OnebusawaySDK::APIError]
|
457
417
|
# @return [Object]
|
458
|
-
#
|
459
418
|
def request(req)
|
460
419
|
self.class.validate!(req)
|
420
|
+
model = req.fetch(:model) { OnebusawaySDK::Unknown }
|
461
421
|
opts = req[:options].to_h
|
462
422
|
OnebusawaySDK::RequestOptions.validate!(opts)
|
463
423
|
request = build_request(req.except(:options), opts)
|
424
|
+
url = request.fetch(:url)
|
464
425
|
|
465
426
|
# Don't send the current retry count in the headers if the caller modified the header defaults.
|
466
427
|
send_retry_header = request.fetch(:headers)["x-stainless-retry-count"] == "0"
|
467
|
-
response, stream = send_request(
|
428
|
+
status, response, stream = send_request(
|
468
429
|
request,
|
469
430
|
redirect_count: 0,
|
470
431
|
retry_count: 0,
|
471
432
|
send_retry_header: send_retry_header
|
472
433
|
)
|
473
|
-
|
434
|
+
|
435
|
+
decoded = OnebusawaySDK::Util.decode_content(response, stream: stream)
|
436
|
+
case req
|
437
|
+
in { stream: Class => st }
|
438
|
+
st.new(model: model, url: url, status: status, response: response, messages: decoded)
|
439
|
+
in { page: Class => page }
|
440
|
+
page.new(client: self, req: req, headers: response, page_data: decoded)
|
441
|
+
else
|
442
|
+
unwrapped = OnebusawaySDK::Util.dig(decoded, req[:unwrap])
|
443
|
+
OnebusawaySDK::Converter.coerce(model, unwrapped)
|
444
|
+
end
|
474
445
|
end
|
475
446
|
|
476
447
|
# @return [String]
|
477
|
-
#
|
478
448
|
def inspect
|
449
|
+
# rubocop:disable Layout/LineLength
|
479
450
|
base_url = OnebusawaySDK::Util.unparse_uri(@base_url)
|
480
451
|
"#<#{self.class.name}:0x#{object_id.to_s(16)} base_url=#{base_url} max_retries=#{@max_retries} timeout=#{@timeout}>"
|
452
|
+
# rubocop:enable Layout/LineLength
|
481
453
|
end
|
482
454
|
end
|
483
455
|
end
|