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.application;
017
018import com.vonage.client.*;
019import com.vonage.client.auth.ApiKeyHeaderAuthMethod;
020import com.vonage.client.common.HttpMethod;
021import java.util.List;
022import java.util.Objects;
023import java.util.UUID;
024import java.util.function.Function;
025
026/**
027 * A client for talking to the Vonage Application API. The standard way to obtain an instance of
028 * this class is to use {@link VonageClient#getApplicationClient()}.
029 */
030public class ApplicationClient {
031    final RestEndpoint<ListApplicationRequest, ApplicationList> listApplications;
032    final RestEndpoint<Application, Application> createApplication;
033    final RestEndpoint<UUID, Application> getApplication;
034    final RestEndpoint<Application, Application> updateApplication;
035    final RestEndpoint<UUID, Void> deleteApplication;
036
037    public ApplicationClient(HttpWrapper wrapper) {
038        @SuppressWarnings("unchecked")
039        final class Endpoint<T, R> extends DynamicEndpoint<T, R> {
040            Endpoint(Function<T, String> pathGetter, HttpMethod method, R... type) {
041                super(DynamicEndpoint.<T, R> builder(type)
042                        .responseExceptionType(ApplicationResponseException.class)
043                        .wrapper(wrapper).requestMethod(method)
044                        .authMethod(ApiKeyHeaderAuthMethod.class)
045                        .pathGetter((de, req) -> {
046                            String base = de.getHttpWrapper().getHttpConfig().getVersionedApiBaseUri("v2");
047                            String path = base + "/applications";
048                            if (pathGetter != null) {
049                                path += "/" + pathGetter.apply(req);
050                            }
051                            return path;
052                        })
053                );
054            }
055        }
056
057        listApplications = new Endpoint<>(null, HttpMethod.GET);
058        createApplication = new Endpoint<>(null, HttpMethod.POST);
059        getApplication = new Endpoint<>(UUID::toString, HttpMethod.GET);
060        updateApplication = new Endpoint<>(Application::getId, HttpMethod.PUT);
061        deleteApplication = new Endpoint<>(UUID::toString, HttpMethod.DELETE);
062    }
063
064    private Application validateApplication(Application request) {
065        return Objects.requireNonNull(request, "Application request is required.");
066    }
067
068    private UUID validateApplicationId(String id) {
069        return UUID.fromString(Objects.requireNonNull(id, "Application ID is required."));
070    }
071
072    /**
073     * Create a new application.
074     *
075     * @param application The application properties for the application to be created with.
076     *
077     * @return The application which has been created.
078     *
079     * @throws ApplicationResponseException If there was an error processing the request.
080     */
081    public Application createApplication(Application application) throws ApplicationResponseException {
082        return createApplication.execute(validateApplication(application));
083    }
084
085    /**
086     * Update an existing application.
087     *
088     * @param application The application properties for the application to be updated with.
089     *
090     * @return The application which has been updated.
091     *
092     * @throws ApplicationResponseException If there was an error processing the request.
093     */
094    public Application updateApplication(Application application) throws ApplicationResponseException {
095        return updateApplication.execute(validateApplication(application));
096    }
097
098    /**
099     * Retrieve an application.
100     *
101     * @param applicationId The UUID of the application to retrieve as a string.
102     *
103     * @return The corresponding application.
104     *
105     * @throws ApplicationResponseException If there was an error processing the request.
106     */
107    public Application getApplication(String applicationId) throws ApplicationResponseException {
108        return getApplication.execute(validateApplicationId(applicationId));
109    }
110
111    /**
112     * Delete an application.
113     *
114     * @param applicationId The UUID of the application to delete as a string.
115     *
116     * @throws ApplicationResponseException If there was an error processing the request.
117     */
118    public void deleteApplication(String applicationId) throws ApplicationResponseException {
119        deleteApplication.execute(validateApplicationId(applicationId));
120    }
121
122    /**
123     * Lists the first 1000 available applications.
124     *
125     * @return The list of available applications.
126     *
127     * @throws ApplicationResponseException If there was an error processing the request.
128     *
129     * @since 7.7.0
130     */
131    public List<Application> listAllApplications() throws ApplicationResponseException {
132        return listApplications(ListApplicationRequest.builder().pageSize(1000).build()).getApplications();
133    }
134
135    /**
136     * Lists the first page of available applications.
137     *
138     * @return The ApplicationList HAL response.
139     *
140     * @throws ApplicationResponseException If there was an error processing the request.
141     */
142    public ApplicationList listApplications() throws ApplicationResponseException {
143        return listApplications(ListApplicationRequest.builder().build());
144    }
145
146    /**
147     * List the available applications.
148     *
149     * @param listApplicationRequest The page and number of applications per page to list.
150     *
151     * @return The ApplicationList HAL response.
152     *
153     * @throws ApplicationResponseException If there was an error processing the request.
154     */
155    public ApplicationList listApplications(ListApplicationRequest listApplicationRequest) throws ApplicationResponseException {
156        return listApplications.execute(listApplicationRequest);
157    }
158}