From 0bbd7466cd4152f9dfefb4d2e0fe7156f05a52d5 Mon Sep 17 00:00:00 2001 From: Adriano Serighelli Date: Fri, 12 Sep 2025 18:09:58 -0300 Subject: [PATCH] =?UTF-8?q?Inclu=C3=ADdo=20mecanismos=20para=20utilizar=20?= =?UTF-8?q?contas=20de=20servi=C3=A7o.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/PipefyClient.cs | 53 +++++++++++++++---- BackupPipefy.Presentation/Program.cs | 42 ++++++++++++++- 2 files changed, 82 insertions(+), 13 deletions(-) diff --git a/BackupPipefy.Infrastructure/Services/PipefyClient.cs b/BackupPipefy.Infrastructure/Services/PipefyClient.cs index e99e4a1..a59724d 100644 --- a/BackupPipefy.Infrastructure/Services/PipefyClient.cs +++ b/BackupPipefy.Infrastructure/Services/PipefyClient.cs @@ -1,22 +1,53 @@ -namespace BackupPipefy.Infrastructure.Services +using System; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Net.Security; +using System.Security.Cryptography.X509Certificates; +using System.Threading.Tasks; + +namespace BackupPipefy.Infrastructure.Services { public class PipefyClient { private readonly HttpClient _httpClient; + private readonly PipefyTokenService _tokenService; - public PipefyClient(string apiToken) - { - _httpClient = new HttpClient(); - _httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiToken}"); + public PipefyClient(PipefyTokenService tokenService, bool useProxy = false) + { + if (useProxy) + { + var handler = new HttpClientHandler + { + Proxy = new WebProxy("127.0.0.1", 8888), + UseProxy = true, + ServerCertificateCustomValidationCallback = (HttpRequestMessage req, X509Certificate2? cert, X509Chain? chain, SslPolicyErrors errors) => true + }; + + _httpClient = new HttpClient(handler); + } + else + { + _httpClient = new HttpClient(); + } + + _tokenService = tokenService; } - public async Task GetCardsAsync(int pipeId, DateTime? lastUpdated = null) + public async Task GetCardsAsync(int pipeId, DateTime lastUpdated) { - // Aqui você vai montar sua query GraphQL real - string query = "{ allCards { edges { node { id updated_at } } } }"; + var token = await _tokenService.GetTokenAsync(); + _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); + + // Montar GraphQL e enviar request + // Exemplo simplificado: + var query = new + { + query = @"{ allCards(pipeId: 303718996, after: \""WyIxLjAiLCI1MC4wIiw4MTgzNjgwNzhd\""){ edges{ node{ id title emailMessagingAddress } } pageInfo{ endCursor hasNextPage } }}" + }; - var payload = new { query }; - var content = new StringContent(System.Text.Json.JsonSerializer.Serialize(payload), System.Text.Encoding.UTF8, "application/json"); + var content = new StringContent(System.Text.Json.JsonSerializer.Serialize(query)); + content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); var response = await _httpClient.PostAsync("https://api.pipefy.com/graphql", content); response.EnsureSuccessStatusCode(); @@ -24,4 +55,4 @@ return await response.Content.ReadAsStringAsync(); } } -} \ No newline at end of file +} diff --git a/BackupPipefy.Presentation/Program.cs b/BackupPipefy.Presentation/Program.cs index 3751555..44e1d5e 100644 --- a/BackupPipefy.Presentation/Program.cs +++ b/BackupPipefy.Presentation/Program.cs @@ -1,2 +1,40 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +using BackupPipefy.Infrastructure.Data; +using BackupPipefy.Infrastructure.Services; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; + +class Program +{ + static async Task Main(string[] args) + { + var builder = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional: false); + + var config = builder.Build(); + + string connStr = config["Database:ConnectionString"]; + int pipeId = int.Parse(config["Pipefy:PipeId"]); + int requestsPerMinute = int.Parse(config["Pipefy:RequestsPerMinute"]); + string clientId = config["Pipefy:ClientId"]; + string clientSecret = config["Pipefy:ClientSecret"]; + string tokenUrl = config["Pipefy:TokenUrl"]; + string personalAccessToken = config["Pipefy:PersonalAccessToken"]; + + var options = new DbContextOptionsBuilder() + .UseNpgsql(connStr) + .Options; + + using var context = new BackupContext(options); + context.Database.Migrate(); + + var tokenService = new PipefyTokenService(clientId, clientSecret, tokenUrl, useProxy: true, personalAccessToken); + var pipefyClient = new PipefyClient(tokenService, useProxy: true); + + var service = new BackupService(pipefyClient, context, requestsPerMinute); + + await service.RunBackup(pipeId); + + Console.WriteLine("Backup concluído!"); + } +} \ No newline at end of file