# encoding: utf-8
# Code generated by Microsoft (R) AutoRest Code Generator 1.0.1.0
# Changes may cause incorrect behavior and will be lost if the code is
# regenerated.

module MobileCenterApi
  #
  # Microsoft Visual Studio Mobile Center API
  #
  class Releases

    #
    # Creates and initializes a new instance of the Releases class.
    # @param client service class for accessing basic functionality.
    #
    def initialize(client)
      @client = client
    end

    # @return [MobileCenterClient] reference to the MobileCenterClient
    attr_reader :client

    #
    # Get a release with hash `release_hash`. If multiple releases matches the
    # release_hash, return the latest one. If `release_hash` is `latest`, return
    # the latest release that was distributed to the current user (from all the
    # distribution groups).
    #
    # @param app_secret [String] The secret of the target application
    # @param release_hash [String] The hash of the release, or `latest` to get the
    # latest release from all the distribution groups assigned to the current user.
    # @param internal_app_id [String] The app ID
    # @param custom_headers [Hash{String => String}] A hash of custom headers that
    # will be added to the HTTP request.
    #
    # @return [Object] operation results.
    #
    def get_latest_by_hash(app_secret, release_hash, internal_app_id, custom_headers = nil)
      response = get_latest_by_hash_async(app_secret, release_hash, internal_app_id, custom_headers).value!
      response.body unless response.nil?
    end

    #
    # Get a release with hash `release_hash`. If multiple releases matches the
    # release_hash, return the latest one. If `release_hash` is `latest`, return
    # the latest release that was distributed to the current user (from all the
    # distribution groups).
    #
    # @param app_secret [String] The secret of the target application
    # @param release_hash [String] The hash of the release, or `latest` to get the
    # latest release from all the distribution groups assigned to the current user.
    # @param internal_app_id [String] The app ID
    # @param custom_headers [Hash{String => String}] A hash of custom headers that
    # will be added to the HTTP request.
    #
    # @return [MsRest::HttpOperationResponse] HTTP response information.
    #
    def get_latest_by_hash_with_http_info(app_secret, release_hash, internal_app_id, custom_headers = nil)
      get_latest_by_hash_async(app_secret, release_hash, internal_app_id, custom_headers).value!
    end

    #
    # Get a release with hash `release_hash`. If multiple releases matches the
    # release_hash, return the latest one. If `release_hash` is `latest`, return
    # the latest release that was distributed to the current user (from all the
    # distribution groups).
    #
    # @param app_secret [String] The secret of the target application
    # @param release_hash [String] The hash of the release, or `latest` to get the
    # latest release from all the distribution groups assigned to the current user.
    # @param internal_app_id [String] The app ID
    # @param [Hash{String => String}] A hash of custom headers that will be added
    # to the HTTP request.
    #
    # @return [Concurrent::Promise] Promise object which holds the HTTP response.
    #
    def get_latest_by_hash_async(app_secret, release_hash, internal_app_id, custom_headers = nil)
      fail ArgumentError, 'app_secret is nil' if app_secret.nil?
      fail ArgumentError, 'release_hash is nil' if release_hash.nil?
      fail ArgumentError, 'internal_app_id is nil' if internal_app_id.nil?


      request_headers = {}

      # Set Headers
      request_headers['internal-app-id'] = internal_app_id unless internal_app_id.nil?
      path_template = 'v0.1/sdk/apps/{app_secret}/releases/{release_hash}'

      request_url = @base_url || @client.base_url

      options = {
          middlewares: [[MsRest::RetryPolicyMiddleware, times: 3, retry: 0.02], [:cookie_jar]],
          path_params: {'app_secret' => app_secret,'release_hash' => release_hash},
          headers: request_headers.merge(custom_headers || {}),
          base_url: request_url
      }
      promise = @client.make_request_async(:get, path_template, options)

      promise = promise.then do |result|
        http_response = result.response
        status_code = http_response.status
        response_content = http_response.body
        unless status_code == 200 || status_code == 404 || status_code == 500
          error_model = JSON.load(response_content)
          fail MsRest::HttpOperationError.new(result.request, http_response, error_model)
        end

        # Deserialize Response
        if status_code == 200
          begin
            parsed_response = response_content.to_s.empty? ? nil : JSON.load(response_content)
            result_mapper = MobileCenterApi::Models::ReleaseDetails.mapper()
            result.body = @client.deserialize(result_mapper, parsed_response, 'result.body')
          rescue Exception => e
            fail MsRest::DeserializationError.new('Error occurred in deserializing the response', e.message, e.backtrace, result)
          end
        end
        # Deserialize Response
        if status_code == 404
          begin
            parsed_response = response_content.to_s.empty? ? nil : JSON.load(response_content)
            result_mapper = MobileCenterApi::Models::ErrorDetails.mapper()
            result.body = @client.deserialize(result_mapper, parsed_response, 'result.body')
          rescue Exception => e
            fail MsRest::DeserializationError.new('Error occurred in deserializing the response', e.message, e.backtrace, result)
          end
        end
        # Deserialize Response
        if status_code == 500
          begin
            parsed_response = response_content.to_s.empty? ? nil : JSON.load(response_content)
            result_mapper = MobileCenterApi::Models::ErrorDetails.mapper()
            result.body = @client.deserialize(result_mapper, parsed_response, 'result.body')
          rescue Exception => e
            fail MsRest::DeserializationError.new('Error occurred in deserializing the response', e.message, e.backtrace, result)
          end
        end

        result
      end

      promise.execute
    end

    #
    # Get a release with id `release_id`. if `release_id` is `latest`, return the
    # latest release that was distributed to the current user (from all the
    # distribution groups).
    #
    # @param release_id [String] The ID of the release, or `latest` to get the
    # latest release from all the distribution groups assigned to the current user.
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param custom_headers [Hash{String => String}] A hash of custom headers that
    # will be added to the HTTP request.
    #
    # @return [Object] operation results.
    #
    def get_latest_by_user(release_id, owner_name, app_name, custom_headers = nil)
      response = get_latest_by_user_async(release_id, owner_name, app_name, custom_headers).value!
      response.body unless response.nil?
    end

    #
    # Get a release with id `release_id`. if `release_id` is `latest`, return the
    # latest release that was distributed to the current user (from all the
    # distribution groups).
    #
    # @param release_id [String] The ID of the release, or `latest` to get the
    # latest release from all the distribution groups assigned to the current user.
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param custom_headers [Hash{String => String}] A hash of custom headers that
    # will be added to the HTTP request.
    #
    # @return [MsRest::HttpOperationResponse] HTTP response information.
    #
    def get_latest_by_user_with_http_info(release_id, owner_name, app_name, custom_headers = nil)
      get_latest_by_user_async(release_id, owner_name, app_name, custom_headers).value!
    end

    #
    # Get a release with id `release_id`. if `release_id` is `latest`, return the
    # latest release that was distributed to the current user (from all the
    # distribution groups).
    #
    # @param release_id [String] The ID of the release, or `latest` to get the
    # latest release from all the distribution groups assigned to the current user.
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param [Hash{String => String}] A hash of custom headers that will be added
    # to the HTTP request.
    #
    # @return [Concurrent::Promise] Promise object which holds the HTTP response.
    #
    def get_latest_by_user_async(release_id, owner_name, app_name, custom_headers = nil)
      fail ArgumentError, 'release_id is nil' if release_id.nil?
      fail ArgumentError, 'owner_name is nil' if owner_name.nil?
      fail ArgumentError, 'app_name is nil' if app_name.nil?


      request_headers = {}
      path_template = 'v0.1/apps/{owner_name}/{app_name}/releases/{release_id}'

      request_url = @base_url || @client.base_url

      options = {
          middlewares: [[MsRest::RetryPolicyMiddleware, times: 3, retry: 0.02], [:cookie_jar]],
          path_params: {'release_id' => release_id,'owner_name' => owner_name,'app_name' => app_name},
          headers: request_headers.merge(custom_headers || {}),
          base_url: request_url
      }
      promise = @client.make_request_async(:get, path_template, options)

      promise = promise.then do |result|
        http_response = result.response
        status_code = http_response.status
        response_content = http_response.body
        unless status_code == 200 || status_code == 400 || status_code == 404
          error_model = JSON.load(response_content)
          fail MsRest::HttpOperationError.new(result.request, http_response, error_model)
        end

        # Deserialize Response
        if status_code == 200
          begin
            parsed_response = response_content.to_s.empty? ? nil : JSON.load(response_content)
            result_mapper = MobileCenterApi::Models::ReleaseDetails.mapper()
            result.body = @client.deserialize(result_mapper, parsed_response, 'result.body')
          rescue Exception => e
            fail MsRest::DeserializationError.new('Error occurred in deserializing the response', e.message, e.backtrace, result)
          end
        end
        # Deserialize Response
        if status_code == 400
          begin
            parsed_response = response_content.to_s.empty? ? nil : JSON.load(response_content)
            result_mapper = MobileCenterApi::Models::ErrorDetails.mapper()
            result.body = @client.deserialize(result_mapper, parsed_response, 'result.body')
          rescue Exception => e
            fail MsRest::DeserializationError.new('Error occurred in deserializing the response', e.message, e.backtrace, result)
          end
        end
        # Deserialize Response
        if status_code == 404
          begin
            parsed_response = response_content.to_s.empty? ? nil : JSON.load(response_content)
            result_mapper = MobileCenterApi::Models::ErrorDetails.mapper()
            result.body = @client.deserialize(result_mapper, parsed_response, 'result.body')
          rescue Exception => e
            fail MsRest::DeserializationError.new('Error occurred in deserializing the response', e.message, e.backtrace, result)
          end
        end

        result
      end

      promise.execute
    end

    #
    # Updates a release.
    #
    # @param release_id [Float] The ID of the release
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param distribution_group_name [String] Name of a distribution group. The
    # release will be associated with this distribution group. If the distribution
    # group doesn't exist a 400 is returned. If both distribution group name and id
    # are passed, the id is taking precedence.
    # @param distribution_group_id [String] Id of a distribution group. The release
    # will be associated with this distribution group. If the distribution group
    # doesn't exist a 400 is returned. If both distribution group name and id are
    # passed, the id is taking precedence.
    # @param release_notes [String] Release notes for this release.
    # @param custom_headers [Hash{String => String}] A hash of custom headers that
    # will be added to the HTTP request.
    #
    # @return [Object] operation results.
    #
    def update(release_id, owner_name, app_name, distribution_group_name = nil, distribution_group_id = nil, release_notes = nil, custom_headers = nil)
      response = update_async(release_id, owner_name, app_name, distribution_group_name, distribution_group_id, release_notes, custom_headers).value!
      response.body unless response.nil?
    end

    #
    # Updates a release.
    #
    # @param release_id [Float] The ID of the release
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param distribution_group_name [String] Name of a distribution group. The
    # release will be associated with this distribution group. If the distribution
    # group doesn't exist a 400 is returned. If both distribution group name and id
    # are passed, the id is taking precedence.
    # @param distribution_group_id [String] Id of a distribution group. The release
    # will be associated with this distribution group. If the distribution group
    # doesn't exist a 400 is returned. If both distribution group name and id are
    # passed, the id is taking precedence.
    # @param release_notes [String] Release notes for this release.
    # @param custom_headers [Hash{String => String}] A hash of custom headers that
    # will be added to the HTTP request.
    #
    # @return [MsRest::HttpOperationResponse] HTTP response information.
    #
    def update_with_http_info(release_id, owner_name, app_name, distribution_group_name = nil, distribution_group_id = nil, release_notes = nil, custom_headers = nil)
      update_async(release_id, owner_name, app_name, distribution_group_name, distribution_group_id, release_notes, custom_headers).value!
    end

    #
    # Updates a release.
    #
    # @param release_id [Float] The ID of the release
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param distribution_group_name [String] Name of a distribution group. The
    # release will be associated with this distribution group. If the distribution
    # group doesn't exist a 400 is returned. If both distribution group name and id
    # are passed, the id is taking precedence.
    # @param distribution_group_id [String] Id of a distribution group. The release
    # will be associated with this distribution group. If the distribution group
    # doesn't exist a 400 is returned. If both distribution group name and id are
    # passed, the id is taking precedence.
    # @param release_notes [String] Release notes for this release.
    # @param [Hash{String => String}] A hash of custom headers that will be added
    # to the HTTP request.
    #
    # @return [Concurrent::Promise] Promise object which holds the HTTP response.
    #
    def update_async(release_id, owner_name, app_name, distribution_group_name = nil, distribution_group_id = nil, release_notes = nil, custom_headers = nil)
      fail ArgumentError, 'release_id is nil' if release_id.nil?
      fail ArgumentError, 'owner_name is nil' if owner_name.nil?
      fail ArgumentError, 'app_name is nil' if app_name.nil?

      body = ReleaseUpdateRequest.new
      unless distribution_group_name.nil? && distribution_group_id.nil? && release_notes.nil?
        body.distribution_group_name = distribution_group_name
        body.distribution_group_id = distribution_group_id
        body.release_notes = release_notes
      end

      request_headers = {}

      request_headers['Content-Type'] = 'application/json; charset=utf-8'

      # Serialize Request
      request_mapper = MobileCenterApi::Models::ReleaseUpdateRequest.mapper()
      request_content = @client.serialize(request_mapper,  body, 'body')
      request_content = request_content != nil ? JSON.generate(request_content, quirks_mode: true) : nil

      path_template = 'v0.1/apps/{owner_name}/{app_name}/releases/{release_id}'

      request_url = @base_url || @client.base_url

      options = {
          middlewares: [[MsRest::RetryPolicyMiddleware, times: 3, retry: 0.02], [:cookie_jar]],
          path_params: {'release_id' => release_id,'owner_name' => owner_name,'app_name' => app_name},
          body: request_content,
          headers: request_headers.merge(custom_headers || {}),
          base_url: request_url
      }
      promise = @client.make_request_async(:patch, path_template, options)

      promise = promise.then do |result|
        http_response = result.response
        status_code = http_response.status
        response_content = http_response.body
        unless status_code == 200 || status_code == 400
          error_model = JSON.load(response_content)
          fail MsRest::HttpOperationError.new(result.request, http_response, error_model)
        end

        # Deserialize Response
        if status_code == 200
          begin
            parsed_response = response_content.to_s.empty? ? nil : JSON.load(response_content)
            result_mapper = MobileCenterApi::Models::ReleaseDetails.mapper()
            result.body = @client.deserialize(result_mapper, parsed_response, 'result.body')
          rescue Exception => e
            fail MsRest::DeserializationError.new('Error occurred in deserializing the response', e.message, e.backtrace, result)
          end
        end
        # Deserialize Response
        if status_code == 400
          begin
            parsed_response = response_content.to_s.empty? ? nil : JSON.load(response_content)
            result_mapper = MobileCenterApi::Models::ErrorDetails.mapper()
            result.body = @client.deserialize(result_mapper, parsed_response, 'result.body')
          rescue Exception => e
            fail MsRest::DeserializationError.new('Error occurred in deserializing the response', e.message, e.backtrace, result)
          end
        end

        result
      end

      promise.execute
    end

    #
    # Deletes a release.
    #
    # @param release_id [String] The ID of the release
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param custom_headers [Hash{String => String}] A hash of custom headers that
    # will be added to the HTTP request.
    #
    # @return [ErrorDetails] operation results.
    #
    def delete(release_id, owner_name, app_name, custom_headers = nil)
      response = delete_async(release_id, owner_name, app_name, custom_headers).value!
      response.body unless response.nil?
    end

    #
    # Deletes a release.
    #
    # @param release_id [String] The ID of the release
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param custom_headers [Hash{String => String}] A hash of custom headers that
    # will be added to the HTTP request.
    #
    # @return [MsRest::HttpOperationResponse] HTTP response information.
    #
    def delete_with_http_info(release_id, owner_name, app_name, custom_headers = nil)
      delete_async(release_id, owner_name, app_name, custom_headers).value!
    end

    #
    # Deletes a release.
    #
    # @param release_id [String] The ID of the release
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param [Hash{String => String}] A hash of custom headers that will be added
    # to the HTTP request.
    #
    # @return [Concurrent::Promise] Promise object which holds the HTTP response.
    #
    def delete_async(release_id, owner_name, app_name, custom_headers = nil)
      fail ArgumentError, 'release_id is nil' if release_id.nil?
      fail ArgumentError, 'owner_name is nil' if owner_name.nil?
      fail ArgumentError, 'app_name is nil' if app_name.nil?


      request_headers = {}
      path_template = 'v0.1/apps/{owner_name}/{app_name}/releases/{release_id}'

      request_url = @base_url || @client.base_url

      options = {
          middlewares: [[MsRest::RetryPolicyMiddleware, times: 3, retry: 0.02], [:cookie_jar]],
          path_params: {'release_id' => release_id,'owner_name' => owner_name,'app_name' => app_name},
          headers: request_headers.merge(custom_headers || {}),
          base_url: request_url
      }
      promise = @client.make_request_async(:delete, path_template, options)

      promise = promise.then do |result|
        http_response = result.response
        status_code = http_response.status
        response_content = http_response.body
        unless status_code == 200 || status_code == 404 || status_code == 500
          error_model = JSON.load(response_content)
          fail MsRest::HttpOperationError.new(result.request, http_response, error_model)
        end

        # Deserialize Response
        if status_code == 404
          begin
            parsed_response = response_content.to_s.empty? ? nil : JSON.load(response_content)
            result_mapper = MobileCenterApi::Models::ErrorDetails.mapper()
            result.body = @client.deserialize(result_mapper, parsed_response, 'result.body')
          rescue Exception => e
            fail MsRest::DeserializationError.new('Error occurred in deserializing the response', e.message, e.backtrace, result)
          end
        end
        # Deserialize Response
        if status_code == 500
          begin
            parsed_response = response_content.to_s.empty? ? nil : JSON.load(response_content)
            result_mapper = MobileCenterApi::Models::ErrorDetails.mapper()
            result.body = @client.deserialize(result_mapper, parsed_response, 'result.body')
          rescue Exception => e
            fail MsRest::DeserializationError.new('Error occurred in deserializing the response', e.message, e.backtrace, result)
          end
        end

        result
      end

      promise.execute
    end

    #
    # Return detailed information about releases.
    #
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param published_only [Boolean] when true, filters out releases that were
    # uplaoded but were never distributed. Releases that under deleted distribution
    # groups will not be filtered out.
    # @param custom_headers [Hash{String => String}] A hash of custom headers that
    # will be added to the HTTP request.
    #
    # @return [Array] operation results.
    #
    def list(owner_name, app_name, published_only = nil, custom_headers = nil)
      response = list_async(owner_name, app_name, published_only, custom_headers).value!
      response.body unless response.nil?
    end

    #
    # Return detailed information about releases.
    #
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param published_only [Boolean] when true, filters out releases that were
    # uplaoded but were never distributed. Releases that under deleted distribution
    # groups will not be filtered out.
    # @param custom_headers [Hash{String => String}] A hash of custom headers that
    # will be added to the HTTP request.
    #
    # @return [MsRest::HttpOperationResponse] HTTP response information.
    #
    def list_with_http_info(owner_name, app_name, published_only = nil, custom_headers = nil)
      list_async(owner_name, app_name, published_only, custom_headers).value!
    end

    #
    # Return detailed information about releases.
    #
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param published_only [Boolean] when true, filters out releases that were
    # uplaoded but were never distributed. Releases that under deleted distribution
    # groups will not be filtered out.
    # @param [Hash{String => String}] A hash of custom headers that will be added
    # to the HTTP request.
    #
    # @return [Concurrent::Promise] Promise object which holds the HTTP response.
    #
    def list_async(owner_name, app_name, published_only = nil, custom_headers = nil)
      fail ArgumentError, 'owner_name is nil' if owner_name.nil?
      fail ArgumentError, 'app_name is nil' if app_name.nil?


      request_headers = {}
      path_template = 'v0.1/apps/{owner_name}/{app_name}/releases'

      request_url = @base_url || @client.base_url

      options = {
          middlewares: [[MsRest::RetryPolicyMiddleware, times: 3, retry: 0.02], [:cookie_jar]],
          path_params: {'owner_name' => owner_name,'app_name' => app_name},
          query_params: {'published_only' => published_only},
          headers: request_headers.merge(custom_headers || {}),
          base_url: request_url
      }
      promise = @client.make_request_async(:get, path_template, options)

      promise = promise.then do |result|
        http_response = result.response
        status_code = http_response.status
        response_content = http_response.body
        unless status_code == 200
          error_model = JSON.load(response_content)
          fail MsRest::HttpOperationError.new(result.request, http_response, error_model)
        end

        # Deserialize Response
        if status_code == 200
          begin
            parsed_response = response_content.to_s.empty? ? nil : JSON.load(response_content)
            result_mapper = {
              required: false,
              serialized_name: 'parsed_response',
              type: {
                name: 'Sequence',
                element: {
                    required: false,
                    serialized_name: 'BasicReleaseDetailsElementType',
                    type: {
                      name: 'Composite',
                      class_name: 'BasicReleaseDetails'
                    }
                }
              }
            }
            result.body = @client.deserialize(result_mapper, parsed_response, 'result.body')
          rescue Exception => e
            fail MsRest::DeserializationError.new('Error occurred in deserializing the response', e.message, e.backtrace, result)
          end
        end

        result
      end

      promise.execute
    end

    #
    # Get the latest release from every distribution group associated with an
    # application.
    #
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param custom_headers [Hash{String => String}] A hash of custom headers that
    # will be added to the HTTP request.
    #
    # @return [Array] operation results.
    #
    def list_latest(owner_name, app_name, custom_headers = nil)
      response = list_latest_async(owner_name, app_name, custom_headers).value!
      response.body unless response.nil?
    end

    #
    # Get the latest release from every distribution group associated with an
    # application.
    #
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param custom_headers [Hash{String => String}] A hash of custom headers that
    # will be added to the HTTP request.
    #
    # @return [MsRest::HttpOperationResponse] HTTP response information.
    #
    def list_latest_with_http_info(owner_name, app_name, custom_headers = nil)
      list_latest_async(owner_name, app_name, custom_headers).value!
    end

    #
    # Get the latest release from every distribution group associated with an
    # application.
    #
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param [Hash{String => String}] A hash of custom headers that will be added
    # to the HTTP request.
    #
    # @return [Concurrent::Promise] Promise object which holds the HTTP response.
    #
    def list_latest_async(owner_name, app_name, custom_headers = nil)
      fail ArgumentError, 'owner_name is nil' if owner_name.nil?
      fail ArgumentError, 'app_name is nil' if app_name.nil?


      request_headers = {}
      path_template = 'v0.1/apps/{owner_name}/{app_name}/recent_releases'

      request_url = @base_url || @client.base_url

      options = {
          middlewares: [[MsRest::RetryPolicyMiddleware, times: 3, retry: 0.02], [:cookie_jar]],
          path_params: {'owner_name' => owner_name,'app_name' => app_name},
          headers: request_headers.merge(custom_headers || {}),
          base_url: request_url
      }
      promise = @client.make_request_async(:get, path_template, options)

      promise = promise.then do |result|
        http_response = result.response
        status_code = http_response.status
        response_content = http_response.body
        unless status_code == 200
          error_model = JSON.load(response_content)
          fail MsRest::HttpOperationError.new(result.request, http_response, error_model)
        end

        # Deserialize Response
        if status_code == 200
          begin
            parsed_response = response_content.to_s.empty? ? nil : JSON.load(response_content)
            result_mapper = {
              required: false,
              serialized_name: 'parsed_response',
              type: {
                name: 'Sequence',
                element: {
                    required: false,
                    serialized_name: 'BasicReleaseDetailsElementType',
                    type: {
                      name: 'Composite',
                      class_name: 'BasicReleaseDetails'
                    }
                }
              }
            }
            result.body = @client.deserialize(result_mapper, parsed_response, 'result.body')
          rescue Exception => e
            fail MsRest::DeserializationError.new('Error occurred in deserializing the response', e.message, e.backtrace, result)
          end
        end

        result
      end

      promise.execute
    end

    #
    # Return detailed information about a distributed release in a given
    # distribution group.
    #
    # @param distribution_group_name [String] The name of the distribution group.
    # @param release_id [String] Must be `latest`, a specific `release_id` is not
    # supported at this time.
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param custom_headers [Hash{String => String}] A hash of custom headers that
    # will be added to the HTTP request.
    #
    # @return [Object] operation results.
    #
    def get_latest_by_distribution_group(distribution_group_name, release_id, owner_name, app_name, custom_headers = nil)
      response = get_latest_by_distribution_group_async(distribution_group_name, release_id, owner_name, app_name, custom_headers).value!
      response.body unless response.nil?
    end

    #
    # Return detailed information about a distributed release in a given
    # distribution group.
    #
    # @param distribution_group_name [String] The name of the distribution group.
    # @param release_id [String] Must be `latest`, a specific `release_id` is not
    # supported at this time.
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param custom_headers [Hash{String => String}] A hash of custom headers that
    # will be added to the HTTP request.
    #
    # @return [MsRest::HttpOperationResponse] HTTP response information.
    #
    def get_latest_by_distribution_group_with_http_info(distribution_group_name, release_id, owner_name, app_name, custom_headers = nil)
      get_latest_by_distribution_group_async(distribution_group_name, release_id, owner_name, app_name, custom_headers).value!
    end

    #
    # Return detailed information about a distributed release in a given
    # distribution group.
    #
    # @param distribution_group_name [String] The name of the distribution group.
    # @param release_id [String] Must be `latest`, a specific `release_id` is not
    # supported at this time.
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param [Hash{String => String}] A hash of custom headers that will be added
    # to the HTTP request.
    #
    # @return [Concurrent::Promise] Promise object which holds the HTTP response.
    #
    def get_latest_by_distribution_group_async(distribution_group_name, release_id, owner_name, app_name, custom_headers = nil)
      fail ArgumentError, 'distribution_group_name is nil' if distribution_group_name.nil?
      fail ArgumentError, 'release_id is nil' if release_id.nil?
      fail ArgumentError, 'owner_name is nil' if owner_name.nil?
      fail ArgumentError, 'app_name is nil' if app_name.nil?


      request_headers = {}
      path_template = 'v0.1/apps/{owner_name}/{app_name}/distribution_groups/{distribution_group_name}/releases/{release_id}'

      request_url = @base_url || @client.base_url

      options = {
          middlewares: [[MsRest::RetryPolicyMiddleware, times: 3, retry: 0.02], [:cookie_jar]],
          path_params: {'distribution_group_name' => distribution_group_name,'release_id' => release_id,'owner_name' => owner_name,'app_name' => app_name},
          headers: request_headers.merge(custom_headers || {}),
          base_url: request_url
      }
      promise = @client.make_request_async(:get, path_template, options)

      promise = promise.then do |result|
        http_response = result.response
        status_code = http_response.status
        response_content = http_response.body
        unless status_code == 200 || status_code == 404 || status_code == 501
          error_model = JSON.load(response_content)
          fail MsRest::HttpOperationError.new(result.request, http_response, error_model)
        end

        # Deserialize Response
        if status_code == 200
          begin
            parsed_response = response_content.to_s.empty? ? nil : JSON.load(response_content)
            result_mapper = MobileCenterApi::Models::ReleaseDetails.mapper()
            result.body = @client.deserialize(result_mapper, parsed_response, 'result.body')
          rescue Exception => e
            fail MsRest::DeserializationError.new('Error occurred in deserializing the response', e.message, e.backtrace, result)
          end
        end
        # Deserialize Response
        if status_code == 404
          begin
            parsed_response = response_content.to_s.empty? ? nil : JSON.load(response_content)
            result_mapper = MobileCenterApi::Models::ErrorDetails.mapper()
            result.body = @client.deserialize(result_mapper, parsed_response, 'result.body')
          rescue Exception => e
            fail MsRest::DeserializationError.new('Error occurred in deserializing the response', e.message, e.backtrace, result)
          end
        end
        # Deserialize Response
        if status_code == 501
          begin
            parsed_response = response_content.to_s.empty? ? nil : JSON.load(response_content)
            result_mapper = MobileCenterApi::Models::ErrorDetails.mapper()
            result.body = @client.deserialize(result_mapper, parsed_response, 'result.body')
          rescue Exception => e
            fail MsRest::DeserializationError.new('Error occurred in deserializing the response', e.message, e.backtrace, result)
          end
        end

        result
      end

      promise.execute
    end

    #
    # Deletes a release with id 'release_id' in a given distribution group.
    #
    # @param distribution_group_name [String] The name of the distribution group.
    # @param release_id [String] The ID identifying the unique release.
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param custom_headers [Hash{String => String}] A hash of custom headers that
    # will be added to the HTTP request.
    #
    # @return [Object] operation results.
    #
    def delete_with_distribution_group_id(distribution_group_name, release_id, owner_name, app_name, custom_headers = nil)
      response = delete_with_distribution_group_id_async(distribution_group_name, release_id, owner_name, app_name, custom_headers).value!
      response.body unless response.nil?
    end

    #
    # Deletes a release with id 'release_id' in a given distribution group.
    #
    # @param distribution_group_name [String] The name of the distribution group.
    # @param release_id [String] The ID identifying the unique release.
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param custom_headers [Hash{String => String}] A hash of custom headers that
    # will be added to the HTTP request.
    #
    # @return [MsRest::HttpOperationResponse] HTTP response information.
    #
    def delete_with_distribution_group_id_with_http_info(distribution_group_name, release_id, owner_name, app_name, custom_headers = nil)
      delete_with_distribution_group_id_async(distribution_group_name, release_id, owner_name, app_name, custom_headers).value!
    end

    #
    # Deletes a release with id 'release_id' in a given distribution group.
    #
    # @param distribution_group_name [String] The name of the distribution group.
    # @param release_id [String] The ID identifying the unique release.
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param [Hash{String => String}] A hash of custom headers that will be added
    # to the HTTP request.
    #
    # @return [Concurrent::Promise] Promise object which holds the HTTP response.
    #
    def delete_with_distribution_group_id_async(distribution_group_name, release_id, owner_name, app_name, custom_headers = nil)
      fail ArgumentError, 'distribution_group_name is nil' if distribution_group_name.nil?
      fail ArgumentError, 'release_id is nil' if release_id.nil?
      fail ArgumentError, 'owner_name is nil' if owner_name.nil?
      fail ArgumentError, 'app_name is nil' if app_name.nil?


      request_headers = {}
      path_template = 'v0.1/apps/{owner_name}/{app_name}/distribution_groups/{distribution_group_name}/releases/{release_id}'

      request_url = @base_url || @client.base_url

      options = {
          middlewares: [[MsRest::RetryPolicyMiddleware, times: 3, retry: 0.02], [:cookie_jar]],
          path_params: {'distribution_group_name' => distribution_group_name,'release_id' => release_id,'owner_name' => owner_name,'app_name' => app_name},
          headers: request_headers.merge(custom_headers || {}),
          base_url: request_url
      }
      promise = @client.make_request_async(:delete, path_template, options)

      promise = promise.then do |result|
        http_response = result.response
        status_code = http_response.status
        response_content = http_response.body
        unless status_code == 200 || status_code == 403 || status_code == 404
          error_model = JSON.load(response_content)
          fail MsRest::HttpOperationError.new(result.request, http_response, error_model)
        end

        # Deserialize Response
        if status_code == 403
          begin
            parsed_response = response_content.to_s.empty? ? nil : JSON.load(response_content)
            result_mapper = MobileCenterApi::Models::ErrorDetails.mapper()
            result.body = @client.deserialize(result_mapper, parsed_response, 'result.body')
          rescue Exception => e
            fail MsRest::DeserializationError.new('Error occurred in deserializing the response', e.message, e.backtrace, result)
          end
        end
        # Deserialize Response
        if status_code == 404
          begin
            parsed_response = response_content.to_s.empty? ? nil : JSON.load(response_content)
            result_mapper = MobileCenterApi::Models::ErrorDetails.mapper()
            result.body = @client.deserialize(result_mapper, parsed_response, 'result.body')
          rescue Exception => e
            fail MsRest::DeserializationError.new('Error occurred in deserializing the response', e.message, e.backtrace, result)
          end
        end

        result
      end

      promise.execute
    end

    #
    # Return detailed information about distributed releases in a given
    # distribution group.
    #
    # @param distribution_group_name [String] The name of the distribution group.
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param custom_headers [Hash{String => String}] A hash of custom headers that
    # will be added to the HTTP request.
    #
    # @return [Object] operation results.
    #
    def list_by_distribution_group(distribution_group_name, owner_name, app_name, custom_headers = nil)
      response = list_by_distribution_group_async(distribution_group_name, owner_name, app_name, custom_headers).value!
      response.body unless response.nil?
    end

    #
    # Return detailed information about distributed releases in a given
    # distribution group.
    #
    # @param distribution_group_name [String] The name of the distribution group.
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param custom_headers [Hash{String => String}] A hash of custom headers that
    # will be added to the HTTP request.
    #
    # @return [MsRest::HttpOperationResponse] HTTP response information.
    #
    def list_by_distribution_group_with_http_info(distribution_group_name, owner_name, app_name, custom_headers = nil)
      list_by_distribution_group_async(distribution_group_name, owner_name, app_name, custom_headers).value!
    end

    #
    # Return detailed information about distributed releases in a given
    # distribution group.
    #
    # @param distribution_group_name [String] The name of the distribution group.
    # @param owner_name [String] The name of the owner
    # @param app_name [String] The name of the application
    # @param [Hash{String => String}] A hash of custom headers that will be added
    # to the HTTP request.
    #
    # @return [Concurrent::Promise] Promise object which holds the HTTP response.
    #
    def list_by_distribution_group_async(distribution_group_name, owner_name, app_name, custom_headers = nil)
      fail ArgumentError, 'distribution_group_name is nil' if distribution_group_name.nil?
      fail ArgumentError, 'owner_name is nil' if owner_name.nil?
      fail ArgumentError, 'app_name is nil' if app_name.nil?


      request_headers = {}
      path_template = 'v0.1/apps/{owner_name}/{app_name}/distribution_groups/{distribution_group_name}/releases'

      request_url = @base_url || @client.base_url

      options = {
          middlewares: [[MsRest::RetryPolicyMiddleware, times: 3, retry: 0.02], [:cookie_jar]],
          path_params: {'distribution_group_name' => distribution_group_name,'owner_name' => owner_name,'app_name' => app_name},
          headers: request_headers.merge(custom_headers || {}),
          base_url: request_url
      }
      promise = @client.make_request_async(:get, path_template, options)

      promise = promise.then do |result|
        http_response = result.response
        status_code = http_response.status
        response_content = http_response.body
        unless status_code == 200 || status_code == 404
          error_model = JSON.load(response_content)
          fail MsRest::HttpOperationError.new(result.request, http_response, error_model)
        end

        # Deserialize Response
        if status_code == 200
          begin
            parsed_response = response_content.to_s.empty? ? nil : JSON.load(response_content)
            result_mapper = {
              required: false,
              serialized_name: 'parsed_response',
              type: {
                name: 'Sequence',
                element: {
                    required: false,
                    serialized_name: 'BasicReleaseDetailsElementType',
                    type: {
                      name: 'Composite',
                      class_name: 'BasicReleaseDetails'
                    }
                }
              }
            }
            result.body = @client.deserialize(result_mapper, parsed_response, 'result.body')
          rescue Exception => e
            fail MsRest::DeserializationError.new('Error occurred in deserializing the response', e.message, e.backtrace, result)
          end
        end
        # Deserialize Response
        if status_code == 404
          begin
            parsed_response = response_content.to_s.empty? ? nil : JSON.load(response_content)
            result_mapper = MobileCenterApi::Models::ErrorDetails.mapper()
            result.body = @client.deserialize(result_mapper, parsed_response, 'result.body')
          rescue Exception => e
            fail MsRest::DeserializationError.new('Error occurred in deserializing the response', e.message, e.backtrace, result)
          end
        end

        result
      end

      promise.execute
    end

  end
end
