ruby-brightpearl 0.1.01 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b9c38f924ae03982adecfada432233c107e5467db0eb095b6d08d21fe97b12c7
4
- data.tar.gz: 9e27faf80334e375e42028afc329e7858ad6fdee017edae060220875a51fd49a
3
+ metadata.gz: 005e8e38899a3409cace6dc97b98f6e56057ad6631c4ee1f1fd8acaefae15c78
4
+ data.tar.gz: bf794427303a93c9a590a5de4c1ab5490003bbbeae0e383d15e775c6a95233e9
5
5
  SHA512:
6
- metadata.gz: 39f59eb208a807d6bd797aade272521f80c33616a9be0e8b00183ae1fcee74c336a36422898d71cd1445833e8c2f3bc29e26ed1a7c2d9bfa9c7f5d2eb623ea73
7
- data.tar.gz: 50c52c2a69b03d906667b891dff07786846e07f8fc63b2be6a2a834acab0cc6b603b7dc9614103e0f17f45917a1d53710da55228ecaf43d4b774ebfc19752e3d
6
+ metadata.gz: d074392ad00706d6f0e766e831e55e21d01873b8139318690ae0b9f841f61f734f0d11d05dfaa6504087d5a2dacba3a673028693002a544dbdb428f4ca2c17c7
7
+ data.tar.gz: 2f6227ee40bdba46caf0c8dd7b4a70f01c66f4de1dd849c32dfaf63c2385040fd2d2a9902aa5b16d921711eee00d0bfc53cf4d582d230c630d4c18ea3c7387e2
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2022-02-02
4
+ - Add refresh token mechanic with method `use_refresh_token`
5
+ - Change config values
6
+ - Add refresh_token to allow automatic update of tokens when calling the new method `use_refresh_token`
7
+ - Remove `oauth_redirect_url` in favor of keyword arguments on `oauth_url` and `request_token`
8
+ - Create a temp method for Client to use a token per request
9
+ - Add error handling on auth request_token method and tests for some common errors.
10
+
3
11
  ## [0.1.0] - 2022-01-21
4
12
 
5
13
  - Initial release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-brightpearl (0.1.01)
4
+ ruby-brightpearl (0.2.0)
5
5
  httparty (~> 0.20)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -41,41 +41,55 @@ Using the official portal create a new application, the type must be `instance`,
41
41
 
42
42
  Start by setting up the initial configuration for the client:
43
43
  ```ruby
44
- Brightpearl.config.account = ACCOUNT # From developer account creation
44
+ require 'brightpearl'
45
+
46
+ Brightpearl.config.account = ACCOUNT # Account to request API access
45
47
  Brightpearl.config.dev_ref = DEV_REF # From developer account creation
46
48
 
47
49
  Brightpearl.config.app_ref = APP_REF # Brightpearl app ID
48
50
  Brightpearl.config.app_secret = APP_REF # Brightpearl app secret
49
-
50
- Brightpearl.config.oauth_redirect_url = OAUTH_REDIRECT_URL # Brightpearl app authorized redirect url
51
51
  ```
52
52
 
53
53
  Before the API calls can be performed you will need to get a token, to get one the authentication flow must be followed as per brightpearl documentation: https://help.brightpearl.com/hc/en-us/articles/360032240811-Set-up-the-Authorization-Code-Grant-flow
54
54
 
55
55
  The oauth URL can be then generated with:
56
56
  ```ruby
57
- Brightpearl::Auth.oauth_url("random-passcode") # => "https://oauth.brightpearl.com/authorize/testAccount?response_type=code&client_id=testAppName&redirect_uri=https://2f826695ec8a.ngrok.io/oauth&state=random-passcode
57
+ Brightpearl::Auth.oauth_url(state: "random-passcode", redirect_uri: "https://www.something.io/oauth") # => "https://oauth.brightpearl.com/authorize/testAccount?response_type=code&client_id=testAppName&redirect_uri=https://www.something.io/oauth&state=random-passcode
58
58
  ```
