blackstack-core 1.2.29 → 1.2.31

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: 61e89c40560e966edd297c6fbce7e0099a94bce4dd88c0653f7e20feceb8234e
4
- data.tar.gz: 28543d06207cc6399371e857b0982ec7432801e5344372304165a0aaa4053660
3
+ metadata.gz: f9422bf619ee5c62514cede0f715c896f9721bd679c106b328dce187a5a404a6
4
+ data.tar.gz: 46f704d4f1ce7e8c6d5664ef4c3de74bc5a1dd6179e42f8247956410445fccb4
5
5
  SHA512:
6
- metadata.gz: 8545ef59846393d06cb272b8e503847b3c11e9837933ac770c546acf724265a6e560407dea7bcbe67bccf49ec505b89463b4aab52bb83094fdeb792b517f7479
7
- data.tar.gz: 3eaac8af64b460a4cf941d3756faa219da65a75300cbe47fe1574ae4bb9426e3a5d6a2c028dcb81e940646bc4566f433ed48493cbf4665c5eee58d76b341823f
6
+ metadata.gz: 4cffd17c2b4a54c112fc6cacd6fec8a3f48b9286ea83e401662770a6580f4c40c030a229be9b3a066b965fb5a853cb79eb85d7ae15d358e8ca5908d0fe830eab
7
+ data.tar.gz: b71b7512bd2e941742359dd7135772956ed8956eafb98dc1c459760e0055a8009faabb14d3bd9e300047b401a747f053794cce0deaf6ddfd8cb4d3e7b2ef9ef9
File without changes
File without changes
File without changes
File without changes
File without changes
data/lib/extend_fixnum.rb CHANGED
File without changes
data/lib/extend_float.rb CHANGED
File without changes
data/lib/extend_string.rb CHANGED
File without changes
data/lib/extend_time.rb CHANGED
File without changes
data/lib/functions.rb CHANGED
@@ -862,6 +862,10 @@ module BlackStack
862
862
  DEFAULT_SSL_VERIFY_MODE = OpenSSL::SSL::VERIFY_NONE
863
863
  SUCCESS = 'success'
864
864
 
865
+ DEFAULT_OPEN_TIMEOUT = 10 # seconds to wait for the initial connection
866
+ DEFAULT_READ_TIMEOUT = 30 # seconds to wait for each response
867
+ DEFAULT_RETRY_ATTEMPTS = 3
868
+
865
869
  @@lockfiles = []
866
870
 
867
871
  @@max_api_call_channels = 0 # 0 means infinite
@@ -927,32 +931,81 @@ module BlackStack
927
931
  #
928
932
  # TODO: parameter support_redirections has been deprecated.
929
933
  #
930
- def self.call_post(url, body = {}, ssl_verify_mode=BlackStack::Netting::DEFAULT_SSL_VERIFY_MODE, support_redirections=true)
931
- # issue: https://github.com/leandrosardi/mysaas/issues/59
932
- #
933
- # when ruby pushes hash of hashes (or hash of arrays), all values are converted into strings.
934
- # and arrays are mapped to the last element only.
935
- #
936
- # the solution is to convert each element of the hash into a string using `.to_json` method.
937
- #
938
- # references:
939
- # - https://stackoverflow.com/questions/1667630/how-do-i-convert-a-string-object-into-a-hash-object
940
- # - https://stackoverflow.com/questions/67572866/how-to-build-complex-json-to-post-to-a-web-service-with-rails-5-2-and-faraday-ge
941
- #
942
- # iterate the keys of the hash
943
- #
944
- params = {} # not needed for post calls to access points
945
- path = URI::parse(url).path
946
- domain = url.gsub(/#{Regexp.escape(path)}/, '')
947
-
948
- conn = Faraday.new(domain, :ssl=>{:verify=>ssl_verify_mode!=OpenSSL::SSL::VERIFY_NONE})
949
- ret = conn.post(path, params) do |req|
950
- req.body = body.to_json
951
- req.headers['Content-Type'] = 'application/json'
952
- req.headers['Accept'] = 'application/json'
934
+ def self.call_post(url,
935
+ body = {},
936
+ ssl_verify_mode = BlackStack::Netting::DEFAULT_SSL_VERIFY_MODE,
937
+ support_redirections = true)
938
+
939
+ max_redirects = 5
940
+ attempts = 0
941
+
942
+ while attempts < DEFAULT_RETRY_ATTEMPTS
943
+ begin
944
+ # Create a basic Faraday connection
945
+ conn = Faraday.new(
946
+ url: url, #"#{uri.scheme}://#{uri.host}:#{uri.port}",
947
+ ssl: { verify: ssl_verify_mode != OpenSSL::SSL::VERIFY_NONE },
948
+ request: {
949
+ open_timeout: DEFAULT_OPEN_TIMEOUT,
950
+ timeout: DEFAULT_READ_TIMEOUT
951
+ }
952
+ ) do |f|
953
+ f.adapter Faraday.default_adapter
954
+ end
955
+
956
+ # Perform the initial POST request
957
+ response = conn.post(uri.path) do |req|
958
+ req.body = JSON.generate(body) # Manually serialize to JSON
959
+ req.headers['Content-Type'] = 'application/json'
960
+ req.headers['Accept'] = 'application/json'
961
+ end
962
+
963
+ # Manually handle HTTP redirects if requested
964
+ redirect_count = 0
965
+ while support_redirections &&
966
+ response.status.between?(300, 399) &&
967
+ response.headers['location'] &&
968
+ redirect_count < max_redirects
969
+
970
+ redirect_count += 1
971
+ new_url = URI.join(url, response.headers['location']).to_s
972
+
973
+ # Update `url` for the next iteration
974
+ url = new_url
975
+ uri = URI.parse(new_url)
976
+
977
+ response = conn.post(uri.path) do |req|
978
+ req.body = JSON.generate(body)
979
+ req.headers['Content-Type'] = 'application/json'
980
+ req.headers['Accept'] = 'application/json'
981
+ end
982
+ end
983
+
984
+ # Raise an error if the final response is not 2xx
985
+ unless response.success?
986
+ raise "HTTP #{response.status}: #{response.body}"
987
+ end
988
+
989
+ # If all is good, return the Faraday response object
990
+ return response
991
+
992
+ rescue Errno::ETIMEDOUT, Timeout::Error, Faraday::ConnectionFailed => e
993
+ # Basic retry logic for transient network errors
994
+ attempts += 1
995
+ if attempts < DEFAULT_RETRY_ATTEMPTS
996
+ sleep_time = 2**attempts
997
+ sleep(sleep_time) # Exponential backoff
998
+ retry
999
+ else
1000
+ # Too many failures, re-raise
1001
+ raise e
1002
+ end
1003
+ rescue => e
1004
+ # For any other error, just re-raise
1005
+ raise e
1006
+ end
953
1007
  end
954
- ret
955
- end
1008
+ end # def self.call_post
956
1009
 
957
1010
  # TODO: deprecated
958
1011
  def self.api_call(url, params={}, method=BlackStack::Netting::CALL_METHOD_POST, ssl_verify_mode=BlackStack::Netting::DEFAULT_SSL_VERIFY_MODE, max_retries=5)
@@ -963,9 +1016,8 @@ module BlackStack
963
1016
  while (nTries < max_retries && bSuccess == false)
964
1017
  begin
965
1018
  nTries = nTries + 1
966
- uri = URI(url)
967
- res = BlackStack::Netting::call_post(uri, params, ssl_verify_mode) if method==BlackStack::Netting::CALL_METHOD_POST
968
- res = BlackStack::Netting::call_get(uri, params, ssl_verify_mode) if method==BlackStack::Netting::CALL_METHOD_GET
1019
+ res = BlackStack::Netting::call_post(url, params, ssl_verify_mode) if method==BlackStack::Netting::CALL_METHOD_POST
1020
+ res = BlackStack::Netting::call_get(url, params, ssl_verify_mode) if method==BlackStack::Netting::CALL_METHOD_GET
969
1021
  parsed = JSON.parse(res.body)
970
1022
  if (parsed['status']==BlackStack::Netting::SUCCESS)
971
1023
  bSuccess = true
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blackstack-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.29
4
+ version: 1.2.31
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leandro Daniel Sardi
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2025-01-13 00:00:00.000000000 Z
10
+ date: 2025-01-30 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: content_spinning
@@ -86,7 +85,6 @@ homepage: https://rubygems.org/gems/blackstack-core
86
85
  licenses:
87
86
  - MIT
88
87
  metadata: {}
89
- post_install_message:
90
88
  rdoc_options: []
91
89
  require_paths:
92
90
  - lib
@@ -101,8 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
99
  - !ruby/object:Gem::Version
102
100
  version: '0'
103
101
  requirements: []
104
- rubygems_version: 3.3.7
105
- signing_key:
102
+ rubygems_version: 3.6.3
106
103
  specification_version: 4
107
104
  summary: Core modules, functions and constants for the BlackStack framework.
108
105
  test_files: []