Skip to content

Commit

Permalink
Switch to authorization header
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurynas Butkus committed Sep 14, 2024
1 parent b073aa7 commit 345a2fe
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 73 deletions.
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ jobs:
- run: bundle install
- env:
CONVERT_API_SECRET: ${{ secrets.CONVERTAPI_SECRET }}
CONVERT_API_TOKEN: ${{ secrets.CONVERTAPI_TOKEN }}
run: bundle exec rake spec
17 changes: 2 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,11 @@ gem 'convert_api'

### Configuration

You can get your secret at https://www.convertapi.com/a/auth
You can get your credentials at https://www.convertapi.com/a/auth

```ruby
ConvertApi.configure do |config|
config.api_secret = 'your-api-secret'
end
```

Or

You can get your token at https://www.convertapi.com/a/access-tokens
```ruby
ConvertApi.configure do |config|
config.token = 'your-token'
config.api_credentials = 'your-api-secret-or-token'
end
```

Expand Down Expand Up @@ -136,10 +127,6 @@ Find more advanced examples in the [examples/](https://github.com/ConvertAPI/con

Run `CONVERT_API_SECRET=your_secret rake spec` to run the tests.

Or

Run `CONVERT_API_TOKEN=your_token rake spec` to run the tests.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Contributing
Expand Down
4 changes: 1 addition & 3 deletions examples/conversions_chaining.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
require 'tmpdir'

ConvertApi.configure do |config|
config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret
# or
config.token = ENV['CONVERT_API_TOKEN'] # your token
config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token
end

# Short example of conversions chaining, the PDF pages extracted and saved as separated JPGs and then ZIP'ed
Expand Down
4 changes: 1 addition & 3 deletions examples/convert_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
require 'tmpdir'

ConvertApi.configure do |config|
config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret
# or
config.token = ENV['CONVERT_API_TOKEN'] # your token
config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token
end

# Example of converting text to PDF
Expand Down
5 changes: 2 additions & 3 deletions examples/convert_url_to_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
require 'tmpdir'

ConvertApi.configure do |config|
config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret
# or
config.token = ENV['CONVERT_API_TOKEN'] # your token
config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token
end

# Example of converting Web Page URL to PDF file
# https://www.convertapi.com/web-to-pdf

Expand Down
4 changes: 1 addition & 3 deletions examples/convert_word_to_pdf_and_png.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
require 'tmpdir'

ConvertApi.configure do |config|
config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret
# or
config.token = ENV['CONVERT_API_TOKEN'] # your token
config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token
end

# Example of saving Word docx to PDF and to PNG
Expand Down
4 changes: 1 addition & 3 deletions examples/create_pdf_thumbnail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
require 'tmpdir'

ConvertApi.configure do |config|
config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret
# or
config.token = ENV['CONVERT_API_TOKEN'] # your token
config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token
end

# Example of extracting first page from PDF and then chaining conversion PDF page to JPG.
Expand Down
4 changes: 1 addition & 3 deletions examples/retrieve_user_information.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
require 'convert_api'

ConvertApi.configure do |config|
config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret
# or
config.token = ENV['CONVERT_API_TOKEN'] # your token
config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token
end

# Retrieve user information
Expand Down
4 changes: 1 addition & 3 deletions examples/split_and_merge_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
require 'tmpdir'

ConvertApi.configure do |config|
config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret
# or
config.token = ENV['CONVERT_API_TOKEN'] # your token
config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token
end

# Example of extracting first and last pages from PDF and then merging them back to new PDF.
Expand Down
24 changes: 12 additions & 12 deletions lib/convert_api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ class Client

def get(path, params = {}, options = {})
handle_response do
request = Net::HTTP::Get.new(request_uri(path, params), DEFAULT_HEADERS)
request = Net::HTTP::Get.new(request_uri(path, params), headers_with_auth)

http(options).request(request)
end
end

def post(path, params, options = {})
handle_response do
request = Net::HTTP::Post.new(request_uri(path), DEFAULT_HEADERS)
request = Net::HTTP::Post.new(request_uri(path), headers_with_auth)
request.form_data = build_form_data(params)

http(options).request(request)
Expand Down Expand Up @@ -101,12 +101,7 @@ def http(options = {})
end

def request_uri(path, params = {})
raise(AuthenticationError, 'API secret or Token not configured') if authentication.nil?

params_with_authentication = params.merge(authentication)
query = URI.encode_www_form(params_with_authentication)

base_uri.path + path + '?' + query
base_uri.path + path + '?' + URI.encode_www_form(params)
end

def build_form_data(params)
Expand All @@ -123,11 +118,16 @@ def build_form_data(params)
data
end

def authentication
return { Secret: config.api_secret } unless config.api_secret.nil?
return { Token: config.token } unless config.token.nil?
def headers_with_auth
DEFAULT_HEADERS.merge(auth_headers)
end

def auth_headers
{ 'Authorization' => "Bearer #{api_credentials}" }
end

nil
def api_credentials
config.api_credentials || raise(AuthenticationError, 'API credentials not configured')
end

def base_uri
Expand Down
3 changes: 1 addition & 2 deletions lib/convert_api/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module ConvertApi
class Configuration
attr_accessor :api_secret
attr_accessor :token
attr_accessor :api_credentials
attr_accessor :base_uri
attr_accessor :connect_timeout
attr_accessor :read_timeout
Expand Down
26 changes: 7 additions & 19 deletions spec/convert_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@
end

describe '.configure' do
let(:api_secret) { 'test_secret' }
let(:token) { 'test_token' }
let(:api_credentials) { 'test_secret' }
let(:conversion_timeout) { 20 }

it 'configures' do
described_class.configure do |config|
config.api_secret = api_secret
config.token = token
config.api_credentials = api_credentials
config.conversion_timeout = conversion_timeout
end

expect(described_class.config.api_secret).to eq(api_secret)
expect(described_class.config.token).to eq(token)
expect(described_class.config.api_credentials).to eq(api_credentials)
expect(described_class.config.conversion_timeout).to eq(conversion_timeout)
end
end
Expand Down Expand Up @@ -93,23 +90,14 @@
end

context 'when has error' do
it 'raises error without secret or token' do
described_class.config.api_secret = nil
described_class.config.token = nil
it 'raises error without credentials' do
described_class.config.api_credentials = nil

expect { subject }.to raise_error(ConvertApi::AuthenticationError, /not configured/)
end

it 'with invalid secret' do
described_class.config.api_secret = 'invalid'
described_class.config.token = nil

expect { subject }.to raise_error(ConvertApi::ClientError)
end

it 'with invalid token' do
described_class.config.token = 'invalid'
described_class.config.api_secret = nil
it 'with invalid credentials' do
described_class.config.api_credentials = 'invalid'

expect { subject }.to raise_error(ConvertApi::ClientError)
end
Expand Down
4 changes: 1 addition & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
end

config.before(:each) do
ConvertApi.config.api_secret = ENV['CONVERT_API_SECRET']
# or
ConvertApi.config.token = ENV['CONVERT_API_TOKEN']
ConvertApi.config.api_credentials = ENV['CONVERT_API_SECRET']
end
end

0 comments on commit 345a2fe

Please sign in to comment.