/// <summary> /// To attach a project an existing project, we must collect all orgs the current user is a member of. /// In addition the current user may be a guest of a specific project, in which case we must also look at /// all projects to find organizations. /// </summary> /// <param name="organizationField"></param> /// <param name="projectIdField"></param> void LoadReuseOrganizationField(PopupField <string> organizationField, PopupField <string> projectIdField = null) { ServicesConfiguration.instance.RequestCurrentUserApiUrl(currentUserApiUrl => { var getOrganizationsRequest = new UnityWebRequest(currentUserApiUrl + "?include=orgs,projects", UnityWebRequest.kHttpVerbGET) { downloadHandler = new DownloadHandlerBuffer() }; getOrganizationsRequest.suppressErrorsToConsole = true; getOrganizationsRequest.SetRequestHeader("AUTHORIZATION", $"Bearer {UnityConnect.instance.GetUserInfo().accessToken}"); var operation = getOrganizationsRequest.SendWebRequest(); operation.completed += op => { try { if (ServicesUtils.IsUnityWebRequestReadyForJsonExtract(getOrganizationsRequest)) { var jsonParser = new JSONParser(getOrganizationsRequest.downloadHandler.text); var json = jsonParser.Parse(); try { m_OrgIdByName.Clear(); var sortedOrganizationNames = new List <string>(); foreach (var rawOrg in json.AsDict()[k_JsonOrgsNodeName].AsList()) { var org = rawOrg.AsDict(); if (k_AnyRoleFilter.Contains(org[k_JsonRoleNodeName].AsString())) { sortedOrganizationNames.Add(org[k_JsonNameNodeName].AsString()); m_OrgIdByName.Add(org[k_JsonNameNodeName].AsString(), org[k_JsonIdNodeName].AsString()); } } foreach (var rawProject in json.AsDict()[k_JsonProjectsNodeName].AsList()) { var project = rawProject.AsDict(); if (!project[k_JsonArchivedNodeName].AsBool() && !sortedOrganizationNames.Contains(project[k_JsonOrgNameNodeName].AsString())) { sortedOrganizationNames.Add(project[k_JsonOrgNameNodeName].AsString()); m_OrgIdByName.Add(project[k_JsonOrgNameNodeName].AsString(), project[k_JsonOrgIdNodeName].AsString()); } } sortedOrganizationNames.Sort(); var popUpChoices = new List <string> { L10n.Tr(k_SelectOrganizationText) }; organizationField.SetEnabled(true); popUpChoices.AddRange(sortedOrganizationNames); organizationField.choices = popUpChoices; organizationField.SetValueWithoutNotify(organizationField.choices[0]); if (projectIdField != null) { projectIdField.choices = new List <string> { L10n.Tr(k_SelectProjectText) }; projectIdField.SetValueWithoutNotify(L10n.Tr(k_SelectProjectText)); projectIdField.SetEnabled(false); } } catch (Exception ex) { if (exceptionCallback != null) { exceptionCallback.Invoke(ex); } else { //If there is no exception callback, we have to at least log it Debug.LogException(ex); } } } else { var ex = new UnityConnectWebRequestException(L10n.Tr(k_CouldNotObtainOrganizationsMessage)) { error = getOrganizationsRequest.error, method = getOrganizationsRequest.method, timeout = getOrganizationsRequest.timeout, url = getOrganizationsRequest.url, responseHeaders = getOrganizationsRequest.GetResponseHeaders(), responseCode = getOrganizationsRequest.responseCode, isHttpError = (getOrganizationsRequest.result == UnityWebRequest.Result.ProtocolError), isNetworkError = (getOrganizationsRequest.result == UnityWebRequest.Result.ConnectionError), }; if (exceptionCallback != null) { exceptionCallback.Invoke(ex); } else { //If there is no exception callback, we have to at least log it Debug.LogException(ex); } } } finally { getOrganizationsRequest.Dispose(); } }; }); }
void LoadProjectField(string organizationName, PopupField <string> projectIdField) { ServicesConfiguration.instance.RequestOrganizationProjectsApiUrl(m_OrgIdByName[organizationName], organizationProjectsApiUrl => { var getProjectsRequest = new UnityWebRequest(organizationProjectsApiUrl, UnityWebRequest.kHttpVerbGET) { downloadHandler = new DownloadHandlerBuffer() }; getProjectsRequest.suppressErrorsToConsole = true; getProjectsRequest.SetRequestHeader("AUTHORIZATION", $"Bearer {UnityConnect.instance.GetUserInfo().accessToken}"); var operation = getProjectsRequest.SendWebRequest(); operation.completed += op => { try { if (ServicesUtils.IsUnityWebRequestReadyForJsonExtract(getProjectsRequest)) { var jsonParser = new JSONParser(getProjectsRequest.downloadHandler.text); var json = jsonParser.Parse(); try { m_ProjectInfoByName = new Dictionary <string, ProjectInfoData>(); var jsonProjects = json.AsDict()[k_JsonProjectsNodeName].AsList(); foreach (var jsonProject in jsonProjects) { if (!jsonProject.AsDict()[k_JsonArchivedNodeName].AsBool()) { var projectInfo = ExtractProjectInfoFromJson(jsonProject); m_ProjectInfoByName.Add(projectInfo.name, projectInfo); } } var projectNames = new List <string> { L10n.Tr(k_SelectProjectText) }; var sortedProjectNames = new List <string>(m_ProjectInfoByName.Keys); sortedProjectNames.Sort(); projectNames.AddRange(sortedProjectNames); projectIdField.choices = projectNames; projectIdField.SetEnabled(true); } catch (Exception ex) { if (exceptionCallback != null) { exceptionCallback.Invoke(ex); } else { //If there is no exception callback, we have to at least log it Debug.LogException(ex); } } } else { var ex = new UnityConnectWebRequestException(L10n.Tr(k_CouldNotObtainProjectMessage)) { error = getProjectsRequest.error, method = getProjectsRequest.method, timeout = getProjectsRequest.timeout, url = getProjectsRequest.url, responseHeaders = getProjectsRequest.GetResponseHeaders(), responseCode = getProjectsRequest.responseCode, isHttpError = (getProjectsRequest.result == UnityWebRequest.Result.ProtocolError), isNetworkError = (getProjectsRequest.result == UnityWebRequest.Result.ConnectionError), }; if (exceptionCallback != null) { exceptionCallback.Invoke(ex); } else { //If there is no exception callback, we have to at least log it Debug.LogException(ex); } } } finally { getProjectsRequest.Dispose(); } }; }); }
void CreateOperationOnCompleted(AsyncOperation obj) { if (m_CurrentRequest == null) { //If we lost our m_CurrentRequest request reference, we can't risk doing anything. return; } if (m_CurrentRequest.result != UnityWebRequest.Result.ProtocolError) { var jsonParser = new JSONParser(m_CurrentRequest.downloadHandler.text); var json = jsonParser.Parse(); var abort = false; try { var projectInfo = ExtractProjectInfoFromJson(json); try { ServicesRepository.DisableAllServices(); BindProject(projectInfo); } catch (Exception ex) { if (exceptionCallback != null) { exceptionCallback.Invoke(ex); abort = true; } else { //If there is no exception callback, we have to at least log it Debug.LogException(ex); } } if (!abort) { createButtonCallback?.Invoke(projectInfo); } } finally { m_CurrentRequest?.Dispose(); m_CurrentRequest = null; } } else if (m_CurrentRequest.responseCode == k_HttpStatusCodeUnprocessableEntity) { m_CurrentRequest?.Dispose(); m_CurrentRequest = null; m_CreateIteration++; RequestCreateOperation(); } else { try { var ex = new UnityConnectWebRequestException(L10n.Tr(k_CouldNotCreateProjectMessage)) { error = m_CurrentRequest.error, method = m_CurrentRequest.method, timeout = m_CurrentRequest.timeout, url = m_CurrentRequest.url, responseHeaders = m_CurrentRequest.GetResponseHeaders(), responseCode = m_CurrentRequest.responseCode, isHttpError = (m_CurrentRequest.result == UnityWebRequest.Result.ProtocolError), isNetworkError = (m_CurrentRequest.result == UnityWebRequest.Result.ConnectionError), }; if (exceptionCallback != null) { exceptionCallback.Invoke(ex); } else { //If there is no exception callback, we have to at least log it Debug.LogException(ex); } } finally { m_CurrentRequest?.Dispose(); m_CurrentRequest = null; } } }
void CreateOperationOnCompleted(AsyncOperation obj) { if (m_CurrentRequest == null) { //If we lost our m_CurrentRequest request reference, we can't risk doing anything. return; } if (ServicesUtils.IsUnityWebRequestReadyForJsonExtract(m_CurrentRequest)) { var jsonParser = new JSONParser(m_CurrentRequest.downloadHandler.text); var json = jsonParser.Parse(); var abort = false; try { var projectInfo = ExtractProjectInfoFromJson(json); try { ServicesRepository.DisableAllServices(shouldUpdateApiFlag: false); //Only register before creation. Remove first in case it was already added. //TODO: Review to avoid dependency on project refreshed UnityConnect.instance.ProjectStateChanged -= OnProjectStateChangedAfterCreation; UnityConnect.instance.ProjectStateChanged += OnProjectStateChangedAfterCreation; BindProject(projectInfo); } catch (Exception ex) { if (exceptionCallback != null) { exceptionCallback.Invoke(ex); abort = true; } else { //If there is no exception callback, we have to at least log it Debug.LogException(ex); } } if (!abort) { createButtonCallback?.Invoke(projectInfo); } } finally { m_CurrentRequest?.Dispose(); m_CurrentRequest = null; } } else if (m_CurrentRequest.responseCode == k_HttpStatusCodeUnprocessableEntity) { m_CurrentRequest?.Dispose(); m_CurrentRequest = null; m_CreateIteration++; RequestCreateOperation(); } else { try { var ex = new UnityConnectWebRequestException(L10n.Tr(k_CouldNotCreateProjectMessage)) { error = m_CurrentRequest.error, method = m_CurrentRequest.method, timeout = m_CurrentRequest.timeout, url = m_CurrentRequest.url, responseHeaders = m_CurrentRequest.GetResponseHeaders(), responseCode = m_CurrentRequest.responseCode, isHttpError = (m_CurrentRequest.result == UnityWebRequest.Result.ProtocolError), isNetworkError = (m_CurrentRequest.result == UnityWebRequest.Result.ConnectionError), }; if (exceptionCallback != null) { exceptionCallback.Invoke(ex); } else { //If there is no exception callback, we have to at least log it Debug.LogException(ex); } } finally { m_CurrentRequest?.Dispose(); m_CurrentRequest = null; } } }
void CreateOperationOnCompleted(AsyncOperation obj) { if (m_CurrentRequest == null) { //If we lost our m_CurrentRequest request reference, we can't risk doing anything. return; } if (m_CurrentRequest.result != UnityWebRequest.Result.ProtocolError) { var jsonParser = new JSONParser(m_CurrentRequest.downloadHandler.text); var json = jsonParser.Parse(); var abort = false; try { var projectInfo = ExtractProjectInfoFromJson(json); try { UnityConnect.instance.BindProject(projectInfo.guid, projectInfo.projectId, projectInfo.organizationId); EditorAnalytics.SendProjectServiceBindingEvent(new ProjectBindManager.ProjectBindState() { bound = true, projectName = projectInfo.name }); NotificationManager.instance.Publish(Notification.Topic.ProjectBind, Notification.Severity.Info, L10n.Tr(k_ProjectLinkSuccessMessage)); } catch (Exception ex) { if (exceptionCallback != null) { exceptionCallback.Invoke(ex); abort = true; } else { //If there is no exception callback, we have to at least log it Debug.LogException(ex); } } if (!abort) { createButtonCallback?.Invoke(projectInfo); } } finally { m_CurrentRequest?.Dispose(); m_CurrentRequest = null; } } else if (m_CurrentRequest.responseCode == k_HttpStatusCodeUnprocessableEntity) { m_CurrentRequest?.Dispose(); m_CurrentRequest = null; m_CreateIteration++; RequestCreateOperation(); } else { try { var ex = new UnityConnectWebRequestException(L10n.Tr(k_CouldNotCreateProjectMessage)) { error = m_CurrentRequest.error, method = m_CurrentRequest.method, timeout = m_CurrentRequest.timeout, url = m_CurrentRequest.url, responseHeaders = m_CurrentRequest.GetResponseHeaders(), responseCode = m_CurrentRequest.responseCode, isHttpError = (m_CurrentRequest.result == UnityWebRequest.Result.ProtocolError), isNetworkError = (m_CurrentRequest.result == UnityWebRequest.Result.ConnectionError), }; if (exceptionCallback != null) { exceptionCallback.Invoke(ex); } else { //If there is no exception callback, we have to at least log it Debug.LogException(ex); } } finally { m_CurrentRequest?.Dispose(); m_CurrentRequest = null; } } }