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.video;
017
018import com.vonage.client.QueryParamsRequest;
019import java.util.LinkedHashMap;
020import java.util.Map;
021
022/**
023 * Filter properties and pagination for {@link VideoClient#listArchives(ListStreamCompositionsRequest)}
024 * and {@link VideoClient#listBroadcasts(ListStreamCompositionsRequest)}.
025 */
026public class ListStreamCompositionsRequest implements QueryParamsRequest {
027        private final Integer offset, count;
028        private final String sessionId;
029
030        public ListStreamCompositionsRequest(Builder builder) {
031                sessionId = builder.sessionId;
032                if ((offset = builder.offset) != null && offset < 0) {
033                        throw new IllegalArgumentException("Offset cannot be negative.");
034                }
035                if ((count = builder.count) != null && (count < 0 || count > 1000)) {
036                        throw new IllegalArgumentException("Count must be between 0 and 1000.");
037                }
038        }
039
040        @Override
041        public Map<String, String> makeParams() {
042                Map<String, String> params = new LinkedHashMap<>(4);
043                if (offset != null) {
044                        params.put("offset", offset.toString());
045                }
046                if (count != null) {
047                        params.put("count", count.toString());
048                }
049                if (sessionId != null) {
050                        params.put("sessionId", sessionId);
051                }
052                return params;
053        }
054
055        /**
056         * The start offset in the list of existing compositions.
057         *
058         * @return The results offset index.
059         */
060        public Integer getOffset() {
061                return offset;
062        }
063
064        /**
065         * The number of compositions to retrieve starting at offset.
066         *
067         * @return The maximum number of results.
068         */
069        public Integer getCount() {
070                return count;
071        }
072
073        /**
074         * Retrieve only compositions for a given session ID.
075         *
076         * @return The session ID to filter by.
077         */
078        public String getSessionId() {
079                return sessionId;
080        }
081
082        /**
083         * Used in {@linkplain VideoClient} for default retrieval (no-args).
084         *
085         * @return A new filter for returning the maximum number of results.
086         *
087         * @since 8.6.0
088         */
089        static ListStreamCompositionsRequest maxResults() {
090                return builder().count(1000).build();
091        }
092
093        /**
094         * Instantiates a Builder, used to construct this object.
095         *
096         * @return A new {@linkplain ListStreamCompositionsRequest.Builder}.
097         */
098        public static Builder builder() {
099                return new Builder();
100        }
101
102        public static class Builder {
103                private Integer offset, count;
104                private String sessionId;
105
106                Builder() {}
107
108                /**
109                 * Set an offset query parameters to specify the index offset of the first composition.
110                 * 0 is offset of the most recently started composition (excluding deleted compositions).
111                 * 1 is the offset of the composition that started prior to the most recent composition.
112                 * The default value is 0.
113                 *
114                 * @param offset The offset index.
115                 *
116                 * @return This builder with the offset setting.
117                 */
118                public Builder offset(int offset) {
119                        this.offset = offset;
120                        return this;
121                }
122
123
124                /**
125                 * Set a count query parameter to limit the number of composition to be returned. The default number of
126                 * composition returned is 50 (or fewer, if there are fewer than 50 compositions). The maximum number of
127                 * composition the call will return is 1000.
128                 *
129                 * @param count The number of results to return, between 0 and 1000.
130                 *
131                 * @return This builder with the count setting.
132                 */
133                public Builder count(int count) {
134                        this.count = count;
135                        return this;
136                }
137
138                /**
139                 * Set a sessionId query parameter to list compositions for a specific session ID. This is useful
140                 * when listing multiple compositions for sessions with {@link StreamMode#AUTO}.
141                 *
142                 * @param sessionId The session ID to filter by.
143                 *
144                 * @return This builder with the sessionId setting.
145                 */
146                public Builder sessionId(String sessionId) {
147                        this.sessionId = sessionId;
148                        return this;
149                }
150
151                /**
152                 * Builds the {@linkplain ListStreamCompositionsRequest} with this builder's settings.
153                 *
154                 * @return A new {@link ListStreamCompositionsRequest} instance.
155                 */
156                public ListStreamCompositionsRequest build() {
157                        return new ListStreamCompositionsRequest(this);
158                }
159        }
160}