001package com.box.sdkgen.managers.storagepolicies; 002 003import static com.box.sdkgen.internal.utils.UtilsManager.convertToString; 004import static com.box.sdkgen.internal.utils.UtilsManager.entryOf; 005import static com.box.sdkgen.internal.utils.UtilsManager.mapOf; 006import static com.box.sdkgen.internal.utils.UtilsManager.mergeMaps; 007import static com.box.sdkgen.internal.utils.UtilsManager.prepareParams; 008 009import com.box.sdkgen.networking.auth.Authentication; 010import com.box.sdkgen.networking.fetchoptions.FetchOptions; 011import com.box.sdkgen.networking.fetchoptions.ResponseFormat; 012import com.box.sdkgen.networking.fetchresponse.FetchResponse; 013import com.box.sdkgen.networking.network.NetworkSession; 014import com.box.sdkgen.schemas.storagepolicies.StoragePolicies; 015import com.box.sdkgen.schemas.storagepolicy.StoragePolicy; 016import com.box.sdkgen.serialization.json.JsonManager; 017import java.util.Map; 018 019public class StoragePoliciesManager { 020 021 public Authentication auth; 022 023 public NetworkSession networkSession; 024 025 public StoragePoliciesManager() { 026 this.networkSession = new NetworkSession(); 027 } 028 029 protected StoragePoliciesManager(Builder builder) { 030 this.auth = builder.auth; 031 this.networkSession = builder.networkSession; 032 } 033 034 /** Fetches all the storage policies in the enterprise. */ 035 public StoragePolicies getStoragePolicies() { 036 return getStoragePolicies(new GetStoragePoliciesQueryParams(), new GetStoragePoliciesHeaders()); 037 } 038 039 /** 040 * Fetches all the storage policies in the enterprise. 041 * 042 * @param queryParams Query parameters of getStoragePolicies method 043 */ 044 public StoragePolicies getStoragePolicies(GetStoragePoliciesQueryParams queryParams) { 045 return getStoragePolicies(queryParams, new GetStoragePoliciesHeaders()); 046 } 047 048 /** 049 * Fetches all the storage policies in the enterprise. 050 * 051 * @param headers Headers of getStoragePolicies method 052 */ 053 public StoragePolicies getStoragePolicies(GetStoragePoliciesHeaders headers) { 054 return getStoragePolicies(new GetStoragePoliciesQueryParams(), headers); 055 } 056 057 /** 058 * Fetches all the storage policies in the enterprise. 059 * 060 * @param queryParams Query parameters of getStoragePolicies method 061 * @param headers Headers of getStoragePolicies method 062 */ 063 public StoragePolicies getStoragePolicies( 064 GetStoragePoliciesQueryParams queryParams, GetStoragePoliciesHeaders headers) { 065 Map<String, String> queryParamsMap = 066 prepareParams( 067 mapOf( 068 entryOf("fields", convertToString(queryParams.getFields())), 069 entryOf("marker", convertToString(queryParams.getMarker())), 070 entryOf("limit", convertToString(queryParams.getLimit())))); 071 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 072 FetchResponse response = 073 this.networkSession 074 .getNetworkClient() 075 .fetch( 076 new FetchOptions.Builder( 077 String.join( 078 "", 079 this.networkSession.getBaseUrls().getBaseUrl(), 080 "/2.0/storage_policies"), 081 "GET") 082 .params(queryParamsMap) 083 .headers(headersMap) 084 .responseFormat(ResponseFormat.JSON) 085 .auth(this.auth) 086 .networkSession(this.networkSession) 087 .build()); 088 return JsonManager.deserialize(response.getData(), StoragePolicies.class); 089 } 090 091 /** 092 * Fetches a specific storage policy. 093 * 094 * @param storagePolicyId The ID of the storage policy. Example: "34342" 095 */ 096 public StoragePolicy getStoragePolicyById(String storagePolicyId) { 097 return getStoragePolicyById(storagePolicyId, new GetStoragePolicyByIdHeaders()); 098 } 099 100 /** 101 * Fetches a specific storage policy. 102 * 103 * @param storagePolicyId The ID of the storage policy. Example: "34342" 104 * @param headers Headers of getStoragePolicyById method 105 */ 106 public StoragePolicy getStoragePolicyById( 107 String storagePolicyId, GetStoragePolicyByIdHeaders headers) { 108 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 109 FetchResponse response = 110 this.networkSession 111 .getNetworkClient() 112 .fetch( 113 new FetchOptions.Builder( 114 String.join( 115 "", 116 this.networkSession.getBaseUrls().getBaseUrl(), 117 "/2.0/storage_policies/", 118 convertToString(storagePolicyId)), 119 "GET") 120 .headers(headersMap) 121 .responseFormat(ResponseFormat.JSON) 122 .auth(this.auth) 123 .networkSession(this.networkSession) 124 .build()); 125 return JsonManager.deserialize(response.getData(), StoragePolicy.class); 126 } 127 128 public Authentication getAuth() { 129 return auth; 130 } 131 132 public NetworkSession getNetworkSession() { 133 return networkSession; 134 } 135 136 public static class Builder { 137 138 protected Authentication auth; 139 140 protected NetworkSession networkSession; 141 142 public Builder() {} 143 144 public Builder auth(Authentication auth) { 145 this.auth = auth; 146 return this; 147 } 148 149 public Builder networkSession(NetworkSession networkSession) { 150 this.networkSession = networkSession; 151 return this; 152 } 153 154 public StoragePoliciesManager build() { 155 if (this.networkSession == null) { 156 this.networkSession = new NetworkSession(); 157 } 158 return new StoragePoliciesManager(this); 159 } 160 } 161}