using Newtonsoft.Json.Linq; using Newtonsoft.Json; using Pipefy.Models; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Collections.Generic; namespace Pipefy.Services { public class PipefyApiService : IPipefyApiService { private readonly string _apiUrl; private readonly string _token; public PipefyApiService(string apiUrl, string token) { _apiUrl = apiUrl; _token = token; } public async Task GetRecordsAsync(string tableId) { JArray allRecords = new JArray(); string cursor = "null"; string query = $"{{\"query\":\"query GetRecords($cursor: String){{ table_records(table_id: \\\"{tableId}\\\",first:50,after:$cursor){{ pageInfo{{ hasNextPage endCursor }} edges{{ node{{ id record_fields{{ field {{ id }} value array_value }} }} }} }}}}\",\"variables\":{{\"cursor\":{cursor}}}}}"; bool hasNextPage = true; while (hasNextPage) { var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + _token); var request = new HttpRequestMessage(HttpMethod.Post, _apiUrl); request.Headers.Add("Accept", "application/json"); var content = new StringContent(query, null, "application/json"); request.Content = content; var response = await httpClient.SendAsync(request); if (response.IsSuccessStatusCode) { var responseContent = await response.Content.ReadAsStringAsync(); var responseData = JObject.Parse(responseContent); var records = responseData["data"]!["table_records"]!["edges"]; foreach (var record in records!) { allRecords.Add(record); } hasNextPage = responseData["data"]!["table_records"]!["pageInfo"]!["hasNextPage"]!.Value(); cursor = responseData["data"]!["table_records"]!["pageInfo"]!["endCursor"]!.Value()!; query = $"{{\"query\":\"query GetRecords($cursor: String){{ table_records(table_id: \\\"{tableId}\\\",first:50,after:$cursor){{ pageInfo{{ hasNextPage endCursor }} edges{{ node{{ record_fields{{ field {{ id }} value array_value }} }} }} }}}}\",\"variables\":{{\"cursor\":\"{cursor}\"}}}}"; } else { allRecords.Clear(); return allRecords; } } return allRecords; } public async Task GetGestoresAsync(string tableId) { // For now, this is the same as GetRecordsAsync, but can be customized if needed return await GetRecordsAsync(tableId); } public async Task CreateRecordsAsync(string tableId, List records) { // This method should build the GraphQL mutation and send it in batches // For brevity, this is a simplified version if (records == null || records.Count == 0) return true; string strQuery = "{\"query\":\"mutation {\\r\\n "; for (int i = 0; i < records.Count; i++) { if (i % 49 == 0) { strQuery = "{\"query\":\"mutation {\\r\\n "; } strQuery = strQuery + $"N{i}: createTableRecord(input: {{ table_id: \\\"{tableId}\\\", fields_attributes: [ {{ field_id: \\\"nome_da_empresa\\\", field_value: \\\"{records[i].nome_da_empresa}\\\" }} {{ field_id: \\\"c_digo_smart\\\", field_value: \\\"{records[i].c_digo_smart}\\\" }} {{ field_id: \\\"modalidade\\\", field_value: \\\"{records[i].modalidade}\\\" }} {{ field_id: \\\"gestores\\\", field_value: \\\"{records[i].gestores}\\\" }}]}}) {{ table_record {{ id }}}}\\r\\n "; if (i % 48 == 0 && i != 0) { strQuery = strQuery + "}\",\"variables\":{}}"; var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + _token); var request = new HttpRequestMessage(HttpMethod.Post, _apiUrl); request.Headers.Add("Accept", "application/json"); var content = new StringContent(strQuery, null, "application/json"); request.Content = content; var response = await httpClient.SendAsync(request); } } strQuery = strQuery + "}\",\"variables\":{}}"; var finalClient = new HttpClient(); finalClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + _token); var finalRequest = new HttpRequestMessage(HttpMethod.Post, _apiUrl); finalRequest.Headers.Add("Accept", "application/json"); var finalContent = new StringContent(strQuery, null, "application/json"); finalRequest.Content = finalContent; var finalResponse = await finalClient.SendAsync(finalRequest); return finalResponse.IsSuccessStatusCode; } } }