# Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.

# typed: true
# frozen_string_literal: true

require 'faraday'
require 'faraday/multipart'
require 'sorbet-runtime'

module OpenApiSDK
  extend T::Sig
  class Updater
    extend T::Sig
    # This describes the API for searching and applying updates to the Plex Media Server.
    # Updates to the status can be observed via the Event API.
    # 

    sig { params(sdk_config: SDKConfiguration).void }
    def initialize(sdk_config)
      @sdk_configuration = sdk_config
    end


    sig { returns(::OpenApiSDK::Operations::GetUpdateStatusResponse) }
    def get_update_status
      # get_update_status - Querying status of updates
      # Querying status of updates
      url, params = @sdk_configuration.get_server_details
      base_url = Utils.template_url(url, params)
      url = "#{base_url}/updater/status"
      headers = {}
      headers['Accept'] = 'application/json'
      headers['user-agent'] = @sdk_configuration.user_agent

      r = @sdk_configuration.client.get(url) do |req|
        req.headers = headers
        Utils.configure_request_security(req, @sdk_configuration.security) if !@sdk_configuration.nil? && !@sdk_configuration.security.nil?
      end

      content_type = r.headers.fetch('Content-Type', 'application/octet-stream')

      res = ::OpenApiSDK::Operations::GetUpdateStatusResponse.new(
        status_code: r.status, content_type: content_type, raw_response: r
      )
      if r.status == 200
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::OpenApiSDK::Operations::GetUpdateStatusResponseBody)
          res.two_hundred_application_json_object = out
        end
      elsif r.status == 400
      elsif r.status == 401
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::OpenApiSDK::Operations::GetUpdateStatusUpdaterResponseBody)
          res.four_hundred_and_one_application_json_object = out
        end
      end
      res
    end


    sig { params(download: T.nilable(::OpenApiSDK::Operations::Download)).returns(::OpenApiSDK::Operations::CheckForUpdatesResponse) }
    def check_for_updates(download = nil)
      # check_for_updates - Checking for updates
      # Checking for updates
      request = ::OpenApiSDK::Operations::CheckForUpdatesRequest.new(
        
        download: download
      )
      url, params = @sdk_configuration.get_server_details
      base_url = Utils.template_url(url, params)
      url = "#{base_url}/updater/check"
      headers = {}
      query_params = Utils.get_query_params(::OpenApiSDK::Operations::CheckForUpdatesRequest, request)
      headers['Accept'] = 'application/json'
      headers['user-agent'] = @sdk_configuration.user_agent

      r = @sdk_configuration.client.put(url) do |req|
        req.headers = headers
        req.params = query_params
        Utils.configure_request_security(req, @sdk_configuration.security) if !@sdk_configuration.nil? && !@sdk_configuration.security.nil?
      end

      content_type = r.headers.fetch('Content-Type', 'application/octet-stream')

      res = ::OpenApiSDK::Operations::CheckForUpdatesResponse.new(
        status_code: r.status, content_type: content_type, raw_response: r
      )
      if [200, 400].include?(r.status)
      elsif r.status == 401
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::OpenApiSDK::Operations::CheckForUpdatesResponseBody)
          res.object = out
        end
      end
      res
    end


    sig { params(tonight: T.nilable(::OpenApiSDK::Operations::Tonight), skip: T.nilable(::OpenApiSDK::Operations::Skip)).returns(::OpenApiSDK::Operations::ApplyUpdatesResponse) }
    def apply_updates(tonight = nil, skip = nil)
      # apply_updates - Apply Updates
      # Note that these two parameters are effectively mutually exclusive. The `tonight` parameter takes precedence and `skip` will be ignored if `tonight` is also passed
      # 
      request = ::OpenApiSDK::Operations::ApplyUpdatesRequest.new(
        
        tonight: tonight,
        skip: skip
      )
      url, params = @sdk_configuration.get_server_details
      base_url = Utils.template_url(url, params)
      url = "#{base_url}/updater/apply"
      headers = {}
      query_params = Utils.get_query_params(::OpenApiSDK::Operations::ApplyUpdatesRequest, request)
      headers['Accept'] = 'application/json'
      headers['user-agent'] = @sdk_configuration.user_agent

      r = @sdk_configuration.client.put(url) do |req|
        req.headers = headers
        req.params = query_params
        Utils.configure_request_security(req, @sdk_configuration.security) if !@sdk_configuration.nil? && !@sdk_configuration.security.nil?
      end

      content_type = r.headers.fetch('Content-Type', 'application/octet-stream')

      res = ::OpenApiSDK::Operations::ApplyUpdatesResponse.new(
        status_code: r.status, content_type: content_type, raw_response: r
      )
      if [200, 400, 500].include?(r.status)
      elsif r.status == 401
        if Utils.match_content_type(content_type, 'application/json')
          out = Utils.unmarshal_complex(r.env.response_body, ::OpenApiSDK::Operations::ApplyUpdatesResponseBody)
          res.object = out
        end
      end
      res
    end
  end
end
