001/* 002 * Copyright 2024 Vonage 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.vonage.client.subaccounts; 017 018import com.fasterxml.jackson.annotation.JsonProperty; 019import com.vonage.client.JsonableBaseObject; 020 021public class CreateSubaccountRequest extends JsonableBaseObject { 022 String primaryAccountApiKey; 023 private final String name, secret; 024 private final Boolean usePrimaryAccountBalance; 025 026 CreateSubaccountRequest(Builder builder) { 027 if ((name = builder.name) == null || (name.trim().isEmpty() || name.length() > 80) ){ 028 throw new IllegalArgumentException("Name must be between 1 and 80 characters long."); 029 } 030 if ((secret = builder.secret) != null && (secret.trim().length() < 8 || secret.length() > 25)) { 031 throw new IllegalArgumentException("Secret must be between 8 and 25 characters long."); 032 } 033 usePrimaryAccountBalance = builder.usePrimaryAccountBalance; 034 } 035 036 /** 037 * The primary account's API key. 038 * 039 * @return The primary account API key. 040 */ 041 @JsonProperty("primary_account_api_key") 042 String getPrimaryAccountApiKey() { 043 return primaryAccountApiKey; 044 } 045 046 /** 047 * Name of the subaccount. 048 * 049 * @return The subaccount name. 050 */ 051 @JsonProperty("name") 052 public String getName() { 053 return name; 054 } 055 056 /** 057 * Whether to use the primary account's balance. 058 * 059 * @return {@code true} if the balance is shared with the primary account or {@code null} if unspecified (the default). 060 */ 061 @JsonProperty("use_primary_account_balance") 062 public Boolean getUsePrimaryAccountBalance() { 063 return usePrimaryAccountBalance; 064 } 065 066 /** 067 * Subaccount API secret. 068 * 069 * @return The secret to associate with this API key. 070 */ 071 @JsonProperty("secret") 072 public String getSecret() { 073 return secret; 074 } 075 076 /** 077 * Entry point for constructing an instance of this class. 078 * 079 * @return A new Builder. 080 */ 081 public static Builder builder() { 082 return new Builder(); 083 } 084 085 public static class Builder { 086 private String name, secret; 087 private Boolean usePrimaryAccountBalance; 088 089 Builder() {} 090 091 /** 092 * (REQUIRED) Name of the subaccount. 093 * 094 * @param name Subaccount display name. 095 * 096 * @return This builder. 097 */ 098 public Builder name(String name) { 099 this.name = name; 100 return this; 101 } 102 103 /** 104 * (OPTIONAL) Whether to use the primary account's balance. 105 * 106 * @param usePrimaryAccountBalance {@code true} if the balance should be shared with the primary account. 107 * 108 * @return This builder. 109 */ 110 public Builder usePrimaryAccountBalance(boolean usePrimaryAccountBalance) { 111 this.usePrimaryAccountBalance = usePrimaryAccountBalance; 112 return this; 113 } 114 115 /** 116 * (OPTIONAL) Secret for the subaccount. The requirements are as follows: 117 * <ul> 118 * <li>Between 8 and 25 characters long</li> 119 * <li>1 lowercase letter</li> 120 * <li>1 capital letter</li> 121 * <li>1 digit</li> 122 * <li>Must be unique.</li> 123 * </ul> 124 * 125 * @param secret Subaccount API secret. 126 * 127 * @return This builder. 128 */ 129 public Builder secret(String secret) { 130 this.secret = secret; 131 return this; 132 } 133 134 /** 135 * Builds the {@linkplain CreateSubaccountRequest}. 136 * 137 * @return An instance of CreateSubaccountRequest, populated with all fields from this builder. 138 */ 139 public CreateSubaccountRequest build() { 140 return new CreateSubaccountRequest(this); 141 } 142 } 143}