private static void MontaExceptionDesconhecido(ref rootACSResponse resposta) { resposta.meta = new InfoBasicoACS(); resposta.meta.code = -3; resposta.meta.status = "Fail"; resposta.meta.method_name = "Desconhecido"; }
private static void MontaException400(ref rootACSResponse resposta) { resposta.meta = new InfoBasicoACS(); resposta.meta.code = -2; resposta.meta.status = "Fail"; resposta.meta.method_name = "Exception400"; }
public static async System.Threading.Tasks.Task <rootACSResponseUsuario> checkExists(string email) { try { try { var resposta = new rootACSResponseUsuario(); using (var handler = new HttpClientHandler() { CookieContainer = CC }) using (var client = new HttpClient(handler)) { HttpResponseMessage response = await client.GetAsync("https://api.cloud.appcelerator.com/v1/users/query.json?key=" + ChaveACS + "&pretty_json=true" + "&limit=1&skip=0&where=" + HttpUtility.UrlEncode("{\"email\":\"" + email + "\"}")); response.EnsureSuccessStatusCode(); resposta = JsonConvert.DeserializeObject <rootACSResponseUsuario>(await response.Content.ReadAsStringAsync()); } return(resposta); } catch (Exception e) { throw; } } catch (WebException ex) { rootACSResponseUsuario resposta = new rootACSResponseUsuario(); rootACSResponse aux = resposta; if (ex.Status == WebExceptionStatus.ProtocolError) { MontaException400(ref aux); } else { MontaExceptionDesconhecido(ref aux); } resposta = (rootACSResponseUsuario)aux; return(resposta); } catch (Exception) { throw; } }
public static async System.Threading.Tasks.Task <rootACSResponseUsuario> getUser(string idUsuario) { try { try { var resposta = new rootACSResponseUsuario(); using (var handler = new HttpClientHandler() { CookieContainer = CC }) using (var client = new HttpClient(handler)) { HttpResponseMessage response = await client.GetAsync("https://api.cloud.appcelerator.com/v1/users/show.json?key=" + ChaveACS + "&pretty_json=true&response_json_depth=2&user_id=" + idUsuario); response.EnsureSuccessStatusCode(); resposta = JsonConvert.DeserializeObject <rootACSResponseUsuario>(await response.Content.ReadAsStringAsync()); } //Devolvo o Id do usuário da resposta. return(resposta); } catch (Exception e) { throw; } } catch (WebException ex) { rootACSResponseUsuario resposta = new rootACSResponseUsuario(); rootACSResponse aux = resposta; if (ex.Status == WebExceptionStatus.ProtocolError) { MontaException400(ref aux); } else { MontaExceptionDesconhecido(ref aux); } resposta = (rootACSResponseUsuario)aux; return(resposta); } catch (Exception) { throw; } }
private static void checkLogin(ref rootACSResponse root) { root.meta = new InfoBasicoACS(); root.meta.code = -1; root.meta.method_name = "checkLogin"; root.meta.status = "Fail"; if (!statusLogin) { contadortentativa = contadortentativa + 1; dynamic t = new Thread(TentativaLogin); t.Start(); } else { root.meta.code = 0; root.meta.status = "OK"; } }
public static rootACSResponseUsuario DeleteUser(string idUsuario) { try { rootACSResponseUsuario resposta = new rootACSResponseUsuario(); rootACSResponse aux = resposta; //Verifico se o admin está logado. checkLogin(ref aux); resposta = (rootACSResponseUsuario)aux; //Se não estiver retorno o código de erro -1. if (resposta.meta.code != 0) { return(resposta); } HttpWebRequest request = default(HttpWebRequest); //Url de busca de usuário no ACS. request = (HttpWebRequest)WebRequest.Create("https://api.cloud.appcelerator.com/v1/users/delete.json?key=" + ChaveACS + "&pretty_json=true" + "&su_id=" + idUsuario); //Seto o cookie container para manter o valor da sessão. request.CookieContainer = CC; //O método de busca é GET de acordo a documentação da appcelerator. request.Method = "GET"; // Seto o ContentType para json, já que os dados no POST estão no formato JSON. request.ContentType = "application/json"; //Obtenho a resposta da requisição HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream dataStream = default(Stream); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); //Leio a resposta. string responseFromServer = reader.ReadToEnd(); //Deserializo a resposta. resposta = JsonConvert.DeserializeObject <rootACSResponseUsuario>(responseFromServer); //Fecho os streans. reader.Close(); dataStream.Close(); response.Close(); //Devolvo o Id do usuário da resposta. return(resposta); } catch (WebException ex) { rootACSResponseUsuario resposta = new rootACSResponseUsuario(); rootACSResponse aux = resposta; if (ex.Status == WebExceptionStatus.ProtocolError) { MontaException400(ref aux); } else { MontaExceptionDesconhecido(ref aux); } resposta = (rootACSResponseUsuario)aux; return(resposta); } catch (Exception) { throw; } }
public static async System.Threading.Tasks.Task <rootACSResponseUsuario> AlteraUser(InfoUsuarioACS parans) { try { rootACSResponseUsuario resposta = new rootACSResponseUsuario(); rootACSResponse aux = resposta; //Verifico se o admin está logado. checkLogin(ref aux); resposta = (rootACSResponseUsuario)aux; //Se não estiver retorno o código de erro -1. if (resposta.meta.code != 0) { return(resposta); } HttpContent stringUsername = null; if (parans.email != null) { stringUsername = new ByteArrayContent(Encoding.UTF8.GetBytes(parans.email)); } HttpContent stringNome = new ByteArrayContent(Encoding.UTF8.GetBytes(parans.first_name)); HttpContent stringSobrenome = new ByteArrayContent(Encoding.UTF8.GetBytes(parans.last_name)); HttpContent stringSenha = null; HttpContent stringSenha02 = null; if (parans.password != null) { stringSenha = new ByteArrayContent(Encoding.UTF8.GetBytes(parans.password)); stringSenha02 = new ByteArrayContent(Encoding.UTF8.GetBytes(parans.password_confirmation)); } HttpContent stringId = new ByteArrayContent(Encoding.UTF8.GetBytes(parans.id)); HttpContent photoContent = null; if (parans.photo != null) { photoContent = new ByteArrayContent(parans.photo); photoContent.Headers.Add("Content-Type", "image/png"); photoContent.Headers.Add("Content-Disposition", "form-data; name=\"photo\"; filename=\"imagem.png\""); } using (var handler = new HttpClientHandler() { CookieContainer = CC }) using (var client = new HttpClient(handler)) { using (var formData = new MultipartFormDataContent()) { if (stringUsername != null) { formData.Add(stringUsername, string.Format("\"{0}\"", "email")); } formData.Add(stringNome, string.Format("\"{0}\"", "first_name")); formData.Add(stringSobrenome, string.Format("\"{0}\"", "last_name")); if (parans.password != null) { formData.Add(stringSenha, string.Format("\"{0}\"", "password_confirmation")); formData.Add(stringSenha02, string.Format("\"{0}\"", "password")); } formData.Add(stringId, string.Format("\"{0}\"", "su_id")); if (photoContent != null) { formData.Add(photoContent, string.Format("\"{0}\"", "photo"), string.Format("\"{0}\"", "imagem.png")); } HttpResponseMessage response = await client.PostAsync("https://api.cloud.appcelerator.com/v1/users/update.json?key=" + ChaveACS + "&pretty_json=true", formData); response.EnsureSuccessStatusCode(); resposta = JsonConvert.DeserializeObject <rootACSResponseUsuario>(await response.Content.ReadAsStringAsync()); } } //Devolvo o Id do usuário da resposta. return(resposta); } catch (WebException ex) { rootACSResponseUsuario resposta = new rootACSResponseUsuario(); rootACSResponse aux = resposta; if (ex.Status == WebExceptionStatus.ProtocolError) { MontaException400(ref aux); } else { MontaExceptionDesconhecido(ref aux); } resposta = (rootACSResponseUsuario)aux; return(resposta); } catch (Exception) { throw; } }
public static rootACSResponseNotificacao EnviarNotificacao(string canal, object Dados, string clientes = "everyone", string Mensagem = "", string Servico = "") { try { rootACSResponseNotificacao resposta = new rootACSResponseNotificacao(); rootACSResponse aux = resposta; //Verifico se o admin está logado. checkLogin(ref aux); resposta = (rootACSResponseNotificacao)aux; //Se não estiver retorno o código de erro -1. if (resposta.meta.code != 0) { return(resposta); } HttpWebRequest request = default(HttpWebRequest); //Url de notificação no ACS. request = (HttpWebRequest)WebRequest.Create("https://api.cloud.appcelerator.com/v1/push_notification/notify.json?key=" + ChaveACS); //Seto o cookie container para manter o valor da sessão. request.CookieContainer = CC; //O método de notificação é POST de acordo a documentação da appcelerator. request.Method = "POST"; //Serializo os dados da notificacao em um json. ParametrosNotificacao info = new ParametrosNotificacao(); //Seto o canal da notificação. info.channel = canal; info.options = new OptionsNotificacao(); //A notificação expira após 1 dia. info.options.expire_after_seconds = 86400; //Envio os dados para os usuários selecionados, por padrão envia para todo mundo. info.to_ids = clientes; //Monto os dados de configuração da notificação. dynamic payload = new PayLoad(); payload.alert = Mensagem; payload.dados = Dados; payload.mensagem = Mensagem; if (!string.IsNullOrEmpty(Servico)) { payload.servico = Servico; } payload.title = "Britec"; info.payload = payload; string postData = JsonConvert.SerializeObject(info); byte[] byteArray = Encoding.UTF8.GetBytes(postData); // Seto o ContentType para json, já que os dados no POST estão no formato JSON. request.ContentType = "application/json"; //Preparo os dados request.ContentLength = byteArray.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); //Obtenho a resposta da requisição HttpWebResponse response = (HttpWebResponse)request.GetResponse(); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); //Leio a resposta. string responseFromServer = reader.ReadToEnd(); //Deserializo a resposta. resposta = JsonConvert.DeserializeObject <rootACSResponseNotificacao>(responseFromServer); //Fecho os streans. reader.Close(); dataStream.Close(); response.Close(); //Retorno a resposta do serviço return(resposta); } catch (WebException ex) { rootACSResponseNotificacao resposta = new rootACSResponseNotificacao(); rootACSResponse aux = resposta; if (ex.Status == WebExceptionStatus.ProtocolError) { MontaException400(ref aux); } else { MontaExceptionDesconhecido(ref aux); } resposta = (rootACSResponseNotificacao)aux; return(resposta); } catch (Exception) { throw; } }