2023-05-29 17:58:23 -04:00
package data
import (
2023-06-09 23:52:03 -04:00
"bytes"
2023-06-09 01:09:02 -04:00
"encoding/json"
"errors"
2023-05-29 17:58:23 -04:00
"fmt"
"io"
2023-06-09 01:09:02 -04:00
"io/ioutil"
2023-05-29 17:58:23 -04:00
"net/http"
2023-06-09 01:09:02 -04:00
2023-06-09 23:52:03 -04:00
"git.agecem.com/agecem/bottin-agenda/models"
2023-06-09 01:09:02 -04:00
"git.agecem.com/agecem/bottin-agenda/responses"
2023-05-29 17:58:23 -04:00
)
type ApiClient struct {
Key string
Host string
Port int
Protocol string
}
func NewApiClient ( key , host , protocol string , port int ) * ApiClient {
return & ApiClient {
Key : key ,
Host : host ,
Port : port ,
Protocol : protocol ,
}
}
func ( a * ApiClient ) Call ( method , route string , requestBody io . Reader , useKey bool ) ( * http . Response , error ) {
var response * http . Response
endpoint := fmt . Sprintf ( "%s://%s:%d%s" ,
a . Protocol , a . Host , a . Port , route ,
)
// Create client
client := & http . Client { }
// Create request
request , err := http . NewRequest ( method , endpoint , requestBody )
if err != nil {
return response , err
}
if useKey {
if a . Key == "" {
2023-06-10 00:07:51 -04:00
return response , fmt . Errorf ( "L'appel au serveur API requiert une clé, mais aucune n'était donnée. Voir --help pour les instructions sur comment fournir une clé API." )
2023-05-29 17:58:23 -04:00
}
request . Header . Add ( "Authorization" , fmt . Sprintf ( "Bearer %s" , a . Key ) )
}
if requestBody != nil {
request . Header . Add ( "Content-Type" , "application/json" )
}
// Fetch Request
response , err = client . Do ( request )
if err != nil {
return response , err
}
return response , nil
}
2023-06-09 01:09:02 -04:00
// GetHealth allows checking for API server health
func ( a * ApiClient ) GetHealth ( ) ( string , error ) {
var response responses . GetHealthResponse
getHealthResponse , err := a . Call ( http . MethodGet , "/v3/health" , nil , true )
if err != nil {
return response . Message , err
}
defer getHealthResponse . Body . Close ( )
body , err := ioutil . ReadAll ( getHealthResponse . Body )
if err != nil {
return response . Message , err
}
if err := json . Unmarshal ( body , & response ) ; err != nil {
return response . Message , err
}
if response . Message == "" {
2023-06-10 00:07:51 -04:00
return response . Message , errors . New ( "Impossible de confirmer l'état du serveur API." )
2023-06-09 01:09:02 -04:00
}
return response . Message , nil
}
2023-06-09 23:52:03 -04:00
func ( a * ApiClient ) InsertTransactions ( transactions [ ] models . Transaction ) ( [ ] models . Transaction , error ) {
var response responses . PostTransactionsResponse
var buf bytes . Buffer
err := json . NewEncoder ( & buf ) . Encode ( transactions )
if err != nil {
return response . Data . Transactions , err
}
postHealthResponse , err := a . Call ( http . MethodPost , "/v3/transactions" , & buf , true )
defer postHealthResponse . Body . Close ( )
body , err := ioutil . ReadAll ( postHealthResponse . Body )
if err != nil {
return response . Data . Transactions , err
}
if err := json . Unmarshal ( body , & response ) ; err != nil {
return response . Data . Transactions , err
}
if len ( response . Data . Transactions ) == 0 {
return response . Data . Transactions , fmt . Errorf ( response . Message )
}
return response . Data . Transactions , nil
}