class OpenApiSDK::Files

def create(request:, timeout_ms: nil, http_headers: nil)

def create(request:, timeout_ms: nil, http_headers: nil)
  # create - Create File
  # Create a file.
  #
  # **Scopes**: `files:write`
  url, params = @sdk_configuration.get_server_details
  base_url = Utils.template_url(url, params)
  url = "#{base_url}/v1/files/"
  headers = {}
  headers = T.cast(headers, T::Hash[String, String])
  req_content_type, data, form = Utils.serialize_request_body(request, false, false, :request, :json)
  headers["content-type"] = req_content_type
  raise StandardError, "request body is required" if data.nil? && form.nil?
  if form && !form.empty?
    body = Utils.encode_form(form)
  elsif Utils.match_content_type(req_content_type, "application/x-www-form-urlencoded")
    body = URI.encode_www_form(T.cast(data, T::Hash[Symbol, Object]))
  else
    body = data
  end
  headers["Accept"] = "application/json"
  headers["user-agent"] = @sdk_configuration.user_agent
  security = @sdk_configuration.security_source&.call
  timeout = (timeout_ms.to_f / 1000) unless timeout_ms.nil?
  timeout ||= @sdk_configuration.timeout
  connection = @sdk_configuration.client
  hook_ctx = SDKHooks::HookContext.new(
    config: @sdk_configuration,
    base_url: base_url,
    oauth2_scopes: nil,
    operation_id: "files:create",
    security_source: @sdk_configuration.security_source
  )
  error = T.let(nil, T.nilable(StandardError))
  http_response = T.let(nil, T.nilable(Faraday::Response))
  begin
    http_response = T.must(connection).post(url) do |req|
      req.body = body
      req.headers.merge!(headers)
      req.options.timeout = timeout unless timeout.nil?
      Utils.configure_request_security(req, security)
      http_headers&.each do |key, value|
        req.headers[key.to_s] = value
      end
      @sdk_configuration.hooks.before_request(
        hook_ctx: SDKHooks::BeforeRequestHookContext.new(
          hook_ctx: hook_ctx
        ),
        request: req
      )
    end
  rescue StandardError => e
    error = e
  ensure
    if http_response.nil? || Utils.error_status?(http_response.status)
      http_response = @sdk_configuration.hooks.after_error(
        error: error,
        hook_ctx: SDKHooks::AfterErrorHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
    else
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
    end
    if http_response.nil?
      raise error if !error.nil?
      raise "no response"
    end
  end
  content_type = http_response.headers.fetch("Content-Type", "application/octet-stream")
  if Utils.match_status_code(http_response.status, ["201"])
    if Utils.match_content_type(content_type, "application/json")
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
      response_data = http_response.env.response_body
      obj = Crystalline.unmarshal_json(JSON.parse(response_data), Models::Components::FileUpload)
      response = Models::Operations::FilesCreateResponse.new(
        status_code: http_response.status,
        content_type: content_type,
        raw_response: http_response,
        file_upload: T.unsafe(obj)
      )
      return response
    else
      raise(
        ::OpenApiSDK::Models::Errors::APIError.new(
          status_code: http_response.status,
          body: http_response.env.response_body,
          raw_response: http_response
        ),
        "Unknown content type received"
      )
    end
  elsif Utils.match_status_code(http_response.status, ["422"])
    if Utils.match_content_type(content_type, "application/json")
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
      response_data = http_response.env.response_body
      obj = Crystalline.unmarshal_json(JSON.parse(response_data), Models::Errors::HTTPValidationError)
      raise obj
    else
      raise(
        ::OpenApiSDK::Models::Errors::APIError.new(
          status_code: http_response.status,
          body: http_response.env.response_body,
          raw_response: http_response
        ),
        "Unknown content type received"
      )
    end
  elsif Utils.match_status_code(http_response.status, ["4XX"])
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "API error occurred"
    )
  elsif Utils.match_status_code(http_response.status, ["5XX"])
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "API error occurred"
    )
  else
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "Unknown status code received"
    )
  end
end

def delete(id:, timeout_ms: nil, http_headers: nil)

def delete(id:, timeout_ms: nil, http_headers: nil)
  # delete - Delete File
  # Delete a file.
  #
  # **Scopes**: `files:write`
  request = Models::Operations::FilesDeleteRequest.new(
    id: id
  )
  url, params = @sdk_configuration.get_server_details
  base_url = Utils.template_url(url, params)
  url = Utils.generate_url(
    Models::Operations::FilesDeleteRequest,
    base_url,
    "/v1/files/{id}",
    request
  )
  headers = {}
  headers = T.cast(headers, T::Hash[String, String])
  headers["Accept"] = "application/json"
  headers["user-agent"] = @sdk_configuration.user_agent
  security = @sdk_configuration.security_source&.call
  timeout = (timeout_ms.to_f / 1000) unless timeout_ms.nil?
  timeout ||= @sdk_configuration.timeout
  connection = @sdk_configuration.client
  hook_ctx = SDKHooks::HookContext.new(
    config: @sdk_configuration,
    base_url: base_url,
    oauth2_scopes: nil,
    operation_id: "files:delete",
    security_source: @sdk_configuration.security_source
  )
  error = T.let(nil, T.nilable(StandardError))
  http_response = T.let(nil, T.nilable(Faraday::Response))
  begin
    http_response = T.must(connection).delete(url) do |req|
      req.headers.merge!(headers)
      req.options.timeout = timeout unless timeout.nil?
      Utils.configure_request_security(req, security)
      http_headers&.each do |key, value|
        req.headers[key.to_s] = value
      end
      @sdk_configuration.hooks.before_request(
        hook_ctx: SDKHooks::BeforeRequestHookContext.new(
          hook_ctx: hook_ctx
        ),
        request: req
      )
    end
  rescue StandardError => e
    error = e
  ensure
    if http_response.nil? || Utils.error_status?(http_response.status)
      http_response = @sdk_configuration.hooks.after_error(
        error: error,
        hook_ctx: SDKHooks::AfterErrorHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
    else
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
    end
    if http_response.nil?
      raise error if !error.nil?
      raise "no response"
    end
  end
  content_type = http_response.headers.fetch("Content-Type", "application/octet-stream")
  if Utils.match_status_code(http_response.status, ["204"])
    http_response = @sdk_configuration.hooks.after_success(
      hook_ctx: SDKHooks::AfterSuccessHookContext.new(
        hook_ctx: hook_ctx
      ),
      response: http_response
    )
    return Models::Operations::FilesDeleteResponse.new(
      status_code: http_response.status,
      content_type: content_type,
      raw_response: http_response
    )
  elsif Utils.match_status_code(http_response.status, ["403"])
    if Utils.match_content_type(content_type, "application/json")
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
      response_data = http_response.env.response_body
      obj = Crystalline.unmarshal_json(JSON.parse(response_data), Models::Errors::NotPermitted)
      raise obj
    else
      raise(
        ::OpenApiSDK::Models::Errors::APIError.new(
          status_code: http_response.status,
          body: http_response.env.response_body,
          raw_response: http_response
        ),
        "Unknown content type received"
      )
    end
  elsif Utils.match_status_code(http_response.status, ["404"])
    if Utils.match_content_type(content_type, "application/json")
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
      response_data = http_response.env.response_body
      obj = Crystalline.unmarshal_json(JSON.parse(response_data), Models::Errors::ResourceNotFound)
      raise obj
    else
      raise(
        ::OpenApiSDK::Models::Errors::APIError.new(
          status_code: http_response.status,
          body: http_response.env.response_body,
          raw_response: http_response
        ),
        "Unknown content type received"
      )
    end
  elsif Utils.match_status_code(http_response.status, ["422"])
    if Utils.match_content_type(content_type, "application/json")
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
      response_data = http_response.env.response_body
      obj = Crystalline.unmarshal_json(JSON.parse(response_data), Models::Errors::HTTPValidationError)
      raise obj
    else
      raise(
        ::OpenApiSDK::Models::Errors::APIError.new(
          status_code: http_response.status,
          body: http_response.env.response_body,
          raw_response: http_response
        ),
        "Unknown content type received"
      )
    end
  elsif Utils.match_status_code(http_response.status, ["4XX"])
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "API error occurred"
    )
  elsif Utils.match_status_code(http_response.status, ["5XX"])
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "API error occurred"
    )
  else
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "Unknown status code received"
    )
  end
end

def get_url(base_url:, url_variables: nil)

def get_url(base_url:, url_variables: nil)
  sd_base_url, sd_options = @sdk_configuration.get_server_details
  if base_url.nil?
    base_url = sd_base_url
  end
  if url_variables.nil?
    url_variables = sd_options
  end
  return Utils.template_url(base_url, url_variables)
end

def initialize(sdk_config)

def initialize(sdk_config)
  @sdk_configuration = sdk_config
end

def list(organization_id: nil, ids: nil, page: nil, limit: nil, timeout_ms: nil, http_headers: nil)

def list(organization_id: nil, ids: nil, page: nil, limit: nil, timeout_ms: nil, http_headers: nil)
  # list - List Files
  # List files.
  #
  # **Scopes**: `files:read` `files:write`
  request = Models::Operations::FilesListRequest.new(
    organization_id: organization_id,
    ids: ids,
    page: page,
    limit: limit
  )
  url, params = @sdk_configuration.get_server_details
  base_url = Utils.template_url(url, params)
  url = "#{base_url}/v1/files/"
  headers = {}
  headers = T.cast(headers, T::Hash[String, String])
  query_params = Utils.get_query_params(Models::Operations::FilesListRequest, request, nil)
  headers["Accept"] = "application/json"
  headers["user-agent"] = @sdk_configuration.user_agent
  security = @sdk_configuration.security_source&.call
  timeout = (timeout_ms.to_f / 1000) unless timeout_ms.nil?
  timeout ||= @sdk_configuration.timeout
  connection = @sdk_configuration.client
  hook_ctx = SDKHooks::HookContext.new(
    config: @sdk_configuration,
    base_url: base_url,
    oauth2_scopes: nil,
    operation_id: "files:list",
    security_source: @sdk_configuration.security_source
  )
  error = T.let(nil, T.nilable(StandardError))
  http_response = T.let(nil, T.nilable(Faraday::Response))
  begin
    http_response = T.must(connection).get(url) do |req|
      req.headers.merge!(headers)
      req.options.timeout = timeout unless timeout.nil?
      req.params = query_params
      Utils.configure_request_security(req, security)
      http_headers&.each do |key, value|
        req.headers[key.to_s] = value
      end
      @sdk_configuration.hooks.before_request(
        hook_ctx: SDKHooks::BeforeRequestHookContext.new(
          hook_ctx: hook_ctx
        ),
        request: req
      )
    end
  rescue StandardError => e
    error = e
  ensure
    if http_response.nil? || Utils.error_status?(http_response.status)
      http_response = @sdk_configuration.hooks.after_error(
        error: error,
        hook_ctx: SDKHooks::AfterErrorHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
    else
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
    end
    if http_response.nil?
      raise error if !error.nil?
      raise "no response"
    end
  end
  content_type = http_response.headers.fetch("Content-Type", "application/octet-stream")
  if Utils.match_status_code(http_response.status, ["200"])
    if Utils.match_content_type(content_type, "application/json")
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
      response_data = http_response.env.response_body
      obj = Crystalline.unmarshal_json(JSON.parse(response_data), Models::Components::ListResourceFileRead)
      response = Models::Operations::FilesListResponse.new(
        status_code: http_response.status,
        content_type: content_type,
        raw_response: http_response,
        list_resource_file_read: T.unsafe(obj)
      )
      sdk = self
      response.next_page = proc do
        request_page = T.must(!request.page.nil? ? request.page : 1)
        next_page = request_page + 1
        num_pages = Janeway.enum_for("$.pagination.max_page", JSON.parse(response_data)).search
        if num_pages.nil? || num_pages[0] <= request_page
          next nil
        end
        if !response_data
          next nil
        end
        results = Janeway.enum_for("$.items", JSON.parse(response_data)).search
        if results.is_a?(Array)
          results = results[0]
        end
        if results.count.zero?
          next nil
        end
        request_limit = !request.limit.nil? ? request.limit : 1
        if results.count < request_limit
          next nil
        end
        sdk.list(
          organization_id: request.organization_id,
          ids: request.ids,
          page: next_page,
          limit: request.limit,
          http_headers: http_headers
        )
      end
      return response
    else
      raise(
        ::OpenApiSDK::Models::Errors::APIError.new(
          status_code: http_response.status,
          body: http_response.env.response_body,
          raw_response: http_response
        ),
        "Unknown content type received"
      )
    end
  elsif Utils.match_status_code(http_response.status, ["422"])
    if Utils.match_content_type(content_type, "application/json")
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
      response_data = http_response.env.response_body
      obj = Crystalline.unmarshal_json(JSON.parse(response_data), Models::Errors::HTTPValidationError)
      raise obj
    else
      raise(
        ::OpenApiSDK::Models::Errors::APIError.new(
          status_code: http_response.status,
          body: http_response.env.response_body,
          raw_response: http_response
        ),
        "Unknown content type received"
      )
    end
  elsif Utils.match_status_code(http_response.status, ["4XX"])
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "API error occurred"
    )
  elsif Utils.match_status_code(http_response.status, ["5XX"])
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "API error occurred"
    )
  else
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "Unknown status code received"
    )
  end
end

def update(body:, id:, timeout_ms: nil, http_headers: nil)

def update(body:, id:, timeout_ms: nil, http_headers: nil)
  # update - Update File
  # Update a file.
  #
  # **Scopes**: `files:write`
  request = Models::Operations::FilesUpdateRequest.new(
    id: id,
    body: body
  )
  url, params = @sdk_configuration.get_server_details
  base_url = Utils.template_url(url, params)
  url = Utils.generate_url(
    Models::Operations::FilesUpdateRequest,
    base_url,
    "/v1/files/{id}",
    request
  )
  headers = {}
  headers = T.cast(headers, T::Hash[String, String])
  req_content_type, data, form = Utils.serialize_request_body(request, false, false, :body, :json)
  headers["content-type"] = req_content_type
  raise StandardError, "request body is required" if data.nil? && form.nil?
  if form && !form.empty?
    body = Utils.encode_form(form)
  elsif Utils.match_content_type(req_content_type, "application/x-www-form-urlencoded")
    body = URI.encode_www_form(T.cast(data, T::Hash[Symbol, Object]))
  else
    body = data
  end
  headers["Accept"] = "application/json"
  headers["user-agent"] = @sdk_configuration.user_agent
  security = @sdk_configuration.security_source&.call
  timeout = (timeout_ms.to_f / 1000) unless timeout_ms.nil?
  timeout ||= @sdk_configuration.timeout
  connection = @sdk_configuration.client
  hook_ctx = SDKHooks::HookContext.new(
    config: @sdk_configuration,
    base_url: base_url,
    oauth2_scopes: nil,
    operation_id: "files:update",
    security_source: @sdk_configuration.security_source
  )
  error = T.let(nil, T.nilable(StandardError))
  http_response = T.let(nil, T.nilable(Faraday::Response))
  begin
    http_response = T.must(connection).patch(url) do |req|
      req.body = body
      req.headers.merge!(headers)
      req.options.timeout = timeout unless timeout.nil?
      Utils.configure_request_security(req, security)
      http_headers&.each do |key, value|
        req.headers[key.to_s] = value
      end
      @sdk_configuration.hooks.before_request(
        hook_ctx: SDKHooks::BeforeRequestHookContext.new(
          hook_ctx: hook_ctx
        ),
        request: req
      )
    end
  rescue StandardError => e
    error = e
  ensure
    if http_response.nil? || Utils.error_status?(http_response.status)
      http_response = @sdk_configuration.hooks.after_error(
        error: error,
        hook_ctx: SDKHooks::AfterErrorHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
    else
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
    end
    if http_response.nil?
      raise error if !error.nil?
      raise "no response"
    end
  end
  content_type = http_response.headers.fetch("Content-Type", "application/octet-stream")
  if Utils.match_status_code(http_response.status, ["200"])
    if Utils.match_content_type(content_type, "application/json")
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
      response_data = http_response.env.response_body
      obj = Crystalline.unmarshal_json(
        JSON.parse(response_data),
        Crystalline::DiscriminatedUnion.new(
          "service",
          {
            "downloadable" => Models::Components::DownloadableFileRead,
            "product_media" => Models::Components::ProductMediaFileRead,
            "organization_avatar" => Models::Components::OrganizationAvatarFileRead
          }
        )
      )
      response = Models::Operations::FilesUpdateResponse.new(
        status_code: http_response.status,
        content_type: content_type,
        raw_response: http_response,
        response_files_update: T.unsafe(obj)
      )
      return response
    else
      raise(
        ::OpenApiSDK::Models::Errors::APIError.new(
          status_code: http_response.status,
          body: http_response.env.response_body,
          raw_response: http_response
        ),
        "Unknown content type received"
      )
    end
  elsif Utils.match_status_code(http_response.status, ["403"])
    if Utils.match_content_type(content_type, "application/json")
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
      response_data = http_response.env.response_body
      obj = Crystalline.unmarshal_json(JSON.parse(response_data), Models::Errors::NotPermitted)
      raise obj
    else
      raise(
        ::OpenApiSDK::Models::Errors::APIError.new(
          status_code: http_response.status,
          body: http_response.env.response_body,
          raw_response: http_response
        ),
        "Unknown content type received"
      )
    end
  elsif Utils.match_status_code(http_response.status, ["404"])
    if Utils.match_content_type(content_type, "application/json")
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
      response_data = http_response.env.response_body
      obj = Crystalline.unmarshal_json(JSON.parse(response_data), Models::Errors::ResourceNotFound)
      raise obj
    else
      raise(
        ::OpenApiSDK::Models::Errors::APIError.new(
          status_code: http_response.status,
          body: http_response.env.response_body,
          raw_response: http_response
        ),
        "Unknown content type received"
      )
    end
  elsif Utils.match_status_code(http_response.status, ["422"])
    if Utils.match_content_type(content_type, "application/json")
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
      response_data = http_response.env.response_body
      obj = Crystalline.unmarshal_json(JSON.parse(response_data), Models::Errors::HTTPValidationError)
      raise obj
    else
      raise(
        ::OpenApiSDK::Models::Errors::APIError.new(
          status_code: http_response.status,
          body: http_response.env.response_body,
          raw_response: http_response
        ),
        "Unknown content type received"
      )
    end
  elsif Utils.match_status_code(http_response.status, ["4XX"])
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "API error occurred"
    )
  elsif Utils.match_status_code(http_response.status, ["5XX"])
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "API error occurred"
    )
  else
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "Unknown status code received"
    )
  end
end

def uploaded(body:, id:, timeout_ms: nil, http_headers: nil)

def uploaded(body:, id:, timeout_ms: nil, http_headers: nil)
  # uploaded - Complete File Upload
  # Complete a file upload.
  #
  # **Scopes**: `files:write`
  request = Models::Operations::FilesUploadedRequest.new(
    id: id,
    body: body
  )
  url, params = @sdk_configuration.get_server_details
  base_url = Utils.template_url(url, params)
  url = Utils.generate_url(
    Models::Operations::FilesUploadedRequest,
    base_url,
    "/v1/files/{id}/uploaded",
    request
  )
  headers = {}
  headers = T.cast(headers, T::Hash[String, String])
  req_content_type, data, form = Utils.serialize_request_body(request, false, false, :body, :json)
  headers["content-type"] = req_content_type
  raise StandardError, "request body is required" if data.nil? && form.nil?
  if form && !form.empty?
    body = Utils.encode_form(form)
  elsif Utils.match_content_type(req_content_type, "application/x-www-form-urlencoded")
    body = URI.encode_www_form(T.cast(data, T::Hash[Symbol, Object]))
  else
    body = data
  end
  headers["Accept"] = "application/json"
  headers["user-agent"] = @sdk_configuration.user_agent
  security = @sdk_configuration.security_source&.call
  timeout = (timeout_ms.to_f / 1000) unless timeout_ms.nil?
  timeout ||= @sdk_configuration.timeout
  connection = @sdk_configuration.client
  hook_ctx = SDKHooks::HookContext.new(
    config: @sdk_configuration,
    base_url: base_url,
    oauth2_scopes: nil,
    operation_id: "files:uploaded",
    security_source: @sdk_configuration.security_source
  )
  error = T.let(nil, T.nilable(StandardError))
  http_response = T.let(nil, T.nilable(Faraday::Response))
  begin
    http_response = T.must(connection).post(url) do |req|
      req.body = body
      req.headers.merge!(headers)
      req.options.timeout = timeout unless timeout.nil?
      Utils.configure_request_security(req, security)
      http_headers&.each do |key, value|
        req.headers[key.to_s] = value
      end
      @sdk_configuration.hooks.before_request(
        hook_ctx: SDKHooks::BeforeRequestHookContext.new(
          hook_ctx: hook_ctx
        ),
        request: req
      )
    end
  rescue StandardError => e
    error = e
  ensure
    if http_response.nil? || Utils.error_status?(http_response.status)
      http_response = @sdk_configuration.hooks.after_error(
        error: error,
        hook_ctx: SDKHooks::AfterErrorHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
    else
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
    end
    if http_response.nil?
      raise error if !error.nil?
      raise "no response"
    end
  end
  content_type = http_response.headers.fetch("Content-Type", "application/octet-stream")
  if Utils.match_status_code(http_response.status, ["200"])
    if Utils.match_content_type(content_type, "application/json")
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
      response_data = http_response.env.response_body
      obj = Crystalline.unmarshal_json(
        JSON.parse(response_data),
        Crystalline::DiscriminatedUnion.new(
          "service",
          {
            "downloadable" => Models::Components::DownloadableFileRead,
            "product_media" => Models::Components::ProductMediaFileRead,
            "organization_avatar" => Models::Components::OrganizationAvatarFileRead
          }
        )
      )
      response = Models::Operations::FilesUploadedResponse.new(
        status_code: http_response.status,
        content_type: content_type,
        raw_response: http_response,
        response_files_uploaded: T.unsafe(obj)
      )
      return response
    else
      raise(
        ::OpenApiSDK::Models::Errors::APIError.new(
          status_code: http_response.status,
          body: http_response.env.response_body,
          raw_response: http_response
        ),
        "Unknown content type received"
      )
    end
  elsif Utils.match_status_code(http_response.status, ["403"])
    if Utils.match_content_type(content_type, "application/json")
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
      response_data = http_response.env.response_body
      obj = Crystalline.unmarshal_json(JSON.parse(response_data), Models::Errors::NotPermitted)
      raise obj
    else
      raise(
        ::OpenApiSDK::Models::Errors::APIError.new(
          status_code: http_response.status,
          body: http_response.env.response_body,
          raw_response: http_response
        ),
        "Unknown content type received"
      )
    end
  elsif Utils.match_status_code(http_response.status, ["404"])
    if Utils.match_content_type(content_type, "application/json")
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
      response_data = http_response.env.response_body
      obj = Crystalline.unmarshal_json(JSON.parse(response_data), Models::Errors::ResourceNotFound)
      raise obj
    else
      raise(
        ::OpenApiSDK::Models::Errors::APIError.new(
          status_code: http_response.status,
          body: http_response.env.response_body,
          raw_response: http_response
        ),
        "Unknown content type received"
      )
    end
  elsif Utils.match_status_code(http_response.status, ["422"])
    if Utils.match_content_type(content_type, "application/json")
      http_response = @sdk_configuration.hooks.after_success(
        hook_ctx: SDKHooks::AfterSuccessHookContext.new(
          hook_ctx: hook_ctx
        ),
        response: http_response
      )
      response_data = http_response.env.response_body
      obj = Crystalline.unmarshal_json(JSON.parse(response_data), Models::Errors::HTTPValidationError)
      raise obj
    else
      raise(
        ::OpenApiSDK::Models::Errors::APIError.new(
          status_code: http_response.status,
          body: http_response.env.response_body,
          raw_response: http_response
        ),
        "Unknown content type received"
      )
    end
  elsif Utils.match_status_code(http_response.status, ["4XX"])
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "API error occurred"
    )
  elsif Utils.match_status_code(http_response.status, ["5XX"])
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "API error occurred"
    )
  else
    raise(
      ::OpenApiSDK::Models::Errors::APIError.new(
        status_code: http_response.status,
        body: http_response.env.response_body,
        raw_response: http_response
      ),
      "Unknown status code received"
    )
  end
end