diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..1ad899d Binary files /dev/null and b/.DS_Store differ diff --git a/lib/app_store_connect/client.rb b/lib/app_store_connect/client.rb index 5430c5d..b4d793b 100644 --- a/lib/app_store_connect/client.rb +++ b/lib/app_store_connect/client.rb @@ -6,7 +6,7 @@ require 'app_store_connect/request' module AppStoreConnect - class Client # rubocop:disable Metrics/ClassLength + class Client def initialize(**kwargs) @options = options(**kwargs) @@ -41,27 +41,24 @@ def web_service_endpoint_names end def call(web_service_endpoint, **kwargs) + parser = proc do |response| + JSON.parse(response.body) + end + case web_service_endpoint.http_method when :get - get(web_service_endpoint, **kwargs) + get(web_service_endpoint, **kwargs, &parser) when :post - post(web_service_endpoint, **kwargs) + post(web_service_endpoint, **kwargs, &parser) else raise "invalid http method: #{web_service_endpoint.http_method}" end end - def build_url(web_service_endpoint, **kwargs) - web_service_endpoint - .url - .gsub(/(\{(\w+)\})/) { kwargs.fetch(Regexp.last_match(2).to_sym) } - end - - def url_parameter_names(web_service_endpoint) - web_service_endpoint + def build_uri(web_service_endpoint, **kwargs) + URI(web_service_endpoint .url - .scan(/(\{(\w+)\})/) - .map { |_, n| n.to_sym } + .gsub(/(\{(\w+)\})/) { kwargs.fetch(Regexp.last_match(2).to_sym) }) end def web_service_endpoint_by(name:) @@ -88,49 +85,36 @@ def options(**kwargs) end end - def build_body(request) - request - .to_h - .deep_transform_keys { |k| k.to_s.camelize(:lower) } - .to_json - end - - def build_query(web_service_endpoint, **kwargs) - query_parameters = kwargs.dup.tap do |hash| - url_parameter_names(web_service_endpoint).each do |name| - hash.delete(name.to_sym) - end - end - - query_parameters.to_query - end - - def get(web_service_endpoint, **kwargs) - url = build_url(web_service_endpoint, **kwargs) - query = build_query(web_service_endpoint, **kwargs) - + def get(web_service_endpoint, **kwargs, &block) request = Request.new( + kwargs: kwargs, + web_service_endpoint: web_service_endpoint, http_method: :get, - url: url, - headers: headers, - query: query + uri: build_uri(web_service_endpoint, **kwargs), + headers: headers ) - request.execute + request.execute(&block) end - def post(web_service_endpoint, **kwargs) - url = build_url(web_service_endpoint, **kwargs) - request = "AppStoreConnect::#{web_service_endpoint.http_body_type}".constantize.new(**kwargs) - body = build_body(request) - - response = execute(:post, url, headers: headers, body: body) - - response + def http_body(web_service_endpoint, **kwargs) + "AppStoreConnect::#{web_service_endpoint.http_body_type}" + .constantize + .new(**kwargs) + .to_json end - def execute(http_method, url, **options) - HTTParty.send(http_method, url, options) + def post(web_service_endpoint, **kwargs, &block) + Request.new( + web_service_endpoint: web_service_endpoint, + kwargs: kwargs, + http_method: :post, + uri: build_uri(web_service_endpoint, **kwargs), + headers: headers, + http_body: http_body(web_service_endpoint, **kwargs) + ) + + request.execute(&block) end def headers diff --git a/lib/app_store_connect/request.rb b/lib/app_store_connect/request.rb index 0e97bdd..d6dc85e 100644 --- a/lib/app_store_connect/request.rb +++ b/lib/app_store_connect/request.rb @@ -3,21 +3,38 @@ require 'net/http' module AppStoreConnect + attr_reader :uri + class Request def initialize(**options) + @uri = options.fetch(:uri) @options = options end def execute - response = Net::HTTP.start(uri.host, uri.port, net_http_options) do |http| - http.request(request) - end + Net::HTTP.start(uri.host, uri.port, net_http_options) do |http| + response = http.request(request) - JSON.parse(response.body) + yield response if block_given? + end end private + def web_service_endpoint + @options.fetch(:web_service_endpoint) + end + + def query + return unless http_method == :get + + kwargs.dup.tap do |hash| + url_parameter_names(web_service_endpoint).each do |name| + hash.delete(name.to_sym) + end + end.to_query + end + def http_method @options.fetch(:http_method).to_sym end @@ -26,16 +43,13 @@ def net_http_options { use_ssl: uri.scheme == 'https' } end - def query - return unless http_method == :get - return unless @options[:query] - - @options[:query] + def kwargs + @options.fetch(:kwargs, {}) end def uri - URI(@options.fetch(:url)).tap do |uri| - uri.query = query + @options.fetch(:uri).tap do |uri| + uri.query = query if http_method == :get end end @@ -43,16 +57,31 @@ def headers @options.fetch(:headers, {}) end + def body + return if http_method == :get + + @options + .fetch(:request) + .to_h + .deep_transform_keys { |k| k.to_s.camelize(:lower) } + .to_json + end + + def url_parameter_names(web_service_endpoint) + web_service_endpoint + .url + .scan(/(\{(\w+)\})/) + .map { |_, n| n.to_sym } + end + def request case http_method when :get - Net::HTTP::Get.new(uri).tap do |request| - headers.each do |key, value| - request[key] = value - end - end + Net::HTTP::Get.new(uri, headers) when :post - Net::HTTP::Post.new(uri, headers) + Net::HTTP::Post.new(uri, headers).tap do |request| + request.body = body + end else raise "unsupported http method: #{http_method}" end