using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AiAgent.Core.Infrastructure { public class GitHubGistHelper { // Use your actual token and username private static string GitHubToken = "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"; //private const string GitHubUsername = "fredericfesty@gmail.com"; private static HttpClient GetClient() { HttpClient client = new(); // Setup HttpClient with necessary headers for the GitHub API client.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json"); client.DefaultRequestHeaders.Add("Authorization", $"Bearer {GitHubToken}"); // GitHub API requires a User-Agent header client.DefaultRequestHeaders.Add("User-Agent", "NewsAI"); return client; } public static async Task Test(string[] args) { // 1. CREATE a new Gist Console.WriteLine("Creating a new Gist..."); string? newGistId = await CreateGistAsync("my_document.txt", "Hello from C#!"); if (string.IsNullOrEmpty(newGistId)) { Console.WriteLine("Failed to create Gist."); return; } Console.WriteLine($"Gist created with ID: {newGistId}\n"); // 2. READ the content of the Gist we just created Console.WriteLine("Reading the Gist content..."); string? content = await ReadGistContentAsync(newGistId, "my_document.txt"); Console.WriteLine($"Read content: '{content}'\n"); // 3. UPDATE (write to) the existing Gist Console.WriteLine("Updating the Gist..."); await UpdateGistAsync(newGistId, "my_document.txt", "The content has now been updated!"); // 4. READ again to confirm the update Console.WriteLine("Reading the Gist content after update..."); string? updatedContent = await ReadGistContentAsync(newGistId, "my_document.txt"); Console.WriteLine($"Updated content: '{updatedContent}'"); } // CREATE operation public static async Task CreateGistAsync(string filename, string content) { var gistData = new { description = "A text file stored via C#", @public = false, // Use 'false' for secret gists, 'true' for public files = new Dictionary { [filename] = new { content } } }; string jsonPayload = System.Text.Json.JsonSerializer.Serialize(gistData); var httpContent = new StringContent(jsonPayload, Encoding.UTF8, "application/json"); var client = GetClient(); HttpResponseMessage response = await client.PostAsync("https://api.github.com/gists", httpContent); if (!response.IsSuccessStatusCode) return null; var jsonResponse = System.Text.Json.JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync()); return jsonResponse?["id"]?.ToString(); } // READ operation public static async Task ReadGistContentAsync(string gistId, string filename) { for (int tries = 0; tries < 10; tries++) { try { var client = GetClient(); HttpResponseMessage response = await client.GetAsync($"https://api.github.com/gists/{gistId}"); if (!response.IsSuccessStatusCode) return null; var jsonResponse = System.Text.Json.JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync()); var result = jsonResponse?["files"]?[filename]?["content"]?.ToString(); return result; } catch (Exception ex) { Console.WriteLine($"Attempt {tries + 1} failed: {ex.Message}"); await Task.Delay(3000); // Wait before retrying } } return null; } // UPDATE (write) operation public static async Task UpdateGistAsync(string gistId, string filename, string newContent) { var gistUpdateData = new { files = new Dictionary { [filename] = new { content = newContent } } }; string jsonPayload = System.Text.Json.JsonSerializer.Serialize(gistUpdateData); var httpContent = new StringContent(jsonPayload, Encoding.UTF8, "application/json"); // GitHub uses the PATCH method for updates var client = GetClient(); HttpResponseMessage response = await client.PatchAsync($"https://api.github.com/gists/{gistId}", httpContent); response.EnsureSuccessStatusCode(); } } }