using System.Collections.Generic; using System.Net; using Newtonsoft.Json; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; using MetaCity.BundleKit.Editor.Utils; using UnityEngine; namespace MetaCity.BundleKit.Editor { public class MetacityClient { public static HttpClient client = new HttpClient(); private static readonly string baseUri_int = "https://metacity-int.unity.cn/api/v2"; private static readonly string baseUri_test = "http://localhost:4200/api/v2"; private static readonly string baseUri_u3d = "http://universe.u3d.cn/api/v2"; private static readonly string baseUri = baseUri_test; private static string UploadUrl = "/template/official?uploadbundle=true"; private static string _token; public static async Task GetUploadUrlAsync(string filename) { HttpResponseMessage response = await client.GetAsync(string.Format("{0}/resource/upload-url?file={1}", baseUri_int, filename)); if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); UrlResponse urlResponse = JsonConvert.DeserializeObject(json); return urlResponse.url; } return null; } // 返回当前url public static string GetUploadUrl(WebUrl testmode) { string org = "/api/v2"; string url = baseUri_test.Replace(org, UploadUrl); if (testmode == WebUrl.MetacityInt) { url = baseUri_int.Replace(org, UploadUrl); } else if (testmode == WebUrl.U3D) { url = baseUri_u3d.Replace(org, UploadUrl); } //将baseUri中的/api/v2替换为/template/official?uploadbundle=true return url; } public static async Task GetTemplateById(string templateId) { HttpResponseMessage response = await client.GetAsync(string.Format("{0}/template/id/{1}", baseUri, templateId)); if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); TemplateResponse templateResponse = JsonConvert.DeserializeObject(json); return templateResponse; } else { Debug.LogError(response.ToString()); } return null; } public static async Task> GetAssetBundleByTemplateId(string templateId) { HttpResponseMessage response = await client.GetAsync(string.Format("{0}/artifact/template/{1}", baseUri, templateId)); if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); List abResponses = JsonConvert.DeserializeObject>(json); return abResponses; } else { Debug.LogError(response.ToString()); } return null; } public static async Task GetCloudrenderingById(string cloudrenderingId) { HttpResponseMessage response = await client.GetAsync(string.Format("{0}/cloudrendering/{1}", baseUri, cloudrenderingId)); if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); CloudrenderingResponse crResponse = JsonConvert.DeserializeObject(json); return crResponse; } else { Debug.LogError(response.ToString()); } return null; } public static async Task CheckAccessToken() { HttpResponseMessage response = await client.GetAsync(string.Format("{0}/users/me", baseUri)); if (response.IsSuccessStatusCode) { return true; } return false; } public static async Task GetUserId() { HttpResponseMessage response = await client.GetAsync(string.Format("{0}/users/me", baseUri)); if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); UserResponse userResponse = JsonConvert.DeserializeObject(json); return userResponse?.id; } else { Debug.LogError(response.ToString()); } return null; } public static async Task CreateTemplate(string name, string description, string banner, string platform = "cloudrendering") { var data = new TemplateInput { name = name, description = description, banner = banner, platform = platform, official = false, state = "Draft" }; var jsonInput = JsonTools.ToJson(data); HttpContent content = new StringContent(jsonInput,Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync( string.Format("{0}/template", baseUri), content); if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); TemplateResponse templateResponse = JsonConvert.DeserializeObject(json); return templateResponse; } else { Debug.LogError(response.ToString()); } return null; } public static async Task UpdateTemplateById(TemplateResponse oldTemplate, string templateId, string name, string description, string banner, string activeId, string platform = "cloudrendering") { var data = new TemplateInput { name = name, description = description, banner = banner, platform = platform, activeId = activeId, official = oldTemplate.official, state = oldTemplate.state, uosId = oldTemplate.uosId }; var jsonInput = JsonTools.ToJson(data); HttpContent content = new StringContent(jsonInput,Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync( string.Format("{0}/template/{1}", baseUri, templateId), content); if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); TemplateResponse templateResponse = JsonConvert.DeserializeObject(json); return templateResponse; } else { Debug.LogError(response.ToString()); } return null; } public static async Task CreateCloudrendering(string name, string description, string template, string version = "0.0.1") { var data = new CreateCRInput() { name = name, description = description, version = version, template = template }; var jsonInput = JsonTools.ToJson(data); HttpContent content = new StringContent(jsonInput,Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync( string.Format("{0}/cloudrendering", baseUri), content); if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); CloudrenderingResponse crResponse = JsonConvert.DeserializeObject(json); return crResponse; } else { Debug.LogError(response.ToString()); } return null; } public static async Task Publish( string templateId, string cloudrenderingId, string serverType, List assetBundles) { var data = new PublishInput() { templateId = templateId, cloudrenderingId = cloudrenderingId, serverType = serverType, assetBundles = assetBundles }; var jsonInput = JsonTools.ToJson(data); HttpContent content = new StringContent(jsonInput, Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync(string.Format("{0}/publish", baseUri), content); if (response.IsSuccessStatusCode) { return true; } else { Debug.LogError(response.ToString()); } return false; } public static async Task PublishAssetBundles(List assetBundles) { var data = new PublishAssetBundleInput() { assetBundles = assetBundles }; var jsonInput = JsonTools.ToJson(data); HttpContent content = new StringContent(jsonInput, Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync(string.Format("{0}/publish/assetbundles", baseUri), content); if (response.IsSuccessStatusCode) { return true; } else { Debug.LogError(response.ToString()); } return false; } } }