|
| 1 | +// |
| 2 | +// CreatubblesApiClient.cs |
| 3 | +// CreatubblesApiClient |
| 4 | +// |
| 5 | +// Copyright (c) 2016 Creatubbles Pte. Ltd. |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in |
| 15 | +// all copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +// THE SOFTWARE. |
| 24 | + |
| 25 | +using System; |
| 26 | +using UnityEngine; |
| 27 | +using UnityEngine.Networking; |
| 28 | + |
| 29 | +namespace Creatubbles.Api |
| 30 | +{ |
| 31 | + public class CreatubblesApiClient |
| 32 | + { |
| 33 | + private IApiConfiguration configuration; |
| 34 | + public ISecureStorage secureStorage; // TODO - eventually make it private |
| 35 | + |
| 36 | + public CreatubblesApiClient(IApiConfiguration configuration, ISecureStorage secureStorage) |
| 37 | + { |
| 38 | + this.configuration = configuration; |
| 39 | + this.secureStorage = secureStorage; |
| 40 | + } |
| 41 | + |
| 42 | + // make request methods intance methods |
| 43 | + public UnityWebRequest CreatePostAuthenticationApplicationTokenRequest() |
| 44 | + { |
| 45 | + string url = RequestUrl("/oauth/token"); |
| 46 | + WWWForm data = new WWWForm(); |
| 47 | + data.AddField("grant_type", "client_credentials"); |
| 48 | + data.AddField("client_id", configuration.AppId); |
| 49 | + data.AddField("client_secret", configuration.AppSecret); |
| 50 | + |
| 51 | + UnityWebRequest request = UnityWebRequest.Post(url, data); |
| 52 | + request.downloadHandler = new DownloadHandlerBuffer(); |
| 53 | + |
| 54 | + return request; |
| 55 | + } |
| 56 | + |
| 57 | + public UnityWebRequest CreatePostAuthenticationUserTokenRequest(string username, string password) |
| 58 | + { |
| 59 | + string url = RequestUrl("/oauth/token"); |
| 60 | + WWWForm data = new WWWForm(); |
| 61 | + data.AddField("grant_type", "password"); |
| 62 | + data.AddField("client_id", configuration.AppId); |
| 63 | + data.AddField("client_secret", configuration.AppSecret); |
| 64 | + data.AddField("username", username); |
| 65 | + data.AddField("password", password); |
| 66 | + |
| 67 | + UnityWebRequest request = UnityWebRequest.Post(url, data); |
| 68 | + request.downloadHandler = new DownloadHandlerBuffer(); |
| 69 | + |
| 70 | + return request; |
| 71 | + } |
| 72 | + |
| 73 | + public UnityWebRequest CreateGetLandingUrlsRequest(string applicationToken) |
| 74 | + { |
| 75 | + string url = RequestUrl("/landing_urls"); |
| 76 | + UnityWebRequest request = UnityWebRequest.Get(url); |
| 77 | + SetAuthorizationHeaderBearerToken(request, applicationToken); |
| 78 | + request.downloadHandler = new DownloadHandlerBuffer(); |
| 79 | + |
| 80 | + return request; |
| 81 | + } |
| 82 | + |
| 83 | + public UnityWebRequest CreateGetLoggedInUserRequest(string userToken) |
| 84 | + { |
| 85 | + string url = RequestUrl("/users/me"); |
| 86 | + UnityWebRequest request = UnityWebRequest.Get(url); |
| 87 | + SetAuthorizationHeaderBearerToken(request, userToken); |
| 88 | + request.downloadHandler = new DownloadHandlerBuffer(); |
| 89 | + |
| 90 | + return request; |
| 91 | + } |
| 92 | + |
| 93 | + public UnityWebRequest CreateNewCreationRequest(string userToken, NewCreationData creationData) |
| 94 | + { |
| 95 | + string url = RequestUrl("/creations"); |
| 96 | + WWWForm data = new WWWForm(); |
| 97 | + if (creationData.name != null) |
| 98 | + { |
| 99 | + data.AddField("name", creationData.name); |
| 100 | + } |
| 101 | + if (creationData.creatorIds != null) |
| 102 | + { |
| 103 | + string creatorIds = String.Join(",", creationData.creatorIds); |
| 104 | + data.AddField("creator_ids", creatorIds); |
| 105 | + } |
| 106 | + if (creationData.HasCreationMonth) |
| 107 | + { |
| 108 | + data.AddField("created_at_month", creationData.creationMonth); |
| 109 | + } |
| 110 | + if (creationData.HasCreationYear) |
| 111 | + { |
| 112 | + data.AddField("created_at_year", creationData.creationYear); |
| 113 | + } |
| 114 | + if (creationData.reflectionText != null) |
| 115 | + { |
| 116 | + data.AddField("reflection_text", creationData.reflectionText); |
| 117 | + } |
| 118 | + if (creationData.reflectionVideoUrl != null) |
| 119 | + { |
| 120 | + data.AddField("reflection_video_url", creationData.reflectionVideoUrl); |
| 121 | + } |
| 122 | + |
| 123 | + UnityWebRequest request = UnityWebRequest.Post(url, data); |
| 124 | + SetAuthorizationHeaderBearerToken(request, userToken); |
| 125 | + request.downloadHandler = new DownloadHandlerBuffer(); |
| 126 | + |
| 127 | + return request; |
| 128 | + } |
| 129 | + |
| 130 | + public UnityWebRequest CreateGetCreationRequest(string userToken, string creationId) |
| 131 | + { |
| 132 | + string url = RequestUrl("/creations/" + creationId); |
| 133 | + UnityWebRequest request = UnityWebRequest.Get(url); |
| 134 | + SetAuthorizationHeaderBearerToken(request, userToken); |
| 135 | + request.downloadHandler = new DownloadHandlerBuffer(); |
| 136 | + |
| 137 | + return request; |
| 138 | + } |
| 139 | + |
| 140 | + public UnityWebRequest CreatePostCreationUploadRequest(string userToken, string creationId, UploadExtension extension = UploadExtension.JPG) |
| 141 | + { |
| 142 | + string url = RequestUrl("/creations/" + creationId + "/uploads"); |
| 143 | + WWWForm data = new WWWForm(); |
| 144 | + data.AddField("extension", extension.StringValue()); |
| 145 | + |
| 146 | + UnityWebRequest request = UnityWebRequest.Post(url, data); |
| 147 | + SetAuthorizationHeaderBearerToken(request, userToken); |
| 148 | + request.downloadHandler = new DownloadHandlerBuffer(); |
| 149 | + |
| 150 | + return request; |
| 151 | + } |
| 152 | + |
| 153 | + public UnityWebRequest CreatePutUploadFileRequest(string userToken, string url, string contentType, byte[] data) |
| 154 | + { |
| 155 | + UnityWebRequest request = UnityWebRequest.Put(url, data); |
| 156 | + request.SetRequestHeader("Content-Type", contentType); |
| 157 | + request.downloadHandler = new DownloadHandlerBuffer(); |
| 158 | + |
| 159 | + return request; |
| 160 | + } |
| 161 | + |
| 162 | + // TODO - abortedWithMessage - argument included when upload fails; include the body returned by the failed upload attempt or ‘user’ in case the user aborted the upload |
| 163 | + // API doc: https://stateoftheart.creatubbles.com/api/#update-creation-upload |
| 164 | + public UnityWebRequest CreatePutUploadFinishedRequest(string userToken, string pingUrl, string abortedWithMessage = null) |
| 165 | + { |
| 166 | + UnityWebRequest request; |
| 167 | + if (abortedWithMessage != null) |
| 168 | + { |
| 169 | + WWWForm parameters = new WWWForm(); |
| 170 | + parameters.AddField("aborted_with", abortedWithMessage); |
| 171 | + request = UnityWebRequest.Put(pingUrl, parameters.data); |
| 172 | + } |
| 173 | + else |
| 174 | + { |
| 175 | + request = new UnityWebRequest(pingUrl); |
| 176 | + } |
| 177 | + request.method = UnityWebRequest.kHttpVerbPUT; |
| 178 | + SetAuthorizationHeaderBearerToken(request, userToken); |
| 179 | + request.downloadHandler = new DownloadHandlerBuffer(); |
| 180 | + |
| 181 | + return request; |
| 182 | + } |
| 183 | + |
| 184 | + private string RequestUrl(string path) |
| 185 | + { |
| 186 | + return configuration.BaseUrl + "/" + configuration.ApiVersion + path; |
| 187 | + } |
| 188 | + |
| 189 | + private void SetAuthorizationHeaderBearerToken(UnityWebRequest request, string token) |
| 190 | + { |
| 191 | + request.SetRequestHeader("Authorization", "Bearer " + token); |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments