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.voice.ncco;
017
018import com.fasterxml.jackson.annotation.JsonProperty;
019import com.vonage.client.JsonableBaseObject;
020import java.util.Arrays;
021import java.util.Collection;
022import java.util.Map;
023
024/**
025 * An NCCO notify action which allows for custom events to be sent to a configured webhook.
026 */
027public class NotifyAction extends JsonableBaseObject implements Action {
028    private static final String ACTION = "notify";
029
030    private Map<String, ?> payload;
031    private Collection<String> eventUrl;
032    private EventMethod eventMethod;
033
034    NotifyAction() {}
035
036    private NotifyAction(Builder builder) {
037        payload = builder.payload;
038        eventUrl = builder.eventUrl;
039        eventMethod = builder.eventMethod;
040    }
041
042    @Override
043    public String getAction() {
044        return ACTION;
045    }
046
047    @JsonProperty("payload")
048    public Map<String, ?> getPayload() {
049        return payload;
050    }
051
052    @JsonProperty("eventUrl")
053    public Collection<String> getEventUrl() {
054        return eventUrl;
055    }
056
057    @JsonProperty("eventMethod")
058    public EventMethod getEventMethod() {
059        return eventMethod;
060    }
061
062    public static Builder builder(Map<String, ?> payload, Collection<String> eventUrl) {
063        return new Builder(payload, eventUrl);
064    }
065
066    public static Builder builder(Map<String, ?> payload, String... eventUrl) {
067        return builder(payload, Arrays.asList(eventUrl));
068    }
069
070    public static class Builder {
071        private Map<String, ?> payload;
072        private Collection<String> eventUrl;
073        private EventMethod eventMethod;
074
075        /**
076         * @param payload  A Map of String keys and ? values that will be converted to JSON and sent to your event URL.
077         * @param eventUrl The URL to send events to.
078         */
079        private Builder(Map<String, ?> payload, Collection<String> eventUrl) {
080            this.payload = payload;
081            this.eventUrl = eventUrl;
082        }
083
084        /**
085         * @param payload A Map of String keys and ? values that will be converted to JSON and sent to your event URL.
086         *
087         * @return This builder.
088         */
089        public Builder payload(Map<String, ?> payload) {
090            this.payload = payload;
091            return this;
092        }
093
094        /**
095         * @param eventUrl The URL to send events to.
096         *
097         * @return This builder.
098         */
099        public Builder eventUrl(Collection<String> eventUrl) {
100            this.eventUrl = eventUrl;
101            return this;
102        }
103
104        /**
105         * @param eventUrl The URL to send events to.
106         *
107         * @return This builder.
108         */
109        public Builder eventUrl(String... eventUrl) {
110            return eventUrl(Arrays.asList(eventUrl));
111        }
112
113        /**
114         * @param eventMethod The HTTP method to use when sending the payload to your event url.
115         *
116         * @return This builder.
117         */
118        public Builder eventMethod(EventMethod eventMethod) {
119            this.eventMethod = eventMethod;
120            return this;
121        }
122
123        /**
124         * Builds the NotifyAction.
125         *
126         * @return A new NotifyAction object from the stored builder options.
127         */
128        public NotifyAction build() {
129            return new NotifyAction(this);
130        }
131    }
132}