You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+93-53Lines changed: 93 additions & 53 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Lithic Ruby API library
2
2
3
-
The Lithic Ruby library provides convenient access to the Lithic REST API from any Ruby 3.2.0+ application.
3
+
The Lithic Ruby library provides convenient access to the Lithic REST API from any Ruby 3.2.0+ application. It ships with comprehensive types & docstrings in Yard, RBS, and RBI – [see below](https://github.com/lithic-com/lithic-ruby#Sorbet) for usage with Sorbet. The standard library's `net/http` is used as the HTTP transport, with connection pooling via the `connection_pool` gem.
This library is written with [Sorbet type definitions](https://sorbet.org/docs/rbi). However, there is no runtime dependency on the `sorbet-runtime`.
42
-
43
-
When using sorbet, it is recommended to use model classes as below. This provides stronger type checking and tooling integration.
44
-
45
-
```ruby
46
-
lithic.cards.create(type:"SINGLE_USE")
47
-
```
48
-
49
39
### Pagination
50
40
51
41
List methods in the Lithic API are paginated.
@@ -65,15 +55,30 @@ page.auto_paging_each do |card|
65
55
end
66
56
```
67
57
68
-
### Errors
58
+
Alternatively, you can use the `#next_page?` and `#next_page` methods for more granular control working with pages.
59
+
60
+
```ruby
61
+
if page.next_page?
62
+
new_page = page.next_page
63
+
puts(new_page.data[0].product_id)
64
+
end
65
+
```
66
+
67
+
### Handling errors
69
68
70
69
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 `Lithic::Errors::APIError` will be thrown:
By default, requests will time out after 60 seconds.
117
-
118
-
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.
119
-
120
-
You can use the `timeout` option to configure or disable this:
121
+
By default, requests will time out after 60 seconds. You can use the timeout option to configure or disable this:
On timeout, `Lithic::Errors::APITimeoutError` is raised.
133
134
134
-
This library uses a simple DSL to represent request parameters and response shapes in `lib/lithic/models`.
135
+
Note that requests that time out are retried by default.
135
136
136
-
With the right [editor plugins](https://shopify.github.io/ruby-lsp), you can ctrl-click on elements of the DSL to navigate around and explore the library.
137
+
## Advanced concepts
137
138
138
-
In all places where a `BaseModel` type is specified, vanilla Ruby `Hash` can also be used. For example, the following are interchangeable as arguments:
139
+
### BaseModel
139
140
140
-
```ruby
141
-
# This has tooling readability, for auto-completion, static analysis, and goto definition with supported language services
All parameter and response objects inherit from `Lithic::Internal::Type::BaseModel`, which provides several conveniences, including:
143
142
144
-
# This also works
145
-
params = {
146
-
type:"SINGLE_USE"
147
-
}
148
-
```
143
+
1. All fields, including unknown ones, are accessible with `obj[:prop]` syntax, and can be destructured with `obj => {prop: prop}` or pattern-matching syntax.
149
144
150
-
## Editor support
145
+
2. Structural equivalence for equality; if two API calls return the same values, comparing the responses with == will return true.
151
146
152
-
A combination of [Shopify LSP](https://shopify.github.io/ruby-lsp)and [Solargraph](https://solargraph.org/) is recommended for non-[Sorbet](https://sorbet.org) users. The former is especially good at go to definition, while the latter has much better auto-completion support.
147
+
3. Both instances and the classes themselves can be pretty-printed.
153
148
154
-
## Advanced concepts
149
+
4. Helpers such as `#to_h`, `#deep_to_h`, `#to_json`, and `#to_yaml`.
150
+
151
+
### Making custom or undocumented requests
152
+
153
+
#### Undocumented properties
154
+
155
+
You can send undocumented parameters to any endpoint, and read undocumented response properties, like so:
155
156
156
-
### Making custom/undocumented requests
157
+
Note: the `extra_` parameters of the same name overrides the documented parameters.
158
+
159
+
```ruby
160
+
page =
161
+
lithic.cards.list(
162
+
page_size:10,
163
+
request_options: {
164
+
extra_query: {my_query_parameter: value},
165
+
extra_body: {my_body_parameter: value},
166
+
extra_headers: {"my-header": value}
167
+
}
168
+
)
169
+
170
+
puts(page[:my_undocumented_property])
171
+
```
157
172
158
173
#### Undocumented request params
159
174
160
-
If you want to explicitly send an extra param, you can do so with the `extra_query`, `extra_body`, and `extra_headers` under the `request_options:` parameter when making a requests as seen in examples above.
175
+
If you want to explicitly send an extra param, you can do so with the `extra_query`, `extra_body`, and `extra_headers` under the `request_options:` parameter when making a request as seen in examples above.
161
176
162
177
#### Undocumented endpoints
163
178
164
-
To make requests to undocumented endpoints, you can make requests using `client.request`. Options on the client will be respected (such as retries) when making this request.
179
+
To make requests to undocumented endpoints while retaining the benefit of auth, retries, and so on, you can make requests using `client.request`, like so:
165
180
166
181
```ruby
167
182
response = client.request(
168
183
method::post,
169
184
path:'/undocumented/endpoint',
170
185
query: {"dog": "woof"},
171
186
headers: {"useful-header": "interesting-value"},
172
-
body: {"he": "llo"},
187
+
body: {"hello": "world"}
173
188
)
174
189
```
175
190
176
191
### Concurrency & connection pooling
177
192
178
-
The `Lithic::Client` instances are thread-safe, and should be re-used across multiple threads. By default, each `Client` have their own HTTP connection pool, with a maximum number of connections equal to thread count.
193
+
The `Lithic::Client` instances are threadsafe, but only are fork-safe when there are no in-flight HTTP requests.
179
194
180
-
When the maximum number of connections has been checked out from the connection pool, the `Client` will wait for an in use connection to become available. The queue time for this mechanism is accounted for by the per-request timeout.
195
+
Each instance of `Lithic::Client` has its own HTTP connection pool with a default size of 99. As such, we recommend instantiating the client once per application in most settings.
196
+
197
+
When all available connections from the pool are checked out, requests wait for a new connection to become available, with queue time counting towards the request timeout.
181
198
182
199
Unless otherwise specified, other classes in the SDK do not have locks protecting their underlying data structure.
183
200
184
-
Currently, `Lithic::Client` instances are only fork-safe if there are no in-flight HTTP requests.
201
+
## Sorbet
185
202
186
-
### Sorbet
203
+
This library provides comprehensive [RBI](https://sorbet.org/docs/rbi) definitions, and has no dependency on sorbet-runtime.
187
204
188
-
#### Enums
205
+
You can provide typesafe request parameters like so:
189
206
190
-
Sorbet's typed enums require sub-classing of the [`T::Enum` class](https://sorbet.org/docs/tenum) from the `sorbet-runtime` gem.
207
+
```ruby
208
+
lithic.cards.create(type:"SINGLE_USE")
209
+
```
191
210
192
-
Since this library does not depend on `sorbet-runtime`, it uses a [`T.all` intersection type](https://sorbet.org/docs/intersection-types) with a ruby primitive type to construct a "tagged alias" instead.
211
+
Or, equivalently:
193
212
194
213
```ruby
195
-
moduleLithic::AccountFinancialAccountType
196
-
# This alias aids language service driven navigation.
It is possible to pass a compatible model / parameter class to a method that expects keyword arguments by using the `**` splat operator.
224
+
Since this library does not depend on `sorbet-runtime`, it cannot provide [`T::Enum`](https://sorbet.org/docs/tenum) instances. Instead, we provide "tagged symbols" instead, which is always a primitive at runtime:
0 commit comments