001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *      André Justo
018 */
019
020package org.nuxeo.ecm.media.publishing.wistia;
021
022import com.sun.jersey.api.client.Client;
023import com.sun.jersey.api.client.WebResource;
024import com.sun.jersey.multipart.BodyPart;
025import com.sun.jersey.multipart.MultiPart;
026import com.sun.jersey.multipart.file.FileDataBodyPart;
027import com.sun.jersey.multipart.file.StreamDataBodyPart;
028import org.nuxeo.ecm.media.publishing.wistia.model.Account;
029import org.nuxeo.ecm.media.publishing.wistia.model.Media;
030import org.nuxeo.ecm.media.publishing.wistia.model.Project;
031import org.nuxeo.ecm.media.publishing.wistia.model.Stats;
032import org.nuxeo.ecm.media.publishing.wistia.rest.RestRequest;
033import org.nuxeo.ecm.media.publishing.wistia.rest.RestResponse;
034import org.nuxeo.ecm.media.publishing.wistia.rest.WistiaResponseParser;
035import org.nuxeo.ecm.media.publishing.wistia.rest.RequestType;
036
037import javax.ws.rs.core.MediaType;
038import javax.ws.rs.core.MultivaluedMap;
039import java.io.File;
040import java.io.InputStream;
041import java.util.List;
042
043public class WistiaClient {
044
045    private static String BASE_URL = "https://api.wistia.com/v1";
046
047    private static String BASE_UPLOAD_URL = "https://upload.wistia.com";
048
049    private static String BASE_EMBED_URL = "http://fast.wistia.net";
050
051    protected String apiPassword;
052
053    protected WebResource dataService;
054
055    protected WebResource uploadService;
056
057    protected WebResource embedService;
058
059    public WistiaClient(String apiPassword) {
060        this.apiPassword = apiPassword;
061        dataService = new Client().resource(BASE_URL).queryParam("api_password", apiPassword);
062        uploadService = new Client().resource(BASE_UPLOAD_URL).queryParam("api_password", apiPassword);
063        embedService = new Client().resource(BASE_EMBED_URL);
064    }
065
066    /**
067     * Obtains a list of all the media in an account.
068     * @return
069     */
070    public List<Media> getMedias() {
071        RestResponse response = new RestRequest(dataService, "medias.json")
072                .execute();
073
074        return WistiaResponseParser.asMediaList(response.getClientResponse());
075    }
076
077    /**
078     * Gets information about a specific piece of media uploaded to an account.
079     * @param hashedId
080     * @return
081     */
082    public Media getMedia(String hashedId) {
083        RestResponse response = new RestRequest(dataService, "medias/" + hashedId + ".json")
084                .execute();
085
086        return WistiaResponseParser.asMedia(response.getClientResponse());
087    }
088
089    /**
090     * Updates attributes on a piece of media.
091     * @param hashedId
092     * @param queryParams
093     * @return
094     */
095    public Media updateMedia(String hashedId, MultivaluedMap<String,String> queryParams) {
096        RestResponse response = new RestRequest(dataService, "medias/" + hashedId + ".json")
097                .requestType(RequestType.PUT)
098                .queryParams(queryParams)
099                .execute();
100
101        return WistiaResponseParser.asMedia(response.getClientResponse());
102    }
103
104    /**
105     * Deletes a media from an account.
106     * @param hashedId
107     * @return
108     */
109    public Media deleteMedia(String hashedId) {
110        RestResponse response = new RestRequest(dataService, "medias/" + hashedId + ".json")
111                .requestType(RequestType.DELETE)
112                .execute();
113
114        return WistiaResponseParser.asMedia(response.getClientResponse());
115    }
116
117    /**
118     * Aggregates tracking statistics for a video that has been embedded in a website.
119     * @param hashedId
120     * @return
121     */
122    public Stats getMediaStats(String hashedId) {
123        RestResponse response = new RestRequest(dataService, "medias/" + hashedId + "/stats.json")
124                .requestType(RequestType.GET)
125                .execute();
126
127        return WistiaResponseParser.asMedia(response.getClientResponse()).getStats();
128    }
129
130    /**
131     * Gets information about an account.
132     * @return
133     */
134    public Account getAccount() {
135        RestResponse response = new RestRequest(dataService, "account.json")
136                .execute();
137
138        return WistiaResponseParser.asAccount(response.getClientResponse());
139    }
140
141    /**
142     * Obtains a list of all the projects in an account.
143     * @return
144     */
145    public List<Project> getProjects() {
146        RestResponse response = new RestRequest(dataService, "projects.json")
147                .execute();
148
149        return WistiaResponseParser.asProjectList(response.getClientResponse());
150    }
151
152    /**
153     * Gets information about a specific project.
154     * @param hashedId
155     * @return
156     */
157    public Project getProject(String hashedId) {
158        RestResponse response = new RestRequest(dataService, "projects/" + hashedId + ".json")
159                .execute();
160
161        return WistiaResponseParser.asProject(response.getClientResponse());
162    }
163
164    /**
165     * Creates a new project.
166     * @param name
167     * @param queryParams
168     * @return
169     */
170    public Project createProject(String name, MultivaluedMap<String,String> queryParams) {
171        RestResponse response = new RestRequest(dataService, "projects.json")
172                .requestType(RequestType.POST)
173                .queryParams(queryParams)
174                .queryParam("name", name)
175                .execute();
176
177        return WistiaResponseParser.asProject(response.getClientResponse());
178    }
179
180    /**
181     * Updates attributes on a project.
182     * @param hashedId
183     * @param queryParams
184     * @return
185     */
186    public Project updateProject(String hashedId, MultivaluedMap<String,String> queryParams) {
187        RestResponse response = new RestRequest(dataService, "projects/" + hashedId + ".json")
188                .requestType(RequestType.PUT)
189                .queryParams(queryParams)
190                .execute();
191
192        return WistiaResponseParser.asProject(response.getClientResponse());
193    }
194
195    /**
196     * Deletes a project from an account.
197     * @param hashedId
198     * @return
199     */
200    public Project deleteProject(String hashedId) {
201        RestResponse response = new RestRequest(dataService, "projects/" + hashedId + ".json")
202                .requestType(RequestType.DELETE)
203                .execute();
204
205        return WistiaResponseParser.asProject(response.getClientResponse());
206    }
207
208    /**
209     * Uploads a file from URL.
210     * @param url
211     * @param queryParams
212     * @return
213     */
214    public Media upload(String url, MultivaluedMap<String, String> queryParams) {
215        RestResponse response = new RestRequest(uploadService, "")
216                .requestType(RequestType.POST)
217                .contentType(MediaType.APPLICATION_FORM_URLENCODED)
218                .queryParams(queryParams)
219                .queryParam("url", url)
220                .execute();
221
222        return WistiaResponseParser.asMedia(response.getClientResponse());
223    }
224
225    /**
226     * Uploads a file.
227     * @param file
228     * @param queryParams
229     * @return
230     */
231    public Media upload(File file, MultivaluedMap<String, String> queryParams) {
232        FileDataBodyPart bodyPart = new FileDataBodyPart(file.getName(),
233                file, MediaType.APPLICATION_OCTET_STREAM_TYPE);
234        return upload(bodyPart, queryParams);
235    }
236
237    /**
238     * Uploads a file.
239     * @param stream
240     * @param queryParams
241     * @return
242     */
243    public Media upload(String filename, InputStream stream, MultivaluedMap<String, String> queryParams) {
244        StreamDataBodyPart bodyPart = new StreamDataBodyPart(filename,
245            stream, filename, MediaType.APPLICATION_OCTET_STREAM_TYPE);
246        return upload(bodyPart, queryParams);
247    }
248
249    private Media upload(BodyPart bodyPart, MultivaluedMap<String, String> queryParams) {
250        MultiPart multiPart = new MultiPart();
251        multiPart.bodyPart(bodyPart);
252
253        RestResponse response = new RestRequest(uploadService, "")
254            .requestType(RequestType.POST)
255            .contentType(MediaType.MULTIPART_FORM_DATA)
256            .queryParams(queryParams)
257            .execute(multiPart);
258
259        return WistiaResponseParser.asMedia(response.getClientResponse());
260    }
261
262    public String getEmbedCode(String mediaUrl) {
263        RestResponse response = new RestRequest(embedService, "oembed")
264                .queryParam("url", mediaUrl)
265                .execute();
266
267        if (response.getStatus() == 200) {
268            return response.asJson().get("html").getTextValue();
269        }
270
271        return null;
272    }
273}