New API

 using System;

using System.Collections.Generic;

using System.Net.Http;

using System.Net.Http.Headers;

using System.IO;

using System.Text.Json;

using System.Threading.Tasks;


class Program

{

    private static readonly HttpClient client = new HttpClient();


    static async Task Main(string[] args)

    {

        string token = "your_token_here"; // 🔐 Replace with your actual token

        string baseUrl = "https://api-stage.marmin.ai";

        string createdDate = "2025-03-03";


        // 🛡️ Add Authorization header

        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);


        string idsUrl = $"{baseUrl}/api/invoices/ids?created_on={createdDate}";


        try

        {

            // 1) Get array of IDs

            var response = await client.GetAsync(idsUrl);

            response.EnsureSuccessStatusCode();


            var jsonString = await response.Content.ReadAsStringAsync();

            var idList = JsonSerializer.Deserialize<List<string>>(jsonString);


            foreach (var id in idList)

            {

                Console.WriteLine($"Processing ID: {id}");


                // 2) Call 2nd API for details

                string detailUrl = $"{baseUrl}/api/docs/invoice/{id}";

                var detailResponse = await client.GetAsync(detailUrl);

                detailResponse.EnsureSuccessStatusCode();


                var detailContent = await detailResponse.Content.ReadAsStringAsync();

                Console.WriteLine($"Details for {id}: {detailContent}");


                // 3) Call 3rd API to download PDF

                string pdfUrl = $"{baseUrl}/api/invoices/{id}/download-pdf";

                var pdfResponse = await client.GetAsync(pdfUrl);

                pdfResponse.EnsureSuccessStatusCode();


                var pdfBytes = await pdfResponse.Content.ReadAsByteArrayAsync();


                string outputPath = Path.Combine("Downloads", $"{id}.pdf");

                Directory.CreateDirectory("Downloads");

                await File.WriteAllBytesAsync(outputPath, pdfBytes);


                Console.WriteLine($"PDF saved: {outputPath}");

            }

        }

        catch (Exception ex)

        {

            Console.WriteLine($"Error occurred: {ex.Message}");

        }

    }

}

......

[HttpGet("check-last-modified")]

public IActionResult CheckLastModifiedFile()

{

    string folderPath = @"C:\Your\Folder\Path";

    var fileInfo = GetLastModifiedFile(folderPath);


    if (fileInfo == null)

        return NotFound("No files found in the folder.");


    // Example: store/retrieve the last checked time from a DB or config

    DateTime lastChecked = DateTime.Now.AddMinutes(-10); // Example


    if (IsFileModifiedSince(fileInfo, lastChecked))

    {

        return Ok($"File '{fileInfo.Name}' was modified after last check.");

    }


    return Ok("No new modification detected.");

}

.....

public bool IsFileModifiedSince(FileInfo fileInfo, DateTime lastCheckedTime)

{

    return fileInfo.LastWriteTime > lastCheckedTime;

}


Comments

Popular posts from this blog

String Program in C#

CSV using XmlNode

Controller_Model. cs