|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Trophy |
| 4 | + module Admin |
| 5 | + module Rewards |
| 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 rewards. |
| 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 | + # @option params [Integer, nil] :limit |
| 28 | + # @option params [Integer, nil] :skip |
| 29 | + # |
| 30 | + # @return [Array[Trophy::Types::AdminReward]] |
| 31 | + def list(request_options: {}, **params) |
| 32 | + params = Trophy::Internal::Types::Utils.normalize_keys(params) |
| 33 | + query_params = {} |
| 34 | + query_params["limit"] = params[:limit] if params.key?(:limit) |
| 35 | + query_params["skip"] = params[:skip] if params.key?(:skip) |
| 36 | + |
| 37 | + request = Trophy::Internal::JSON::Request.new( |
| 38 | + base_url: request_options[:base_url] || @base_url || @environment&.dig(:admin), |
| 39 | + method: "GET", |
| 40 | + path: "rewards", |
| 41 | + query: query_params, |
| 42 | + request_options: request_options |
| 43 | + ) |
| 44 | + begin |
| 45 | + response = @client.send(request) |
| 46 | + rescue Net::HTTPRequestTimeout |
| 47 | + raise Trophy::Errors::TimeoutError |
| 48 | + end |
| 49 | + code = response.code.to_i |
| 50 | + if code.between?(200, 299) |
| 51 | + Trophy::Types::ListRewardsResponse.load(response.body) |
| 52 | + else |
| 53 | + error_class = Trophy::Errors::ResponseError.subclass_for_code(code) |
| 54 | + raise error_class.new(response.body, code: code) |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + # Create rewards. Optionally include triggers in each reward payload to create them alongside the reward. A |
| 59 | + # reward with any invalid trigger is not created. |
| 60 | + # |
| 61 | + # @param request_options [Hash] |
| 62 | + # @param params [Trophy::Types::CreateRewardsRequest] |
| 63 | + # @option request_options [String] :base_url |
| 64 | + # @option request_options [Hash{String => Object}] :additional_headers |
| 65 | + # @option request_options [Hash{String => Object}] :additional_query_parameters |
| 66 | + # @option request_options [Hash{String => Object}] :additional_body_parameters |
| 67 | + # @option request_options [Integer] :timeout_in_seconds |
| 68 | + # |
| 69 | + # @return [Trophy::Types::CreateRewardsResponse] |
| 70 | + def create(request_options: {}, **params) |
| 71 | + params = Trophy::Internal::Types::Utils.normalize_keys(params) |
| 72 | + request = Trophy::Internal::JSON::Request.new( |
| 73 | + base_url: request_options[:base_url] || @base_url || @environment&.dig(:admin), |
| 74 | + method: "POST", |
| 75 | + path: "rewards", |
| 76 | + body: params, |
| 77 | + request_options: request_options |
| 78 | + ) |
| 79 | + begin |
| 80 | + response = @client.send(request) |
| 81 | + rescue Net::HTTPRequestTimeout |
| 82 | + raise Trophy::Errors::TimeoutError |
| 83 | + end |
| 84 | + code = response.code.to_i |
| 85 | + if code.between?(200, 299) |
| 86 | + Trophy::Types::CreateRewardsResponse.load(response.body) |
| 87 | + else |
| 88 | + error_class = Trophy::Errors::ResponseError.subclass_for_code(code) |
| 89 | + raise error_class.new(response.body, code: code) |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + # Delete rewards by ID. Maximum 100 reward IDs per request. A deleted reward's triggers are deleted too. |
| 94 | + # |
| 95 | + # @param request_options [Hash] |
| 96 | + # @param params [Hash] |
| 97 | + # @option request_options [String] :base_url |
| 98 | + # @option request_options [Hash{String => Object}] :additional_headers |
| 99 | + # @option request_options [Hash{String => Object}] :additional_query_parameters |
| 100 | + # @option request_options [Hash{String => Object}] :additional_body_parameters |
| 101 | + # @option request_options [Integer] :timeout_in_seconds |
| 102 | + # @option params [String, nil] :ids |
| 103 | + # |
| 104 | + # @return [Trophy::Types::DeleteRewardsResponse] |
| 105 | + def delete(request_options: {}, **params) |
| 106 | + params = Trophy::Internal::Types::Utils.normalize_keys(params) |
| 107 | + query_params = {} |
| 108 | + query_params["ids"] = params[:ids] if params.key?(:ids) |
| 109 | + |
| 110 | + request = Trophy::Internal::JSON::Request.new( |
| 111 | + base_url: request_options[:base_url] || @base_url || @environment&.dig(:admin), |
| 112 | + method: "DELETE", |
| 113 | + path: "rewards", |
| 114 | + query: query_params, |
| 115 | + request_options: request_options |
| 116 | + ) |
| 117 | + begin |
| 118 | + response = @client.send(request) |
| 119 | + rescue Net::HTTPRequestTimeout |
| 120 | + raise Trophy::Errors::TimeoutError |
| 121 | + end |
| 122 | + code = response.code.to_i |
| 123 | + if code.between?(200, 299) |
| 124 | + Trophy::Types::DeleteRewardsResponse.load(response.body) |
| 125 | + else |
| 126 | + error_class = Trophy::Errors::ResponseError.subclass_for_code(code) |
| 127 | + raise error_class.new(response.body, code: code) |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + # Update rewards by ID. Maximum 100 rewards per request. Only provided fields are updated; omitted fields are |
| 132 | + # preserved. Use the reward triggers endpoints to change a reward's triggers. |
| 133 | + # |
| 134 | + # @param request_options [Hash] |
| 135 | + # @param params [Trophy::Types::UpdateRewardsRequest] |
| 136 | + # @option request_options [String] :base_url |
| 137 | + # @option request_options [Hash{String => Object}] :additional_headers |
| 138 | + # @option request_options [Hash{String => Object}] :additional_query_parameters |
| 139 | + # @option request_options [Hash{String => Object}] :additional_body_parameters |
| 140 | + # @option request_options [Integer] :timeout_in_seconds |
| 141 | + # |
| 142 | + # @return [Trophy::Types::UpdateRewardsResponse] |
| 143 | + def update(request_options: {}, **params) |
| 144 | + params = Trophy::Internal::Types::Utils.normalize_keys(params) |
| 145 | + request = Trophy::Internal::JSON::Request.new( |
| 146 | + base_url: request_options[:base_url] || @base_url || @environment&.dig(:admin), |
| 147 | + method: "PATCH", |
| 148 | + path: "rewards", |
| 149 | + body: params, |
| 150 | + request_options: request_options |
| 151 | + ) |
| 152 | + begin |
| 153 | + response = @client.send(request) |
| 154 | + rescue Net::HTTPRequestTimeout |
| 155 | + raise Trophy::Errors::TimeoutError |
| 156 | + end |
| 157 | + code = response.code.to_i |
| 158 | + if code.between?(200, 299) |
| 159 | + Trophy::Types::UpdateRewardsResponse.load(response.body) |
| 160 | + else |
| 161 | + error_class = Trophy::Errors::ResponseError.subclass_for_code(code) |
| 162 | + raise error_class.new(response.body, code: code) |
| 163 | + end |
| 164 | + end |
| 165 | + |
| 166 | + # Get a reward by ID. |
| 167 | + # |
| 168 | + # @param request_options [Hash] |
| 169 | + # @param params [Hash] |
| 170 | + # @option request_options [String] :base_url |
| 171 | + # @option request_options [Hash{String => Object}] :additional_headers |
| 172 | + # @option request_options [Hash{String => Object}] :additional_query_parameters |
| 173 | + # @option request_options [Hash{String => Object}] :additional_body_parameters |
| 174 | + # @option request_options [Integer] :timeout_in_seconds |
| 175 | + # @option params [String] :id |
| 176 | + # |
| 177 | + # @return [Trophy::Types::AdminReward] |
| 178 | + def get(request_options: {}, **params) |
| 179 | + params = Trophy::Internal::Types::Utils.normalize_keys(params) |
| 180 | + request = Trophy::Internal::JSON::Request.new( |
| 181 | + base_url: request_options[:base_url] || @base_url || @environment&.dig(:admin), |
| 182 | + method: "GET", |
| 183 | + path: "rewards/#{URI.encode_uri_component(params[:id].to_s)}", |
| 184 | + request_options: request_options |
| 185 | + ) |
| 186 | + begin |
| 187 | + response = @client.send(request) |
| 188 | + rescue Net::HTTPRequestTimeout |
| 189 | + raise Trophy::Errors::TimeoutError |
| 190 | + end |
| 191 | + code = response.code.to_i |
| 192 | + if code.between?(200, 299) |
| 193 | + Trophy::Types::AdminReward.load(response.body) |
| 194 | + else |
| 195 | + error_class = Trophy::Errors::ResponseError.subclass_for_code(code) |
| 196 | + raise error_class.new(response.body, code: code) |
| 197 | + end |
| 198 | + end |
| 199 | + |
| 200 | + # @return [Trophy::Triggers::Client] |
| 201 | + def triggers |
| 202 | + @triggers ||= Trophy::Admin::Rewards::Triggers::Client.new(client: @client, base_url: @base_url, environment: @environment) |
| 203 | + end |
| 204 | + end |
| 205 | + end |
| 206 | + end |
| 207 | +end |
0 commit comments