Incluído mecanismos para utilizar contas de serviço.

This commit is contained in:
Adriano Serighelli 2025-09-12 18:09:58 -03:00
parent 5e0d281f89
commit 0bbd7466cd
2 changed files with 82 additions and 13 deletions

View File

@ -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<string> GetCardsAsync(int pipeId, DateTime? lastUpdated = null)
public async Task<string> 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();
}
}
}
}

View File

@ -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<BackupContext>()
.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!");
}
}