toshi
Version, currently 0.1.12 versions
- 0.1.1latestApr 26, 2023
- 0.1.0not indexedApr 26, 2023
github.com/spoved/toshi.cr
No description declared in shard.yml.
Installation
# Add this to your shard.yml
dependencies:
toshi:
github: spoved/toshi.cr
version: ~> 0.1.1Then run:
shards installshard.yml
- Crystal
>= 1.6.0, < 2.0.0- License
- MIT
- Author
- Holden Omans
Dependencies
Runtime Dependencies
- db*github: crystal-lang/crystal-db
Development Dependencies
- spectator~> 0.11.6github: icy-arctic-fox/spectatordev
README
toshi
Toshi is a Crystal shard for building clients for JSON based apis. It includes a connection pooling, request throttling, and automatic JSON serialization and deserialization.
Installation
-
Add the dependency to your
shard.yml:dependencies: toshi: github: spoved/toshi.cr -
Run
shards install
Usage
require "toshi"
Defining an API
First include Toshi::Api in your class. Then call define_api to define the default configuration for the API.
class MyApi
include Toshi::Api
define_api("api.example.com", "https", 443, "/v1", OpenSSL::SSL::VerifyMode::PEER, HTTP::Headers{
"Content-Type" => "application/json",
"Accept" => "application/json",
}, 200, 20, 0.1, 0.0)
end
At a minimum, you must provide the host. The rest of the options are optional.
class MyApi
include Toshi::Api
define_api "api.example.com"
end
The default options are:
scheme:httpsport:443prefix:niltls_verify_mode:OpenSSL::SSL::VerifyMode::PEERdefault_headers:HTTP::Headers{ "Content-Type" => "application/json", "Accept" => "application/json" }sleep_time:0.0- the time to sleep between requests
The client pool options are:
pool_capacity:200- the maximum number of clients to createinitial_pool_size:20- the initial number of clients to createpool_timeout:0.1- the timeout to wait for a client to be available
Defining an API method
define_api_method defines a new API method based on the given path and method. The method name is derived from the path and method.
If the path includes a variable (e.g. /users/:id, /users/:id/books/:book_id), then the method will be defined with
the variables as the first arguments.
class MyApi
include Toshi::Api
define_api("api.example.com")
define_api_method :get, "/users/:id", User
define_api_method :get, "/users/:id/books/:book_id", UserBook
end
MyApi.get_users(id: 1) # => GET /users/1
MyApi.get_users_books(id: 1, book_id: 2) # => GET /users/1/books/2
You can also define the name of the generated method by passing it:
class MyApi
include Toshi::Api
define_api("api.example.com")
define_api_method :get, "/users/:id", User, :user_method
end
MyApi.user_method(id: 1) # => GET /users/1
If no response class is given, then the response will be nil.
Contributing
- Fork it (https://github.com/spoved/toshi.cr/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Contributors
- Holden Omans - creator and maintainer
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This release
- Version
0.1.1- Tagged
- Apr 26, 2023
- Commit
edbf1aaead8b- Crystal
>= 1.6.0, < 2.0.0- Indexed
- yes
Dependents
Repository
github.com/spoved/toshi.cr
Metadata
- Created
- Aug 19, 2026
- Updated
- Nov 13, 2026
- Synced
- Nov 13, 2026
- Versions
- 2