Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## main

* BREAKING: removes the `headers` param and replaces with `api_key`.

## 0.8.1

* Add logger to configuration by @cdmwebs in https://github.com/knowndecimal/fulfil/pull/49
Expand Down
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ Environment variables:

- **FULFIL_SUBDOMAIN:** - always required to use the gem.
- **FULFIL_OAUTH_TOKEN:** required for oauth bearer authentication
- **FULFIL_API_KEY:** required for authentication via the `X-API-KEY` request header
- **FULFIL_API_KEY:** required for authentication via the `X-API-KEY` request
header. This is used with the Personal Access Token authentication method.

> **Note:** When `FULFIL_OAUTH_TOKEN` is present, the `FULFIL_API_KEY` will be ignored. So,
if oauth doesn't work, returning an Unauthorized error, to use the
Expand Down Expand Up @@ -72,6 +73,24 @@ pp sales
# "rec_name"=>""}]
```

To configure a client without using environment variables, pass them as arguments:

```ruby
client = Fulfil::Client.new(
subdomain: 'test',
api_key: '123'
)
```

With an OAuth token:

```ruby
client = Fulfil::Client.new(
subdomain: 'test',
token: '123'
)
```

### Count

```ruby
Expand Down
20 changes: 12 additions & 8 deletions lib/fulfil/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Fulfil
class Client
class InvalidClientError < StandardError
def message
'Client is not configured correctly.'
super() || 'Client is not configured correctly.'
end
end

Expand All @@ -23,12 +23,17 @@ class ConnectionError < StandardError; end

class ResponseError < StandardError; end

def initialize(subdomain: SUBDOMAIN, token: oauth_token, headers: { 'X-API-KEY' => API_KEY }, debug: false)
def initialize(subdomain: SUBDOMAIN, token: oauth_token, api_key: API_KEY, debug: false)
@subdomain = subdomain
@token = token
@debug = debug
@headers = headers
@headers.delete('X-API-KEY') if @token

if token
@token = token
elsif api_key
@api_key = api_key
else
raise InvalidClientError, 'No token or API key provided.'
end

raise InvalidClientError if invalid?
end
Expand Down Expand Up @@ -150,8 +155,6 @@ def model_url(model:, id: nil, endpoint: nil)
end

def request(endpoint:, verb: :get, **args)
raise InvalidClientError if invalid?

response = client.request(verb, endpoint, args)
Fulfil::ResponseHandler.new(response).verify!

Expand All @@ -177,7 +180,8 @@ def request(endpoint:, verb: :get, **args)
def client
client = HTTP.use(logging: @debug ? { logger: config.logger } : {})
client = client.auth("Bearer #{@token}") if @token
client.headers(@headers)
client.headers({ 'X-API-KEY': @api_key }) if @api_key
client
end

def config
Expand Down
12 changes: 12 additions & 0 deletions test/fulfil/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ def test_invalid_client
assert_raises('InvalidClientError') { Fulfil::Client.new(subdomain: nil, token: nil) }
end

def test_token_authorization
client = Fulfil::Client.new(subdomain: 'test', token: '123')

assert_predicate client, :valid?
end

def test_api_key_authorization
client = Fulfil::Client.new(subdomain: 'test', api_key: '123')

assert_predicate client, :valid?
end

def test_find_one
stub_fulfil_get('sale.sale/213112', 'sale_sale')

Expand Down