fetch_ml/internal/api/protocol_simplified.go
Jeremie Fraeys cd5640ebd2 Slim and secure: move scripts, clean configs, remove secrets
- Move ci-test.sh and setup.sh to scripts/
- Trim docs/src/zig-cli.md to current structure
- Replace hardcoded secrets with placeholders in configs
- Update .gitignore to block .env*, secrets/, keys, build artifacts
- Slim README.md to reflect current CLI/TUI split
- Add cleanup trap to ci-test.sh
- Ensure no secrets are committed
2025-12-07 13:57:51 -05:00

155 lines
3.4 KiB
Go

package api
import (
"encoding/json"
"time"
)
// Simplified protocol using JSON instead of binary serialization
// Response represents a simplified API response
type Response struct {
Type string `json:"type"`
Timestamp int64 `json:"timestamp"`
Data interface{} `json:"data,omitempty"`
Error *ErrorInfo `json:"error,omitempty"`
}
// ErrorInfo represents error information
type ErrorInfo struct {
Code int `json:"code"`
Message string `json:"message"`
Details string `json:"details,omitempty"`
}
// ProgressInfo represents progress information
type ProgressInfo struct {
Type string `json:"type"`
Value uint32 `json:"value"`
Total uint32 `json:"total"`
Message string `json:"message"`
}
// LogInfo represents log information
type LogInfo struct {
Level string `json:"level"`
Message string `json:"message"`
}
// Response types
const (
TypeSuccess = "success"
TypeError = "error"
TypeProgress = "progress"
TypeStatus = "status"
TypeData = "data"
TypeLog = "log"
)
// Error codes
const (
ErrUnknown = 0
ErrInvalidRequest = 1
ErrAuthFailed = 2
ErrPermissionDenied = 3
ErrNotFound = 4
ErrExists = 5
ErrServerOverload = 16
ErrDatabase = 17
ErrNetwork = 18
ErrStorage = 19
ErrTimeout = 20
)
// NewSuccessResponse creates a success response
func NewSuccessResponse(message string) *Response {
return &Response{
Type: TypeSuccess,
Timestamp: time.Now().Unix(),
Data: message,
}
}
// NewSuccessResponseWithData creates a success response with data
func NewSuccessResponseWithData(message string, data interface{}) *Response {
return &Response{
Type: TypeData,
Timestamp: time.Now().Unix(),
Data: map[string]interface{}{
"message": message,
"payload": data,
},
}
}
// NewErrorResponse creates an error response
func NewErrorResponse(code int, message, details string) *Response {
return &Response{
Type: TypeError,
Timestamp: time.Now().Unix(),
Error: &ErrorInfo{
Code: code,
Message: message,
Details: details,
},
}
}
// NewProgressResponse creates a progress response
func NewProgressResponse(progressType string, value, total uint32, message string) *Response {
return &Response{
Type: TypeProgress,
Timestamp: time.Now().Unix(),
Data: ProgressInfo{
Type: progressType,
Value: value,
Total: total,
Message: message,
},
}
}
// NewStatusResponse creates a status response
func NewStatusResponse(data string) *Response {
return &Response{
Type: TypeStatus,
Timestamp: time.Now().Unix(),
Data: data,
}
}
// NewDataResponse creates a data response
func NewDataResponse(dataType string, payload interface{}) *Response {
return &Response{
Type: TypeData,
Timestamp: time.Now().Unix(),
Data: map[string]interface{}{
"type": dataType,
"payload": payload,
},
}
}
// NewLogResponse creates a log response
func NewLogResponse(level, message string) *Response {
return &Response{
Type: TypeLog,
Timestamp: time.Now().Unix(),
Data: LogInfo{
Level: level,
Message: message,
},
}
}
// ToJSON converts the response to JSON bytes
func (r *Response) ToJSON() ([]byte, error) {
return json.Marshal(r)
}
// FromJSON creates a response from JSON bytes
func FromJSON(data []byte) (*Response, error) {
var response Response
err := json.Unmarshal(data, &response)
return &response, err
}