Skip to content

Commit

Permalink
v9.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bodrovis committed Nov 27, 2024
1 parent ac170dc commit 3f6ad5f
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/_data/additional_items.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
path: customizing-json-parser
- title: Choosing Network Adapter
path: choosing-network-adapter
- title: Configuring API host
path: configuring-api-host
- title: Exception handling
path: exception_handling
sub_paths:
Expand Down
8 changes: 8 additions & 0 deletions docs/additional_info/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 9.3.0 (27-Nov-2024)

* Allow to override the API host to send requests to:

```ruby
@client = RubyLokaliseApi.client('LOKALISE_API_TOKEN', api_host: 'http://example.com/api')
```

## 9.2.1 (01-Nov-2024)

* Update dependencies
Expand Down
14 changes: 14 additions & 0 deletions docs/additional_info/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,17 @@ Faraday.default_adapter = :excon
```

Please note that since the release of Faraday 2, most adapters have to be added manually. Find all supported adapters [on Faraday official website](https://github.com/lostisland/awesome-faraday#adapters).

## Configuring API host

By default, API requests are sent to the `https://api.lokalise.com/api2/` URL that acts as a host.

OAuth 2 authentication requests are sent to `https://app.lokalise.com`.

To override the API host, use the following approach (works for all client types):

```ruby
@client = RubyLokaliseApi.client('LOKALISE_API_TOKEN', api_host: 'http://example.com/api')
```

Then use your `@client` as usual.
5 changes: 3 additions & 2 deletions lib/ruby_lokalise_api/base_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

module RubyLokaliseApi
# This class contains the base client. Inherited by Client (regular API client)
# and OAuth2Client (used for OAuth-2 based authentication)
# and OAuth2Client (used for OAuth2-based authentication)
class BaseClient
include RubyLokaliseApi::Rest

attr_reader :token, :token_header
attr_reader :token, :token_header, :api_host
attr_accessor :timeout, :open_timeout

def initialize(token, params = {})
@token = token
@timeout = params.fetch(:timeout, nil)
@open_timeout = params.fetch(:open_timeout, nil)
@token_header = ''
@api_host = params.fetch(:api_host, nil)
end
end
end
2 changes: 1 addition & 1 deletion lib/ruby_lokalise_api/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __base_options(endpoint)
accept: 'application/json',
user_agent: "ruby-lokalise-api gem/#{RubyLokaliseApi::VERSION}"
},
url: endpoint.base_url
url: (endpoint.client.api_host || endpoint.base_url)
}
end

Expand Down
3 changes: 2 additions & 1 deletion lib/ruby_lokalise_api/oauth2/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module RubyLokaliseApi
module OAuth2
# This class defines OAuth2 flow
class Auth
attr_reader :client_id, :client_secret, :timeout, :open_timeout
attr_reader :client_id, :client_secret, :timeout, :open_timeout, :api_host

OAUTH2_ENDPOINT = RubyLokaliseApi::Endpoints::OAuth2::OAuth2Endpoint

Expand All @@ -13,6 +13,7 @@ def initialize(client_id, client_secret, params = {})
@client_secret = client_secret
@timeout = params[:timeout]
@open_timeout = params[:open_timeout]
@api_host = params[:api_host]
end

# Returns OAuth2 endpoint URI
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lokalise_api/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module RubyLokaliseApi
VERSION = '9.2.1'
VERSION = '9.3.0'
end
19 changes: 19 additions & 0 deletions spec/lib/ruby_lokalise_api/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@
expect(another_conn.options.open_timeout).to eq(200)
end

it 'uses default endpoint URL' do
custom_client = RubyLokaliseApi.client(ENV.fetch('LOKALISE_API_TOKEN', nil))

custom_endpoint = endpoint name: 'Projects', client: custom_client, params: { query: [project_id] }

conn = dummy.connection custom_endpoint
expect(conn.url_prefix.to_s).to eq(RubyLokaliseApi::Endpoints::MainEndpoint::BASE_URL)
end

it 'allows to override the endpoint URL' do
custom_api_host = 'http://example.com/api'
custom_client = RubyLokaliseApi.client(ENV.fetch('LOKALISE_API_TOKEN', nil), api_host: custom_api_host)

custom_endpoint = endpoint name: 'Projects', client: custom_client, params: { query: [project_id] }

conn = dummy.connection custom_endpoint
expect(conn.url_prefix.to_s).to eq(custom_api_host)
end

it 'gzip compression is on by default' do
custom_client = RubyLokaliseApi.client(ENV.fetch('LOKALISE_API_TOKEN', nil))

Expand Down

0 comments on commit 3f6ad5f

Please sign in to comment.