/// <summary> /// Uploads one file to the Now /// </summary> /// <see cref="https://zeit.co/docs/api/#endpoints/deployments/upload-deployment-files" /> private static async Task UploadFileRequest(DeploymentFile deploymentFile, HttpMessageInvoker httpClient, ConfigureNow configuration, CancellationToken cancellationToken = default) { var httpMessage = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = new Uri($"{configuration.BaseUrl}/files"), Headers = { { "Authorization", $"Bearer {configuration.Token}" }, { "x-now-digest", deploymentFile.Checksum } }, Content = new ByteArrayContent(deploymentFile.Data) }; httpMessage.Content.Headers.ContentLength = deploymentFile.FileSize; using (var response = await httpClient.SendAsync(httpMessage, cancellationToken)) { var responseMessage = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { Debug.LogError($"[unity-now]: Something went wrong during file upload.\n{responseMessage}"); } } }
/// <summary> /// Creates new Now deployment /// </summary> /// <see cref="https://zeit.co/docs/api/#endpoints/deployments/create-a-new-deployment" /> private static async Task CreateDeploymentRequest(IEnumerable <DeploymentFileReference> deploymentFileReferences, string deploymentName, HttpMessageInvoker httpClient, ConfigureNow configuration, CancellationToken cancellationToken = default) { var content = new DeploymentContent { Name = deploymentName, Version = 2, Files = deploymentFileReferences.ToArray() }; var jsonContent = JsonConvert.SerializeObject(content); var httpMessage = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = new Uri($"{configuration.BaseUrl}/deployments"), Headers = { { "Authorization", $"Bearer {configuration.Token}" } }, Content = new StringContent(jsonContent, Encoding.UTF8, "application/json") }; using (var response = await httpClient.SendAsync(httpMessage, cancellationToken)) { var responseMessage = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { var jObject = JObject.Parse(responseMessage); var deploymentUrl = jObject["url"].ToString(); // Copy URL to clipboard if (configuration.CopyUrl) { GUIUtility.systemCopyBuffer = deploymentUrl; } Debug.Log($"[unity-now]: Ready! <i>https://{deploymentUrl}</i> " + $"{(configuration.CopyUrl ? "(copied)" : "")}"); } else { Debug.LogError($"[unity-now]: Something went wrong during deployment creation.\n{responseMessage}"); } } }