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