IEnumerator PostAsync <T>(UnityTask <StorageResponse <T> > task, string operation, string body) { // Confirm Endpoint URI if (EnpointUri == null) { var urlTask = new UnityTask(TaskStrategy.Custom); yield return(TaskManager.StartRoutine(GetUrlFromCluster(urlTask))); if (EnpointUri == null) { task.Exception = new Exception("Failed to get cluster url"); task.Status = TaskStatus.Faulted; yield break; } } var url = EnpointUri.ToString().EndsWith("/") ? EnpointUri + operation : EnpointUri + "/" + operation; var www = Client.PostAsync(url, body); yield return(TaskManager.StartRoutine(www.WaitRoutine())); if (www.IsFaulted) { task.Exception = www.Exception; task.Status = TaskStatus.Faulted; yield break; } Log(www.Content); task.Result = JsonMapper.ToObject <StorageResponse <T> >(www.Content); task.Status = TaskStatus.Success; }
IEnumerator EnablePresenceAsync(UnityTask <string> task) { var btask = BalancerClient.GetServerUrl(Url, IsCluster, ApplicationKey); yield return(TaskManager.StartRoutine(btask.WaitRoutine())); if (btask.IsFaulted) { task.Exception = btask.Exception; task.Status = TaskStatus.Faulted; yield break; } var server = btask.Result; var presenceUrl = String.IsNullOrEmpty(server) ? server : server[server.Length - 1] == '/' ? server : server + "/"; presenceUrl = String.Format("{0}presence/enable/{1}/{2}", presenceUrl, ApplicationKey, Channel); var content = String.Format("privatekey={0}", PrivateKey); if (MetaData) { content = String.Format("{0}&metadata=1", content); } var htask = HttpClient.PostAsync(presenceUrl, content); yield return(TaskManager.StartRoutine(htask.WaitRoutine())); if (htask.IsFaulted) { task.Exception = htask.Exception; task.Status = TaskStatus.Faulted; yield break; } task.Result = htask.Content; }
IEnumerator SendMessageAsync(UnityTask <bool> task) { var connectionUrl = Url; if (IsCluster) { var cTask = ClusterClient.GetClusterServerWithRetry(Url, ApplicationKey); yield return(TaskManager.StartRoutine(cTask.WaitRoutine())); if (cTask.IsFaulted) { task.Exception = cTask.Exception; task.Status = TaskStatus.Faulted; yield break; } connectionUrl = cTask.Result; } connectionUrl = connectionUrl.Last() == '/' ? connectionUrl : connectionUrl + "/"; var postParameters = String.Format("AT={0}&AK={1}&PK={2}&C={3}&M={4}", AuthenticationToken, ApplicationKey, PrivateKey, Channel, HttpUtility.UrlEncode(Message)); var hTask = Client.PostAsync(String.Format("{0}send", connectionUrl), postParameters); yield return(TaskManager.StartRoutine(hTask.WaitRoutine())); if (hTask.IsFaulted) { task.Exception = hTask.Exception; task.Status = TaskStatus.Faulted; yield break; } task.Result = hTask.StatusCode == HttpStatusCode.Created; }
IEnumerator PostAuthenticationAsync(UnityTask <bool> task) { var connectionUrl = Url; if (IsCluster) { var cTask = ClusterClient.GetClusterServerWithRetry(Url, ApplicationKey); yield return(TaskManager.StartRoutine(cTask.WaitRoutine())); if (cTask.IsFaulted) { task.Exception = cTask.Exception; task.Status = TaskStatus.Faulted; yield break; } connectionUrl = cTask.Result; } connectionUrl = connectionUrl[connectionUrl.Length - 1] == '/' ? connectionUrl : connectionUrl + "/"; var postParameters = String.Format("AT={0}&PVT={1}&AK={2}&TTL={3}&PK={4}", AuthenticationToken, AuthenticationTokenIsPrivate ? 1 : 0, ApplicationKey, TimeToLive, PrivateKey); if (Permissions != null && Permissions.Count > 0) { postParameters += String.Format("&TP={0}", Permissions.Count); foreach (var permission in Permissions) { var permissionItemText = String.Format("{0}=", permission.Key); var list = new List <ChannelPermissions>(permission.Value); foreach (var permissionItemValue in list) { permissionItemText += ((char)permissionItemValue).ToString(); } postParameters += String.Format("&{0}", permissionItemText); } } var hTask = Client.PostAsync(String.Format("{0}authenticate", connectionUrl), postParameters); yield return(TaskManager.StartRoutine(hTask.WaitRoutine())); if (hTask.IsFaulted) { task.Exception = hTask.Exception; task.Status = TaskStatus.Faulted; yield break; } if (hTask.StatusCode == HttpStatusCode.Unauthorized) { task.Exception = new OrtcException(OrtcExceptionReason.Unauthorized, String.Format("Unable to connect to the authentication server {0}.", connectionUrl)); task.Status = TaskStatus.Faulted; yield break; } task.Result = hTask.IsSuccess; }