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 StatusRequisicao realizaLogin(string ChaveACS, string loginACS, string senhaACS) { try { //Url de login no ACS. HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.cloud.appcelerator.com/v1/users/login.json?key=" + ChaveACS + "&pretty_json=true"); //O método de login é POST de acordo a documentação da appcelerator. request.Method = "POST"; //Seto o cookie container para manter o valor da sessão. request.CookieContainer = CC; //Serializo os dados do login em um json. ParansLogin info = new ParansLogin(); info.login = loginACS; info.password = senhaACS; 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 = default(Stream); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); //Leio a resposta. string responseFromServer = reader.ReadToEnd(); //Deserializo a resposta. rootACSResponseUsuario resposta = JsonConvert.DeserializeObject <rootACSResponseUsuario>(responseFromServer); var ccs = CC.GetCookies(new Uri("https://api.cloud.appcelerator.com")); //Fecho os streans. reader.Close(); dataStream.Close(); response.Close(); //Retorno sucesso StatusRequisicao ret = new StatusRequisicao(); ret.sucesso = true; return(ret); } catch (WebException ex) { StatusRequisicao ret = new StatusRequisicao(); if (ex.Status == WebExceptionStatus.ProtocolError) { ret.sucesso = false; ret.mensagem = "Erro de protocolo;Cod-1"; return(ret); } else if (ex.Status == WebExceptionStatus.Timeout) { ret.sucesso = false; ret.mensagem = "Time out;Cod-2"; return(ret); } else { ret.sucesso = false; ret.mensagem = "Desconhecido;Cod-3"; return(ret); } throw; } catch (Exception e) { throw; } }
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; } }