// validation_rest_min.go
// A minimal GO program to test the FirmaSAT Validation REST API

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io"
    "log"
    "net/http"
)

func main() {
    const API_TOKEN = "YOUR_API_TOKEN_HERE"
    const API_URL = "https://di-mgt.com.au/cryptosys/api/fsa_validation"

    // 1. Define some  XML content
    // As a test, send some well-formed XML that is not a valid SAT CFDi document
    // This should return a status "partialsuccess" with an xml-error
    // "XML file is not a supported document."

    xmlContent := `<?xml version="1.0"?><data><item>value</item></data>`

    // 2. Create a JSON payload containing the XML string
    payload := map[string]string{
        "api-token": API_TOKEN,
        "xmldata":   xmlContent,
    }

    // 3. Marshal the payload into JSON bytes
    jsonData, err := json.Marshal(payload)
    if err != nil {
        log.Fatal(err)
    }

    // 4. Create the HTTP POST request
    // Set Content-Type to application/json
    req, err := http.NewRequest("POST", API_URL, bytes.NewBuffer(jsonData))
    if err != nil {
        log.Fatal(err)
    }
    req.Header.Set("Content-Type", "application/json")

    // 5. Execute the request
    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        log.Fatal(err)
    }
    defer resp.Body.Close()

    // Read the response body
    body, err := io.ReadAll(resp.Body)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Status: %s\n", resp.Status)
    fmt.Printf("Response body:\n%s\n", string(body))
}