public static string Main(FichajeDTO fichaje) { Management(); Task <string> response = CheckRowById(id, token, session, fichaje); response.Wait(); return(response.Result); }
private static async Task <string> UpdateRow(string idfile, string token, string session, FichajeDTO fichaje, int index) { using (HttpClient client = new HttpClient()) { var uri = $"https://graph.microsoft.com/v1.0/me/drive/items/{idfile}/workbook/tables('{table}')/rows/" + "/ItemAt(index=" + index + ")"; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); client.DefaultRequestHeaders.Add("workbook-session-id", session); client.DefaultRequestHeaders.Add("Accept", "application/json"); string body = "{ \"values\": [ [ \"" + fichaje.id + "\", \"" + fichaje.dni + "\", \"" + fichaje.nombre + "\"," + "\"" + fichaje.fechaentrada + "\", \"" + fichaje.horaentrada + "\"," + "\"" + fichaje.fechasalida + "\",\"" + fichaje.horasalida + "\"," + "\"" + fichaje.horastrabajadas + "\" ] ], \"index\": " + index + " }"; var httpRequestMessage = new HttpRequestMessage(new HttpMethod("PATCH"), uri); httpRequestMessage.Content = new StringContent(body); var response = await client.SendAsync(httpRequestMessage); string content = null; if (response.Content != null) { content = await response.Content.ReadAsStringAsync(); } return(content); } }
private static async Task <string> CheckRowById(string idfile, string token, string session, FichajeDTO fichaje) { using (HttpClient client = new HttpClient()) { var uri = $"https://graph.microsoft.com/v1.0/me/drive/items/{idfile}/workbook/tables('{table}')/rows"; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); client.DefaultRequestHeaders.Add("workbook-session-id", session); httpclient.DefaultRequestHeaders.Add("Accept", "application/json"); var response = await client.GetAsync(uri); if (response.Content != null) { var jsonresult = Newtonsoft.Json.Linq.JObject.Parse(await response.Content.ReadAsStringAsync()); var jarray = (Newtonsoft.Json.Linq.JArray)jsonresult["value"]; int index = FindRowById(jarray, fichaje.id); if (index != -1) { return(await UpdateRow(idfile, token, session, fichaje, index)); } else { return(await InsertRow(idfile, token, session, fichaje)); } } return("Error en la petición"); } }
private static async Task <string> InsertRow(string idfile, string token, string session, FichajeDTO fichaje) { using (HttpClient client = new HttpClient()) { var uri = $"https://graph.microsoft.com/v1.0/me/drive/items/{idfile}/workbook/tables('{table}')/rows"; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); client.DefaultRequestHeaders.Add("workbook-session-id", session); httpclient.DefaultRequestHeaders.Add("Accept", "application/json"); string body = "{ \"values\": [ [ \"" + fichaje.id + "\", \"" + fichaje.dni + "\", \"" + fichaje.nombre + "\"," + "\"" + fichaje.fechaentrada + "\", \"" + fichaje.horaentrada + "\"," + "\"" + fichaje.fechasalida + "\",\"" + fichaje.horasalida + "\"," + "\"" + fichaje.horastrabajadas + "\" ] ], \"index\": null }"; var response = await client.PostAsync(uri, new StringContent(body, Encoding.UTF8, "application/json")); string content = null; if (response.Content != null) { content = await response.Content.ReadAsStringAsync(); } return(content); } }