ecoportal-api-graphql 0.3.3 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 802706f5cb251ec2df63b4f43576f779e244da1fb7ad0d93875e5d05d87e3539
4
- data.tar.gz: d761dd4b221133959cf2a64b49e5cceb11c10b6bafea42dd115294b44f7812ae
3
+ metadata.gz: 0d37cda00c6ee7d64fb29829b592c73838b384726cb328b57cd228969f0e6103
4
+ data.tar.gz: 5d18a917e0e338e46f65d9132b96b4b032979856da5524426d2d92e43cbf5aee
5
5
  SHA512:
6
- metadata.gz: 9ea32be18933799c0a210250771f95f6fbfbf0bcaaee1bcf907b86cf759eb4350f6786d4c9efe3188a22dacad396cbd9b1d84b79c8233a194f2f99c8a4a3d77d
7
- data.tar.gz: 8af6e498686d97e395a50195970346af89676652721ed673405287c45632f4077b47e5388d179b59ca45f15021ce38718e6aa1cfe54a718b039aaf8c5a63fa43
6
+ metadata.gz: 11d595232646fe8fc8b3096e77bcedd1d6ee635b74cdc25c6c6743607727c9e8e4a98f0538fb60226d6337407105cb6b8c347ebad4d3cf6dcbacf3329baf0ea9
7
+ data.tar.gz: '0926f7ce7f600ca7b8d1badabe4a7f9c6733daa5d5ebb535e5ced1835e1bc52015138409034cb5229c32534d3f0e4f2799bf58d15f4f5b94eafda33fd713059d'
data/CHANGELOG.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Change Log
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
- ## [0.3.4] - 2023-04-xx
4
+ ## [0.3.6] - 2023-04-xx
5
5
 
6
6
  ### Added
7
7
  ### Changed
@@ -14,6 +14,22 @@ All notable changes to this project will be documented in this file.
14
14
 
15
15
  - review `path` tracking
16
16
 
17
+ ## [0.3.5] - 2023-04-03
18
+
19
+ ### Fixed
20
+ - `Ecoportal::API::GraphQL::Helpers::LocationsTree#treeify`
21
+ - Make it real id-case-insensitive (parents in lower case were being missed due to this)
22
+
23
+ ## [0.3.4] - 2023-04-03
24
+
25
+ ### Added
26
+ - `Ecoportal::API::Common::GraphQL::AuthService`
27
+ - `#session_token` added **token auto-renew** functionality (when token expires in less than 90 minutes)
28
+ - `#session_token_renewed`
29
+
30
+ ### Fixed
31
+ - Enabled to specify `host` (other than `live.ecoportal.com`) by the user
32
+
17
33
  ## [0.3.3] - 2023-04-02
18
34
 
19
35
  ### Fixed
@@ -3,20 +3,64 @@ module Ecoportal
3
3
  module Common
4
4
  module GraphQL
5
5
  module AuthService
6
- DEFAULT_SERVER = "live.ecoportal.com"
6
+ DEFAULT_SERVER = "live.ecoportal.com"
7
+ TOKEN_AUTORENEW = 90 # minutes
7
8
 
8
9
  module InstanceMethods
9
- def session_token(host: server, version: nil)
10
+ def session_token(host: server, version: nil, auto_renew: true)
11
+ session_token_data(host: host, version: version).yield_self do |body|
12
+ return nil unless body
13
+ if auto_renew && token_renew?(body["expires_in"])
14
+ session_token_renewed(host: host, version: version, refresh_token: body["refresh_token"])
15
+ else
16
+ body["access_token"]
17
+ end
18
+ end
19
+ end
20
+
21
+ def session_token_renewed(host: server, version: nil, refresh_token: nil)
22
+ unless refresh_token
23
+ return nil unless body = session_token_data(host: host, version: version)
24
+ return nil unless refresh_token = body["resfresh_token"]
25
+ end
26
+ session_refresh_token_data(host: host, version: version, refresh_token: refresh_token).yield_self do |body|
27
+ return nil unless body
28
+ body["access_token"] if body
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def session_token_data(host: server, version: nil)
10
35
  http_client(host: host, version: version).post("/oauth/token", data: {
11
36
  "grant_type" => "password",
12
37
  "email" => user_email,
13
38
  "password" => user_pass
14
39
  }).yield_self do |response|
15
- response.body["access_token"] if response.success?
40
+ if response.success?
41
+ response.body
42
+ else
43
+ nil
44
+ end
16
45
  end
17
46
  end
18
47
 
19
- private
48
+ def session_refresh_token_data(host: server, version: nil, refresh_token:)
49
+ http_client(host: host, version: version).post("/oauth/token", data: {
50
+ "grant_type" => "refresh_token",
51
+ "refresh_token" => refresh_token
52
+ }).yield_self do |response|
53
+ if response.success?
54
+ response.body
55
+ else
56
+ nil
57
+ end
58
+ end
59
+ end
60
+
61
+ def token_renew?(seconds)
62
+ (TOKEN_AUTORENEW * 60) > seconds
63
+ end
20
64
 
21
65
  def http_client(host: server, version: nil)
22
66
  @http_client ||= Ecoportal::API::Common::GraphQL::HttpClient.new(host: host, version: version)
@@ -19,7 +19,7 @@ module Ecoportal
19
19
  puts "Configuring GraphQL Client onto '#{url}'"
20
20
  super(url,
21
21
  headers: {
22
- 'Authorization' => "Bearer #{session_token}"
22
+ 'Authorization' => "Bearer #{session_token(host: host)}"
23
23
  },
24
24
  http_options: {
25
25
  read_timeout: 20,
@@ -6,8 +6,6 @@ module Ecoportal
6
6
  def treeify(nodes)
7
7
  parents = nodes.each_with_object({}) do |node, parents|
8
8
  parent = node.parent
9
- # puts "Node '#{node.name}' with ID: '#{node.id}'. "
10
- # puts " • Parent '#{parent.name}' with ID: '#{parent.id}'." if parent
11
9
  parentId = parent && parent.id.upcase
12
10
  (parents[parentId] ||= []).push(node)
13
11
  end
@@ -17,7 +15,7 @@ module Ecoportal
17
15
  private
18
16
 
19
17
  def get_children(node_id, parents)
20
- (parents[node_id] ||= []).each_with_object([]) do |child, results|
18
+ (parents[node_id&.upcase] ||= []).each_with_object([]) do |child, results|
21
19
  results << {
22
20
  "id" => child.id,
23
21
  "name" => child.name,
@@ -11,8 +11,9 @@ module Ecoportal
11
11
  # @param org_id [String] the id of the target organization.
12
12
  # It defaults to the environmental variable `ORGANIZATION_ID`, if defined
13
13
  # @param logger [Logger] an object with `Logger` interface to generate logs.
14
- def initialize(email: nil, pass: nil, org_id: nil)
15
- @client = Ecoportal::API::Common::GraphQL::Client.new(email: email, pass: pass, org_id: org_id, no_schema: true)
14
+ def initialize(email: nil, pass: nil, org_id: nil, host: "live.ecoportal.com")
15
+ kargs = {email: email, pass: pass, host: host, org_id: org_id, no_schema: true}
16
+ @client = Ecoportal::API::Common::GraphQL::Client.new(**kargs)
16
17
  @fragments = Ecoportal::API::GraphQL::Fragment.new(client)
17
18
  end
18
19
 
@@ -1,5 +1,5 @@
1
1
  module Ecoportal
2
2
  module API
3
- GRAPQL_VERSION = "0.3.3"
3
+ GRAPQL_VERSION = "0.3.5"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ecoportal-api-graphql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oscar Segura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-01 00:00:00.000000000 Z
11
+ date: 2023-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler