|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Trophy |
| 4 | + module Admin |
| 5 | + module Environments |
| 6 | + class Client |
| 7 | + # @param client [Trophy::Internal::Http::RawClient] |
| 8 | + # @param base_url [String, nil] |
| 9 | + # @param environment [Hash[Symbol, String], nil] |
| 10 | + # |
| 11 | + # @return [void] |
| 12 | + def initialize(client:, base_url: nil, environment: nil) |
| 13 | + @client = client |
| 14 | + @base_url = base_url |
| 15 | + @environment = environment |
| 16 | + end |
| 17 | + |
| 18 | + # List active environments. |
| 19 | + # |
| 20 | + # @param request_options [Hash] |
| 21 | + # @param _params [Hash] |
| 22 | + # @option request_options [String] :base_url |
| 23 | + # @option request_options [Hash{String => Object}] :additional_headers |
| 24 | + # @option request_options [Hash{String => Object}] :additional_query_parameters |
| 25 | + # @option request_options [Hash{String => Object}] :additional_body_parameters |
| 26 | + # @option request_options [Integer] :timeout_in_seconds |
| 27 | + # |
| 28 | + # @return [Array[Trophy::Types::AdminEnvironment]] |
| 29 | + def list(request_options: {}, **_params) |
| 30 | + request = Trophy::Internal::JSON::Request.new( |
| 31 | + base_url: request_options[:base_url] || @base_url || @environment&.dig(:admin), |
| 32 | + method: "GET", |
| 33 | + path: "environments", |
| 34 | + request_options: request_options |
| 35 | + ) |
| 36 | + begin |
| 37 | + response = @client.send(request) |
| 38 | + rescue Net::HTTPRequestTimeout |
| 39 | + raise Trophy::Errors::TimeoutError |
| 40 | + end |
| 41 | + code = response.code.to_i |
| 42 | + if code.between?(200, 299) |
| 43 | + Trophy::Types::ListEnvironmentsResponse.load(response.body) |
| 44 | + else |
| 45 | + error_class = Trophy::Errors::ResponseError.subclass_for_code(code) |
| 46 | + raise error_class.new(response.body, code: code) |
| 47 | + end |
| 48 | + end |
| 49 | + end |
| 50 | + end |
| 51 | + end |
| 52 | +end |
0 commit comments