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 +4 -4
- data/CHANGELOG.md +8 -0
- data/Gemfile.lock +1 -1
- data/README.md +25 -11
- data/lib/brightpearl/auth.rb +43 -8
- data/lib/brightpearl/client.rb +11 -1
- data/lib/brightpearl/config.rb +1 -2
- data/lib/brightpearl/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 005e8e38899a3409cace6dc97b98f6e56057ad6631c4ee1f1fd8acaefae15c78
|
4
|
+
data.tar.gz: bf794427303a93c9a590a5de4c1ab5490003bbbeae0e383d15e775c6a95233e9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
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
|
-
|
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://
|
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
|
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 `
|
61
|
+
#### 3) Trading your `code` for an access token.
|
62
62
|
|
63
|
-
The oauth process will
|
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
|
65
|
+
This process be done by:
|
66
66
|
|
67
67
|
```ruby
|
68
|
-
Brightpearl::Auth.request_token(AUTH_TOKEN) # => {
|
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
|
-
|
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
|
|
data/lib/brightpearl/auth.rb
CHANGED
@@ -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=#{
|
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:
|
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
|
-
|
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
|
-
|
28
|
-
|
29
|
-
|
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
|
data/lib/brightpearl/client.rb
CHANGED
@@ -1,6 +1,16 @@
|
|
1
1
|
module Brightpearl
|
2
2
|
class Client
|
3
|
-
#
|
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}",
|
data/lib/brightpearl/config.rb
CHANGED
@@ -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
|
-
|
14
|
+
attr_accessor :refresh_token # API refresh token
|
16
15
|
end
|
17
16
|
end
|
data/lib/brightpearl/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2022-02-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|