59
- NOTE: The argument on `oauth_url` is the state, this should be a non guessable string that the authorization server will pass back to you on redirection which you should check against to prevent CSRF attacks
59
+ NOTE: The state argument on `oauth_url` method is a string defined by yourself, this should be a non guessable string that the authorization server will pass back to you on redirection which you should check against to prevent CSRF attacks
60
60
 
61
- #### 3) Trading your `auth code` for an access token.
61
+ #### 3) Trading your `code` for an access token.
62
62
 
63
- The oauth process will return an url with a param called `code`, the value of this parameter is a temporary token that the app can exchange for an access token.
63
+ The oauth process will redirect to your `redirect_uri` with a param called `code`, the value of this parameter is a temporary token that the app can exchange for an access token.
64
64
 
65
- This can be done by calling:
65
+ This process be done by:
66
66
 
67
67
  ```ruby
68
- Brightpearl::Auth.request_token(AUTH_TOKEN) # => { token: "XXX", refresh_token: "XYZ", api_domain: "ws-use.brightpearl.com" }
68
+ Brightpearl::Auth.request_token(auth_token: AUTH_TOKEN, redirect_uri: "https://www.something.io/oauth") # => { payload: { "access_token" => "XXX", "refresh_token" => "XYZ", "api_domain" => "ws-use.brightpearl.com" } }
69
69
  ```
70
70
 
71
71
  After the token is obtained it can be added to client by setting it on the config:
72
72
  ```ruby
73
73
  Brightpearl.config.api_domain = API_DOMAIN # Such as ws-use.brightpearl.com
74
74
  Brightpearl.config.token = TOKEN
75
+ Brightpearl.config.refresh_token = REFRESH_TOKEN
76
+ ```
77
+
78
+ NOTES:
79
+ * The token has a expiration time, when the token has expired a new one can be obtained using a refresh token.
80
+ * The redirect_uri used on `request_token` should be the same used on `oauth_url`
81
+
82
+ #### 3A) Using the refresh token to get a new access token
83
+
84
+ When the token has expired, the `use_refresh_token` method can be used:
85
+ ```ruby
86
+ Brightpearl::Auth.use_refresh_token(refresh_token: "XXX")
87
+ # If refresh_token is loaded on config just call the method
88
+ Brightpearl::Auth.use_refresh_token()
75
89
  ```
76
90
 
77
- Token has a expiration time, when the token has expired a new one can be obtained using the refresh token.
78
-
91
+ The return value is the same as `request_token`, additionally by default the new `token` and `refresh_token` are loaded on `Brightpearl.config`, if for some reason this is undesired it can be turned off by calling the method as `Brightpearl::Auth.use_refresh_token(autoupdate: false)`
92
+
79
93
  #### 4) Making requests
80
94
  Responses to REST requests are parsed into a hash with the keys `:payload` with the actual response from brightpearl API and `:quota_remaining` with the value of the current quota.
81
95
 
@@ -1,17 +1,17 @@
1
1
  module Brightpearl
2
2
  class Auth
3
- def self.oauth_url(state)
4
- "https://oauth.brightpearl.com/authorize/#{Brightpearl.config.account}?response_type=code&client_id=#{Brightpearl.config.app_ref}&redirect_uri=#{Brightpearl.config.oauth_redirect_url}&state=#{state}"
3
+ def self.oauth_url(state:, redirect_uri:)
4
+ "https://oauth.brightpearl.com/authorize/#{Brightpearl.config.account}?response_type=code&client_id=#{Brightpearl.config.app_ref}&redirect_uri=#{redirect_uri}&state=#{state}"
5
5
  end
6
6
 
7
- def self.request_token(auth_token)
7
+ def self.request_token(auth_token:, redirect_uri:)
8
8
  token_endpoint = "https://oauth.brightpearl.com/token/#{Brightpearl.config.account}"
9
9
  body = {
10
10
  grant_type: "authorization_code",
11
11
  code: auth_token,
12
12
  client_id: Brightpearl.config.app_ref,
13
13
  client_secret: Brightpearl.config.app_secret,
14
- redirect_uri: Brightpearl.config.oauth_redirect_url
14
+ redirect_uri: redirect_uri, # SAME AS THE ONE USED ON oauth_url
15
15
  }
16
16
 
17
17
  response = HTTParty.post(token_endpoint,
@@ -21,13 +21,48 @@ module Brightpearl
21
21
  'charset' => 'utf-8'
22
22
  }
23
23
  )
24
- data = JSON.parse(response.body)
24
+ json = JSON.parse(response.body)
25
+ raise Brightpearl::RequestError.new(json["error_description"] || json["error"], response: json, status: 400) if response.code == 400
25
26
 
26
27
  return {
27
- token: data["access_token"],
28
- refresh_token: data["refresh_token"],
29
- api_domain: data["api_domain"],
28
+ payload: json,
29
+ data: {
30
+ token: json["access_token"],
31
+ refresh_token: json["refresh_token"],
32
+ api_domain: json["api_domain"],
33
+ }
30
34
  }
31
35
  end
36
+
37
+ def self.use_refresh_token(refresh_token: nil, autoupdate: true)
38
+ token_endpoint = "https://oauth.brightpearl.com/token/#{Brightpearl.config.account}"
39
+ rtoken = refresh_token || Brightpearl.config.refresh_token
40
+ body = {
41
+ grant_type: "refresh_token",
42
+ refresh_token: rtoken,
43
+ client_id: Brightpearl.config.app_ref,
44
+ }
45
+ response = HTTParty.post(token_endpoint,
46
+ body: body,
47
+ headers: { "Content-Type": "application/x-www-form-urlencoded", 'charset' => 'utf-8' }
48
+ )
49
+
50
+ json = JSON.parse(response.body)
51
+ if json["access_token"] && autoupdate
52
+ Brightpearl.config.token = json["access_token"]
53
+ Brightpearl.config.refresh_token = json["refresh_token"]
54
+ end
55
+
56
+ return {
57
+ payload: json,
58
+ data: {
59
+ token: json["access_token"],
60
+ refresh_token: json["refresh_token"],
61
+ api_domain: json["api_domain"]
62
+ }
63
+ }
64
+
65
+ end
66
+
32
67
  end
33
68
  end
@@ -1,6 +1,16 @@
1
1
  module Brightpearl
2
2
  class Client
3
- # attr_accessor :token
3
+ # Send a request using a different token than the global config. (Useful when using a different token for specific calls)
4
+ def self.temp(token:, &block)
5
+ original_token = Brightpearl.config.token
6
+ begin
7
+ Brightpearl.config.token = token
8
+ yield
9
+ ensure
10
+ Brightpearl.config.token = original_token
11
+ end
12
+ end
13
+
4
14
  def self.send_request(path:, method: :get, **options )
5
15
  headers = {
6
16
  "brightpearl-app-ref": "#{Brightpearl.config.app_ref}",
@@ -7,11 +7,10 @@ module Brightpearl
7
7
  # Application specific
8
8
  attr_accessor :app_ref # App ref
9
9
  attr_accessor :app_secret # App secret
10
- attr_accessor :oauth_redirect_url # App authorized oauth redirect for app
11
10
 
12
11
  # Based on authentication process
13
12
  attr_accessor :token # API token
14
13
  attr_accessor :api_domain # API URL base. Depends on instance region. Obtained after auth process
15
- # attr_accessor :refresh_token
14
+ attr_accessor :refresh_token # API refresh token
16
15
  end
17
16
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Brightpearl
4
- VERSION = "0.1.01"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-brightpearl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.01
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - vicvans20
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-29 00:00:00.000000000 Z
11
+ date: 2022-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty