lib/storyblok/client.rb in storyblok-2.0.0 vs lib/storyblok/client.rb in storyblok-2.0.1
- old
+ new
@@ -20,18 +20,26 @@
}
attr_reader :configuration, :logger
# @param [Hash] given_configuration
- # @option given_configuration [String] :token Required
+ # @option given_configuration [String] :token Required if oauth_token is not set
+ # @option given_configuration [String] :oauth_token Required if token is not set
# @option given_configuration [String] :api_url
# @option given_configuration [Number] :api_version
# @option given_configuration [false, ::Logger] :logger
# @option given_configuration [::Logger::DEBUG, ::Logger::INFO, ::Logger::WARN, ::Logger::ERROR] :log_level
def initialize(given_configuration = {})
@configuration = default_configuration.merge(given_configuration)
validate_configuration!
+
+ if configuration[:oauth_token]
+ @rest_client = RestClient::Resource.new(base_url, :headers => {
+ :authorization => configuration[:oauth_token]
+ })
+ end
+
setup_logger
end
# Gets a collection of stories
#
@@ -86,11 +94,51 @@
# @return [Hash]
def tree(query = {})
Links.new(Request.new(self, '/cdn/links', query).get).as_tree
end
- def get(request)
+ def post(path, payload, additional_headers = {})
+ run_management_request(:post, path, payload, additional_headers)
+ end
+
+ def put(path, payload, additional_headers = {})
+ run_management_request(:put, path, payload, additional_headers)
+ end
+
+ def delete(path, additional_headers = {})
+ run_management_request(:delete, path, nil, additional_headers)
+ end
+
+ def get(path, additional_headers = {})
+ run_management_request(:get, path, nil, additional_headers)
+ end
+
+ def run_management_request(action, path, payload = {}, additional_headers = {})
+ logger.info(request: { path: path, action: action }) if logger
+ retries_left = 3
+
+ begin
+ if [:post, :put].include?(action)
+ res = @rest_client[path].send(action, payload, additional_headers)
+ else
+ res = @rest_client[path].send(action, additional_headers)
+ end
+ rescue RestClient::TooManyRequests
+ if retries_left != 0
+ retries_left -= 1
+ logger.info("Too many requests. Retry nr. #{(3 - retries_left).to_s} of max. 3 times.") if logger
+ sleep(0.5)
+ retry
+ end
+
+ raise
+ end
+
+ parse_result(res)
+ end
+
+ def cached_get(request)
endpoint = base_url + request.url
query = request_query(request.query)
query_string = build_nested_query(query)
if cache.nil?
@@ -113,15 +161,33 @@
end
end
private
+ def parse_result(res)
+ {'headers' => res.headers, 'data' => JSON.parse(res.body)}
+ end
+
def run_request(endpoint, query_string)
logger.info(request: { endpoint: endpoint, query: query_string }) if logger
- res = RestClient.get "#{endpoint}?#{query_string}"
- {headers: res.headers, data: JSON.parse(res.body)}.to_json
+ retries_left = 3
+
+ begin
+ res = RestClient.get "#{endpoint}?#{query_string}"
+ rescue RestClient::TooManyRequests
+ if retries_left != 0
+ retries_left -= 1
+ logger.info("Too many requests. Retry nr. #{(3 - retries_left).to_s} of max. 3 times.") if logger
+ sleep(0.5)
+ retry
+ end
+
+ raise
+ end
+
+ {'headers' => res.headers, 'data' => JSON.parse(res.body)}.to_json
end
# Patches a query hash with the client configurations for queries
def request_query(query)
query[:token] = configuration[:token]
@@ -147,10 +213,10 @@
@logger = configuration[:logger]
logger.level = configuration[:log_level] if logger
end
def validate_configuration!
- fail ArgumentError, 'You will need to initialize a client with an :token' if configuration[:token].empty?
+ fail ArgumentError, 'You will need to initialize a client with an :token or :oauth_token' if !configuration[:token] and !configuration[:oauth_token]
fail ArgumentError, 'The client configuration needs to contain an :api_url' if configuration[:api_url].empty?
fail ArgumentError, 'The :api_version must be a positive number' unless configuration[:api_version].to_i >= 0
end
def build_nested_query(value, prefix = nil)