naranya_ecm-sdk 0.0.46 → 0.0.47

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
  SHA1:
3
- metadata.gz: 778b8cdbd9878e681728270609039e52ce0c97e4
4
- data.tar.gz: 8098d4252fb1405d72fbbdc1955360cf88829cfe
3
+ metadata.gz: d1fdfd9d7e794c81243641e7cf901d3c9a86aec9
4
+ data.tar.gz: 6d90eac23cf83104b82814ef0c149df363c76637
5
5
  SHA512:
6
- metadata.gz: ad066a7ee48138ccfdc9bbd1dc1f2eeac9231f84980b7258bdc01c5e83689fb493c46a3654f5c63fc2ca6358e0c1900c18f6b038217854dd873d0699182d39b9
7
- data.tar.gz: 38cad328fcd9f70f7662576320869171788b909e739156e8b6178cc8e557ef51dba61d4615cd2056fc4f8371e089487deaed64ccfd46a5b9ede9315cc2d603fa
6
+ metadata.gz: 13b46c12c27bd140428638b2e68460124cb301e8022313347689ced1ae266df8522a1bfc5fb6c3c47195ad5997b0b4afcb43bf5b4099524d906c72e1b1fa88f2
7
+ data.tar.gz: fa2732f9f39e7a59f61c80ec9fe1e8fc969281a39c035d29f088422a7d8d3830cf0e0d081de5862ebd8f9d67b56f5cd05fa0127174ca6acd2bb49b038c6e3838
data/Gemfile CHANGED
@@ -6,4 +6,4 @@ gemspec
6
6
  gem 'redis-activesupport'
7
7
  gem 'pry', require: 'pry'
8
8
  gem 'byebug'
9
- gem "patron"
9
+ gem 'patron'
@@ -1,12 +1,19 @@
1
1
  NContent::SDK.logger.level = Logger::INFO
2
+
3
+ NContent::SDK::RESTClient.oauth_token
4
+
2
5
  # Launch n threads:
3
- 10.times.map do |i|
4
- sleep 0.3
6
+ 20.times.map do |i|
7
+ sleep 0.2
5
8
 
6
9
  Thread.new do
7
10
 
8
11
  10.times.map do |reqno|
9
- a = NaranyaEcm::Category.find "53367aa33535660003000000"
12
+ begin
13
+ a = NaranyaEcm::Category.find "53367aa33535660003000000"
14
+ rescue => e
15
+ puts "!!!!!!!!!!!! #{e.inspect} !!!!!!!!!!!!!!!!!!"
16
+ end
10
17
  sleep 5
11
18
  end
12
19
 
@@ -1,3 +1,3 @@
1
1
  module NaranyaEcm
2
- VERSION = "0.0.46"
2
+ VERSION = "0.0.47"
3
3
  end
@@ -10,6 +10,10 @@ module NContent
10
10
 
11
11
  include ActiveSupport::Configurable
12
12
 
13
+ config_accessor :api_adapter do
14
+ ENV.fetch 'NCONTENT_API_ADAPTER', 'net_http'
15
+ end
16
+
13
17
  config_accessor :api_host do
14
18
  ENV['NCONTENT_API_HOST'] || ENV['NARANYA_ECM_SITE'] || 'http://proy-cms-1.herokuapp.com'
15
19
  end
@@ -32,27 +32,21 @@ module NContent
32
32
 
33
33
  end
34
34
 
35
- #initializer "cequel.add_new_relic" do
36
- # if configuration.fetch(:newrelic_enabled, true)
37
- # begin
38
- # require 'new_relic/agent/method_tracer'
39
- # rescue LoadError => e
40
- # Rails.logger.debug(
41
- # "New Relic not installed; skipping New Relic integration")
42
- # else
43
- # require 'cequel/metal/new_relic_instrumentation'
44
- # end
45
- # end
46
- #end
47
-
48
- #rake_tasks do
49
- # require "cequel/record/tasks"
50
- #end
51
-
52
- #generators do
53
- # require 'cequel/record/configuration_generator'
54
- # require 'cequel/record/record_generator'
55
- #end
35
+ config.after_initialize do
36
+ # Try to initialize a token (from cache or from nContent), just in case
37
+ # all threads start to go medieval asking for tokens at nContent:
38
+ begin
39
+ NContent::SDK::RESTClient.oauth_token
40
+ rescue => e
41
+ Rails.logger.warn "NContent SDK OAuth token test failed on app after_initialize"
42
+ end
43
+ end
44
+
45
+ rake_tasks do
46
+ end
47
+
48
+ generators do
49
+ end
56
50
 
57
51
  end
58
52
  end
@@ -8,48 +8,10 @@ module NContent
8
8
 
9
9
  class RESTClient
10
10
 
11
- include Singleton
12
-
13
11
  CLIENT_TOKEN_CACHE_KEY = "ncontent/api_client/client_token".freeze
14
12
 
15
- delegate :config, :logger, :cache, to: NContent::SDK
16
-
17
- def initialize
18
- @client = OAuth2::Client.new(config.api_key, config.api_secret, site: config.api_host) do |faraday|
19
- ### Oauth2 Client (Faraday) builder:
20
-
21
- faraday.request :url_encoded # them posts...
22
-
23
- faraday.response :logger, logger
24
-
25
- faraday.use NContent::SDK::FaradayMiddleware::RESTAPICallBenchmark
26
-
27
- faraday.use NContent::SDK::FaradayMiddleware::ResponseParser
28
-
29
- if defined?(Patron)
30
- faraday.adapter :patron # Prefer patron if exists
31
- else
32
- faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
33
- end
34
- end
35
- @client_token = get_token
36
- @mutex = Mutex.new
37
- end
38
-
39
- def reset_client_token!
40
- with_mutex do
41
- if cached = cache.read(CLIENT_TOKEN_CACHE_KEY) and cached['access_token'] == @client_token.token
42
- logger.debug "Deleting cache key #{CLIENT_TOKEN_CACHE_KEY}"
43
- cache.delete CLIENT_TOKEN_CACHE_KEY
44
- else
45
- logger.debug "Not deleting cache key #{CLIENT_TOKEN_CACHE_KEY}"
46
- end
47
- @client_token = get_token
48
- end
49
- end
50
-
51
- def client_token
52
- with_mutex { @client_token }
13
+ class << self
14
+ delegate :config, :logger, :cache, to: NContent::SDK
53
15
  end
54
16
 
55
17
  # Make a request to the API:
@@ -57,7 +19,7 @@ module NContent
57
19
  # @param [Symbol] verb the HTTP request method
58
20
  # @param [String] path the HTTP URL path of the request
59
21
  # @param [Hash] opts the options to make the request with
60
- def request(verb, path=nil, opts = {}, &block)
22
+ def self.request(verb, path=nil, opts = {}, &block)
61
23
 
62
24
  retrying = false
63
25
 
@@ -65,72 +27,91 @@ module NContent
65
27
 
66
28
  # If we beforehand know that the current token expired then the client
67
29
  # token should be re-generated.
68
- reset_client_token! if client_token.expired?
30
+ reset_oauth_token! if oauth_token.expired?
69
31
 
70
- client_token.request(verb, path, opts) do |req|
32
+ oauth_token.request(verb, path, opts) do |req|
71
33
  req.options.timeout = config.api_call_timeout
72
34
  req.options.open_timeout = config.api_call_open_timeout
73
35
  yield req if block_given?
74
36
  end
75
37
 
76
38
  rescue OAuth2::Error => error
77
-
78
39
  if error.response.status == 401 && !retrying
79
- reset_client_token!
40
+ reset_oauth_token!
80
41
  retrying = true
81
42
  logger.info "Retrying response #{verb.upcase} #{path} with a fresh nContent token."
82
43
  retry
83
44
  else
45
+ logger.warn "Not retrying response #{verb.upcase} #{path}."
84
46
  NaranyaEcm::Rest::RestError.raise_by_failed_response(error.response)
85
47
  end
86
48
  end
87
49
  end
88
50
 
89
- def get(path=nil, opts = {}, &block)
51
+ def self.get(path=nil, opts = {}, &block)
90
52
  request(:get, path, opts, &block)
91
53
  end
92
54
 
93
- def post(path=nil, opts = {}, &block)
55
+ def self.post(path=nil, opts = {}, &block)
94
56
  request(:post, path, opts, &block)
95
57
  end
96
58
 
97
- def put(path=nil, opts = {}, &block)
59
+ def self.put(path=nil, opts = {}, &block)
98
60
  request(:put, path, opts, &block)
99
61
  end
100
62
 
101
- def patch(path=nil, opts = {}, &block)
63
+ def self.patch(path=nil, opts = {}, &block)
102
64
  request(:patch, path, opts, &block)
103
65
  end
104
66
 
105
- def delete(path=nil, opts = {}, &block)
67
+ def self.delete(path=nil, opts = {}, &block)
106
68
  request(:delete, path, opts, &block)
107
69
  end
108
70
 
109
- class << self
110
- delegate :client_token, :reset_client_token!, :request, :get, :post,
111
- :put, :patch, :delete, to: :instance
112
- end
113
-
114
71
  private
115
72
 
116
- def get_token
117
- cached = cache.read CLIENT_TOKEN_CACHE_KEY
73
+ # The oauth client is accessed as a class method, but will exist one instance
74
+ # per thread (hence the @ instead of @@ and using ||= without remorse)
75
+ def self.oauth_client
76
+ @oauth_client ||= OAuth2::Client.new(config.api_key, config.api_secret, site: config.api_host) do |faraday|
77
+ ### Oauth2 Client (Faraday) builder:
78
+
79
+ faraday.request :url_encoded # them posts...
80
+
81
+ faraday.response :logger, logger
82
+
83
+ faraday.use NContent::SDK::FaradayMiddleware::RESTAPICallBenchmark
84
+
85
+ faraday.use NContent::SDK::FaradayMiddleware::ResponseParser
86
+
87
+ faraday.adapter config.api_adapter.to_sym
88
+ end
89
+ end
118
90
 
119
- returned_token = if cached.present?
120
- logger.debug "Generating client token from cached data: #{cached.inspect}"
121
- OAuth2::AccessToken.from_hash @client, cached
91
+ def self.oauth_token(overwrite_cache = false)
92
+ @oauth_token ||= if !overwrite_cache && (cached_data = cache.read CLIENT_TOKEN_CACHE_KEY)
93
+ logger.debug "Generating client token from cached data: #{cached_data.inspect}"
94
+ OAuth2::AccessToken.from_hash oauth_client, cached_data
122
95
  else
123
96
  logger.info "Requesting new client token from nContent server"
124
- new_token = @client.client_credentials.get_token
97
+ new_token = oauth_client.client_credentials.get_token
125
98
  cache.write CLIENT_TOKEN_CACHE_KEY, new_token.to_hash.with_indifferent_access
126
- logger.debug "Data #{new_token.token.inspect} written to cache key #{CLIENT_TOKEN_CACHE_KEY}."
127
99
  new_token
128
100
  end
129
- returned_token
130
101
  end
131
102
 
132
- def with_mutex
133
- @mutex.synchronize { yield }
103
+ def self.reset_oauth_token!
104
+ # if (cached_data = cache.read CLIENT_TOKEN_CACHE_KEY) && (cached_data['access_token'] == oauth_token.token)
105
+ # The cached data matches the current oauth_token.
106
+ # We'll unset @oauth_token, and call 'oauth_token' with 'true' to
107
+ # force a cache overwrite:
108
+ @oauth_token = nil
109
+ oauth_token(true)
110
+ # else
111
+ # # The cached data does not match with the current oauth_token.
112
+ # # Maybe the cached data was updated by another thread or process.
113
+ # @oauth_token = OAuth2::AccessToken.from_hash oauth_client, cached_data
114
+ # end
134
115
  end
135
116
 
136
117
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: naranya_ecm-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.46
4
+ version: 0.0.47
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roberto Quintanilla
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-05-14 00:00:00.000000000 Z
11
+ date: 2015-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport