spaceship/lib/spaceship/connect_api/client.rb in fastlane-2.125.0.beta.20190531200016 vs spaceship/lib/spaceship/connect_api/client.rb in fastlane-2.125.0

- old
+ new

@@ -1,8 +1,10 @@ require_relative '../client' +require_relative './response' module Spaceship + # rubocop:disable Metrics/ClassLength module ConnectAPI class Client < Spaceship::Client ## # Spaceship HTTP client for the App Store Connect API. # @@ -16,80 +18,95 @@ def self.hostname 'https://appstoreconnect.apple.com/iris/v1/' end + # + # Helpers + # + def build_params(filter: nil, includes: nil, limit: nil, sort: nil, cursor: nil) params = {} + filter = filter.delete_if { |k, v| v.nil? } if filter + params[:filter] = filter if filter && !filter.empty? params[:include] = includes if includes params[:limit] = limit if limit params[:sort] = sort if sort params[:cursor] = cursor if cursor return params end - def get_beta_app_review_detail(filter: {}, includes: nil, limit: nil, sort: nil) - # GET - # https://appstoreconnect.apple.com/iris/v1/betaAppReviewDetails?filter[app]=<app_id> - params = build_params(filter: filter, includes: includes, limit: limit, sort: sort) - - response = request(:get, "betaAppReviewDetails") do |req| + def get(url_or_path, params = nil) + response = request(:get) do |req| + req.url(url_or_path) req.options.params_encoder = Faraday::NestedParamsEncoder - req.params = params + req.params = params if params end handle_response(response) end - def patch_beta_app_review_detail(app_id: nil, attributes: {}) - # PATCH - # https://appstoreconnect.apple.com/iris/v1/apps/<app_id>/betaAppReviewDetails - path = "betaAppReviewDetails/#{app_id}" + def post(url_or_path, body) + response = request(:post) do |req| + req.url(url_or_path) + req.body = body.to_json + req.headers['Content-Type'] = 'application/json' + end + handle_response(response) + end - body = { - data: { - attributes: attributes, - id: app_id, - type: "betaAppReviewDetails" - } - } - + def patch(url_or_path, body) response = request(:patch) do |req| - req.url(path) + req.url(url_or_path) req.body = body.to_json req.headers['Content-Type'] = 'application/json' end handle_response(response) end - def get_beta_app_localizations(filter: {}, includes: nil, limit: nil, sort: nil) - # GET - # https://appstoreconnect.apple.com/iris/v1/betaAppLocalizations?filter[app]=<app_id> - params = build_params(filter: filter, includes: includes, limit: limit, sort: sort) - - response = request(:get, "betaAppLocalizations") do |req| - req.options.params_encoder = Faraday::NestedParamsEncoder - req.params = params + def delete(url_or_path, params = nil, body = nil) + response = request(:delete) do |req| + req.url(url_or_path) + req.options.params_encoder = Faraday::NestedParamsEncoder if params + req.params = params if params + req.body = body.to_json if body + req.headers['Content-Type'] = 'application/json' if body end handle_response(response) end - def get_beta_build_localizations(filter: {}, includes: nil, limit: nil, sort: nil) + # + # apps + # + + def get_apps(filter: {}, includes: nil, limit: nil, sort: nil) # GET - # https://appstoreconnect.apple.com/iris/v1/betaBuildLocalizations?filter[build]=<build_id> - path = "betaBuildLocalizations" + # https://appstoreconnect.apple.com/iris/v1/apps params = build_params(filter: filter, includes: includes, limit: limit, sort: sort) + get("apps", params) + end - response = request(:get, path) do |req| - req.options.params_encoder = Faraday::NestedParamsEncoder - req.params = params - end - handle_response(response) + def get_app(app_id: nil, includes: nil) + # GET + # https://appstoreconnect.apple.com/iris/v1/apps/<app_id> + params = build_params(filter: nil, includes: includes, limit: nil, sort: nil) + get("apps/#{app_id}", params) end + # + # betaAppLocalizations + # + + def get_beta_app_localizations(filter: {}, includes: nil, limit: nil, sort: nil) + # GET + # https://appstoreconnect.apple.com/iris/v1/betaAppLocalizations?filter[app]=<app_id> + params = build_params(filter: filter, includes: includes, limit: limit, sort: sort) + get("betaAppLocalizations", params) + end + def post_beta_app_localizations(app_id: nil, attributes: {}) # POST # https://appstoreconnect.apple.com/iris/v1/betaAppLocalizations path = "betaAppLocalizations" @@ -106,16 +123,11 @@ } } } } - response = request(:post) do |req| - req.url(path) - req.body = body.to_json - req.headers['Content-Type'] = 'application/json' - end - handle_response(response) + post(path, body) end def patch_beta_app_localizations(localization_id: nil, attributes: {}) # PATCH # https://appstoreconnect.apple.com/iris/v1/apps/<app_id>/betaAppLocalizations/<localization_id> @@ -127,18 +139,91 @@ id: localization_id, type: "betaAppLocalizations" } } - response = request(:patch) do |req| - req.url(path) - req.body = body.to_json - req.headers['Content-Type'] = 'application/json' - end - handle_response(response) + patch(path, body) end + # + # betaAppReviewDetails + # + + def get_beta_app_review_detail(filter: {}, includes: nil, limit: nil, sort: nil) + # GET + # https://appstoreconnect.apple.com/iris/v1/betaAppReviewDetails?filter[app]=<app_id> + params = build_params(filter: filter, includes: includes, limit: limit, sort: sort) + get("betaAppReviewDetails", params) + end + + def patch_beta_app_review_detail(app_id: nil, attributes: {}) + # PATCH + # https://appstoreconnect.apple.com/iris/v1/apps/<app_id>/betaAppReviewDetails + path = "betaAppReviewDetails/#{app_id}" + + body = { + data: { + attributes: attributes, + id: app_id, + type: "betaAppReviewDetails" + } + } + + patch(path, body) + end + + # + # betaAppReviewSubmissions + # + + def get_beta_app_review_submissions(filter: {}, includes: nil, limit: nil, sort: nil, cursor: nil) + # GET + # https://appstoreconnect.apple.com/iris/v1/betaAppReviewSubmissions + params = build_params(filter: filter, includes: includes, limit: limit, sort: sort, cursor: cursor) + get("betaAppReviewSubmissions", params) + end + + def post_beta_app_review_submissions(build_id: nil) + # POST + # https://appstoreconnect.apple.com/iris/v1/betaAppReviewSubmissions + path = "betaAppReviewSubmissions" + body = { + data: { + type: "betaAppReviewSubmissions", + relationships: { + build: { + data: { + type: "builds", + id: build_id + } + } + } + } + } + + post(path, body) + end + + def delete_beta_app_review_submission(beta_app_review_submission_id: nil) + # DELETE + # https://appstoreconnect.apple.com/iris/v1/betaAppReviewSubmissions/<beta_app_review_submission_id> + params = build_params(filter: nil, includes: nil, limit: nil, sort: nil, cursor: nil) + delete("betaAppReviewSubmissions/#{beta_app_review_submission_id}", params) + end + + # + # betaBuildLocalizations + # + + def get_beta_build_localizations(filter: {}, includes: nil, limit: nil, sort: nil) + # GET + # https://appstoreconnect.apple.com/iris/v1/betaBuildLocalizations + path = "betaBuildLocalizations" + params = build_params(filter: filter, includes: includes, limit: limit, sort: sort) + get(path, params) + end + def post_beta_build_localizations(build_id: nil, attributes: {}) # POST # https://appstoreconnect.apple.com/iris/v1/betaBuildLocalizations path = "betaBuildLocalizations" @@ -155,16 +240,11 @@ } } } } - response = request(:post) do |req| - req.url(path) - req.body = body.to_json - req.headers['Content-Type'] = 'application/json' - end - handle_response(response) + post(path, body) end def patch_beta_build_localizations(localization_id: nil, feedbackEmail: nil, attributes: {}) # PATCH # https://appstoreconnect.apple.com/iris/v1/apps/<app_id>/betaBuildLocalizations @@ -176,73 +256,151 @@ id: localization_id, type: "betaBuildLocalizations" } } - response = request(:patch) do |req| - req.url(path) - req.body = body.to_json - req.headers['Content-Type'] = 'application/json' - end - handle_response(response) + patch(path, body) end - def get_build_beta_details(filter: {}, includes: nil, limit: nil, sort: nil) + # + # betaBuildMetrics + # + + def get_beta_build_metrics(filter: {}, includes: nil, limit: nil, sort: nil) # GET - # https://appstoreconnect.apple.com/iris/v1/buildBetaDetails + # https://appstoreconnect.apple.com/iris/v1/betaBuildMetrics params = build_params(filter: filter, includes: includes, limit: limit, sort: sort) + get("betaBuildMetrics", params) + end - response = request(:get, "buildBetaDetails") do |req| - req.options.params_encoder = Faraday::NestedParamsEncoder - req.params = params - end - handle_response(response) + # + # betaGroups + # + + def get_beta_groups(filter: {}, includes: nil, limit: nil, sort: nil) + # GET + # https://appstoreconnect.apple.com/iris/v1/betaGroups + params = build_params(filter: filter, includes: includes, limit: limit, sort: sort) + get("betaGroups", params) end - def patch_build_beta_details(build_beta_details_id: nil, attributes: {}) - # PATCH - # https://appstoreconnect.apple.com/iris/v1/buildBetaDetails/<build_beta_details_id> - path = "buildBetaDetails/#{build_beta_details_id}" + def add_beta_groups_to_build(build_id: nil, beta_group_ids: []) + # POST + # https://appstoreconnect.apple.com/iris/v1/builds/<build_id>/relationships/betaGroups + path = "builds/#{build_id}/relationships/betaGroups" + body = { + data: beta_group_ids.map do |id| + { + type: "betaGroups", + id: id + } + end + } + post(path, body) + end + + # + # betaTesters + # + + def get_beta_testers(filter: {}, includes: nil, limit: nil, sort: nil) + # GET + # https://appstoreconnect.apple.com/iris/v1/betaTesters + params = build_params(filter: filter, includes: includes, limit: limit, sort: sort) + get("betaTesters", params) + end + + # beta_testers - [{email: "", firstName: "", lastName: ""}] + def post_bulk_beta_tester_assignments(beta_group_id: nil, beta_testers: nil) + # POST + # https://appstoreconnect.apple.com/iris/v1/bulkBetaTesterAssignments + beta_testers || [] + + beta_testers.map do |tester| + tester[:errors] = [] + end + body = { data: { - attributes: attributes, - id: build_beta_details_id, - type: "buildBetaDetails" + attributes: { + betaTesters: beta_testers + }, + relationships: { + betaGroup: { + data: { + type: "betaGroups", + id: beta_group_id + } + } + }, + type: "bulkBetaTesterAssignments" } } - response = request(:patch) do |req| - req.url(path) - req.body = body.to_json - req.headers['Content-Type'] = 'application/json' - end - handle_response(response) + post("bulkBetaTesterAssignments", body) end - def get_build_deliveries(filter: {}, includes: nil, limit: 10, sort: nil) + def delete_beta_tester_from_apps(beta_tester_id: nil, app_ids: []) + # DELETE + # https://appstoreconnect.apple.com/iris/v1/betaTesters/<beta_tester_id>/relationships/apps + path = "betaTesters/#{beta_tester_id}/relationships/apps" + body = { + data: app_ids.map do |id| + { + type: "apps", + id: id + } + end + } + + delete(path, nil, body) + end + + def delete_beta_tester_from_beta_groups(beta_tester_id: nil, beta_group_ids: []) + # DELETE + # https://appstoreconnect.apple.com/iris/v1/betaTesters/<beta_tester_id>/relationships/betaGroups + path = "betaTesters/#{beta_tester_id}/relationships/betaGroups" + body = { + data: beta_group_ids.map do |id| + { + type: "betaGroups", + id: id + } + end + } + + delete(path, nil, body) + end + + # + # betaTesterMetrics + # + + def get_beta_tester_metrics(filter: {}, includes: nil, limit: nil, sort: nil) # GET - # https://appstoreconnect.apple.com/iris/v1/buildDeliveries + # https://appstoreconnect.apple.com/iris/v1/betaTesterMetrics params = build_params(filter: filter, includes: includes, limit: limit, sort: sort) - - response = request(:get, "buildDeliveries") do |req| - req.options.params_encoder = Faraday::NestedParamsEncoder - req.params = params - end - handle_response(response) + get("betaTesterMetrics", params) end - def get_builds(filter: {}, includes: "buildBetaDetail,betaBuildMetrics", limit: 10, sort: "uploadedDate", cursor: nil, only_data: true) + # + # builds + # + + def get_builds(filter: {}, includes: "buildBetaDetail,betaBuildMetrics", limit: 10, sort: "uploadedDate", cursor: nil) # GET # https://appstoreconnect.apple.com/iris/v1/builds params = build_params(filter: filter, includes: includes, limit: limit, sort: sort, cursor: cursor) + get("builds", params) + end - response = request(:get, "builds") do |req| - req.options.params_encoder = Faraday::NestedParamsEncoder - req.params = params - end - handle_response(response, only_data: only_data) + def get_build(build_id: nil, includes: nil) + # GET + # https://appstoreconnect.apple.com/iris/v1/builds/<build_id>? + params = build_params(filter: nil, includes: includes, limit: nil, sort: nil, cursor: nil) + get("builds/#{build_id}", params) end def patch_builds(build_id: nil, attributes: {}) # PATCH # https://appstoreconnect.apple.com/iris/v1/builds/<build_id> @@ -254,92 +412,76 @@ id: build_id, type: "builds" } } - response = request(:patch) do |req| - req.url(path) - req.body = body.to_json - req.headers['Content-Type'] = 'application/json' - end - handle_response(response) + patch(path, body) end - def post_beta_app_review_submissions(build_id: nil) - # POST - # https://appstoreconnect.apple.com/iris/v1/betaAppReviewSubmissions + # + # buildBetaDetails + # + def get_build_beta_details(filter: {}, includes: nil, limit: nil, sort: nil) + # GET + # https://appstoreconnect.apple.com/iris/v1/buildBetaDetails + params = build_params(filter: filter, includes: includes, limit: limit, sort: sort) + get("buildBetaDetails", params) + end + + def patch_build_beta_details(build_beta_details_id: nil, attributes: {}) + # PATCH + # https://appstoreconnect.apple.com/iris/v1/buildBetaDetails/<build_beta_details_id> + path = "buildBetaDetails/#{build_beta_details_id}" + body = { data: { - type: "betaAppReviewSubmissions", - relationships: { - build: { - data: { - type: "builds", - id: build_id - } - } - } + attributes: attributes, + id: build_beta_details_id, + type: "buildBetaDetails" } } - response = request(:post) do |req| - req.url("betaAppReviewSubmissions") - req.body = body.to_json - req.headers['Content-Type'] = 'application/json' - end - handle_response(response) + patch(path, body) end - def get_pre_release_versions(filter: {}, includes: nil, limit: 40, sort: nil) + # + # buildDeliveries + # + + def get_build_deliveries(filter: {}, includes: nil, limit: nil, sort: nil) # GET - # https://appstoreconnect.apple.com/iris/v1/preReleaseVersions + # https://appstoreconnect.apple.com/iris/v1/buildDeliveries params = build_params(filter: filter, includes: includes, limit: limit, sort: sort) - - response = request(:get, "preReleaseVersions") do |req| - req.options.params_encoder = Faraday::NestedParamsEncoder - req.params = params - end - handle_response(response) + get("buildDeliveries", params) end - def get_beta_groups(filter: {}, includes: nil, limit: 40, sort: nil) + # + # preReleaseVersions + # + + def get_pre_release_versions(filter: {}, includes: nil, limit: nil, sort: nil) # GET - # https://appstoreconnect.apple.com/iris/v1/betaGroups + # https://appstoreconnect.apple.com/iris/v1/preReleaseVersions params = build_params(filter: filter, includes: includes, limit: limit, sort: sort) - - response = request(:get, "betaGroups") do |req| - req.options.params_encoder = Faraday::NestedParamsEncoder - req.params = params - end - handle_response(response) + get("preReleaseVersions", params) end - def add_beta_groups_to_build(build_id: nil, beta_group_ids: []) - # POST - # https://appstoreconnect.apple.com/iris/v1/builds/<build_id>/relationships/betaGroups + # + # users + # - body = { - data: beta_group_ids.map do |id| - { - type: "betaGroups", - id: id - } - end - } - - response = request(:post) do |req| - req.url("builds/#{build_id}/relationships/betaGroups") - req.body = body.to_json - req.headers['Content-Type'] = 'application/json' - end - handle_response(response) + def get_users(filter: {}, includes: nil, limit: nil, sort: nil) + # GET + # https://appstoreconnect.apple.com/iris/v1/users + params = build_params(filter: filter, includes: includes, limit: limit, sort: sort) + get("users", params) end protected - def handle_response(response, only_data: true) + def handle_response(response) if (200...300).cover?(response.status) && (response.body.nil? || response.body.empty?) return end raise InternalServerError, "Server error got #{response.status}" if (500...600).cover?(response.status) @@ -352,13 +494,11 @@ raise UnexpectedResponse, handle_errors(response) if response.body['errors'] raise UnexpectedResponse, "Temporary App Store Connect error: #{response.body}" if response.body['statusCode'] == 'ERROR' - return response.body['data'] if response.body['data'] && only_data - - return response.body + return Spaceship::ConnectAPI::Response.new(body: response.body, status: response.status) end def handle_errors(response) # Example error format # { @@ -405,6 +545,7 @@ return team_id if self.provider.nil? self.provider.provider_id end end end + # rubocop:enable Metrics/ClassLength end