Skip to content

Commit

Permalink
Add proxy configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
iwdt committed Feb 24, 2024
1 parent 855e8d3 commit 1900dec
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 18 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,12 @@ client.v1.anime_videos(anime_id, first_param: 1, second_param: 2)
client.v1.animes(headers: { 'X-Header-Name' => 'My header value' })
```

For configuring a connection through proxy, you can pass `proxy_host`, `proxy_port`, `proxy_user` and `proxy_password` as arguments during client initialization:

```ruby
client = Shikimori::API::Client.new(proxy_host: 'http://my-proxy', proxy_port: 8080, proxy_user: 'my_proxy_user', proxy_password: 'my_proxy_password')
```

## Supported Ruby Versions
This library aims to support and is tested against the following Ruby implementations:
* Ruby 3.0
Expand Down
31 changes: 17 additions & 14 deletions lib/shikimori/api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,23 @@ class Client

attr_reader :v1, :v2

def initialize(site = DEFAULT_SITE_URL, app_name: nil, access_token: nil, refresh_token: nil)
rest = REST.new(
access_token: access_token,
refresh_token: refresh_token,
app_name: app_name
)
@v1 = V1.new(
base_url: URI.new(site).join('api/'),
rest: rest
)
@v2 = V2.new(
base_url: URI.new(site).join('api/v2/'),
rest: rest
)
# Initialize a Shikimori's API Client
#
# @param site [String] Shikimori's base url
# @param options [Hash] Additional options to making requests to shikimori
# @option options [String] :app_name OAuth2 application name
# @option options [String] :access_token User's access token from OAuth2
# @option options [String] :refresh_token User's refresh token from OAuth2
# @option options [String] :proxy_host Proxy hostname
# @option options [Integer] :proxy_port Proxy port
# @option options [String] :proxy_user Proxy username
# @option options [String] :proxy_password Proxy password
def initialize(site = DEFAULT_SITE_URL, **options)
rest = REST.new(**options)
base_uri = URI.new(site)

@v1 = V1.new(base_url: base_uri.join('api/'), rest: rest)
@v2 = V2.new(base_url: base_uri.join('api/v2/'), rest: rest)
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/shikimori/api/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ module Shikimori
module API
# Helpers to make requests
class REST
def initialize(app_name: nil, access_token: nil, refresh_token: nil)
def initialize(app_name: nil, access_token: nil, refresh_token: nil, **options)
@app_name = app_name
@access_token = access_token
@refresh_token = refresh_token
@proxy_host = options.fetch(:proxy_host, nil)
@proxy_port = options.fetch(:proxy_port, nil)
@proxy_user = options.fetch(:proxy_user, nil)
@proxy_password = options.fetch(:proxy_password, nil)
end

def get(uri, **options)
Expand Down Expand Up @@ -67,7 +71,7 @@ def start_request(uri, request, body = nil, **options)

Net::HTTP.start(
uri.hostname.to_s, uri.port,
:ENV, nil, nil, nil,
@proxy_host, @proxy_port, @proxy_user, @proxy_password,
use_ssl: uri.scheme == 'https'
) do |http|
http.request(request)
Expand Down
2 changes: 1 addition & 1 deletion sig/shikimori/api/client.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Shikimori
attr_reader v1: V1
attr_reader v2: V2

def initialize: (?String site, ?app_name: String | nil, ?access_token: String | nil, ?refresh_token: String | nil) -> void
def initialize: (?String site, **untyped) -> void
end
end
end
6 changes: 5 additions & 1 deletion sig/shikimori/api/rest.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ module Shikimori
@app_name: String | nil
@access_token: String | nil
@refresh_token: String | nil
@proxy_host: String | :ENV | nil
@proxy_port: Integer | nil
@proxy_user: String | nil
@proxy_password: String | nil

def initialize: (?app_name: String | nil, ?access_token: String | nil, ?refresh_token: String | nil) -> void
def initialize: (?app_name: String | nil, ?access_token: String | nil, ?refresh_token: String | nil, **untyped) -> void

def get: (::URI::Generic, **untyped) -> untyped
def post: (::URI::Generic, Hash[_ToS, untyped] data, **untyped) -> untyped
Expand Down

0 comments on commit 1900dec

Please sign in to comment.