Skip to content
Snippets Groups Projects
ckmsAkmsClient.go 1.16 KiB
Newer Older
  • Learn to ignore specific revisions
  • Neil-Jocelyn Schark's avatar
    wip
    Neil-Jocelyn Schark committed
    package akmsCkmsClient
    
    import (
    	"bytes"
    	"encoding/json"
    	"fmt"
    	"net/http"
    )
    
    type CkmsAkmsClient struct {
    	url string
    }
    
    func NewCkmsAkmsClient(url string) CkmsAkmsClient {
    	return CkmsAkmsClient{
    		url: url,
    	}
    }
    
    type PushKSAKeyRequest struct {
    	RequestID string   `json:"request_ID"`
    	ProcessID string   `json:"process_ID"`
    	KSAKeys   []KSAKey `json:"ksa_keys"`
    }
    
    type KSAKey struct {
    	KeyID string `json:"key_ID"`
    	Key   string `json:"key"`
    }
    
    func (c *CkmsAkmsClient) SendKSAKeys(requestID string, processID string, ksaKeys []KSAKey) error {
    	pushRequest := PushKSAKeyRequest{
    		RequestID: requestID,
    		ProcessID: processID,
    		KSAKeys:   ksaKeys,
    	}
    
    	jsonData, err := json.Marshal(pushRequest)
    	if err != nil {
    		fmt.Println("Error marshaling JSON:", err)
    		return err
    	}
    
    	resp, err := http.Post(c.url, "application/json", bytes.NewBuffer(jsonData))
    	if err != nil {
    		fmt.Println("Error sending POST request:", err)
    		return err
    	}
    	err = resp.Body.Close()
    	if err != nil {
    		fmt.Println("Error closing response body:", err)
    	}
    
    	if resp.StatusCode != http.StatusNoContent {
    		fmt.Println("Unexpected response status code:", resp.StatusCode)
    		return err
    	}
    
    	return nil
    }