Make client work for olm

This commit is contained in:
Owen
2025-07-18 16:53:13 -07:00
parent cd86e6b6de
commit 629a92ee81
3 changed files with 24 additions and 12 deletions

View File

@@ -34,6 +34,7 @@ type Client struct {
onConnect func() error onConnect func() error
onTokenUpdate func(token string) onTokenUpdate func(token string)
writeMux sync.Mutex writeMux sync.Mutex
clientType string // Type of client (e.g., "newt", "olm")
} }
type ClientOption func(*Client) type ClientOption func(*Client)
@@ -61,10 +62,10 @@ func (c *Client) OnTokenUpdate(callback func(token string)) {
c.onTokenUpdate = callback c.onTokenUpdate = callback
} }
// NewClient creates a new Newt client // NewClient creates a new websocket client
func NewClient(newtID, secret string, endpoint string, pingInterval time.Duration, pingTimeout time.Duration, opts ...ClientOption) (*Client, error) { func NewClient(clientType string, ID, secret string, endpoint string, pingInterval time.Duration, pingTimeout time.Duration, opts ...ClientOption) (*Client, error) {
config := &Config{ config := &Config{
NewtID: newtID, ID: ID,
Secret: secret, Secret: secret,
Endpoint: endpoint, Endpoint: endpoint,
} }
@@ -78,6 +79,7 @@ func NewClient(newtID, secret string, endpoint string, pingInterval time.Duratio
isConnected: false, isConnected: false,
pingInterval: pingInterval, pingInterval: pingInterval,
pingTimeout: pingTimeout, pingTimeout: pingTimeout,
clientType: clientType,
} }
// Apply options before loading config // Apply options before loading config
@@ -199,12 +201,22 @@ func (c *Client) getToken() (string, error) {
} }
} }
var tokenData map[string]interface{}
// Get a new token // Get a new token
tokenData := map[string]interface{}{ if c.clientType == "newt" {
"newtId": c.config.NewtID, tokenData = map[string]interface{}{
"secret": c.config.Secret, "newtId": c.config.ID,
"secret": c.config.Secret,
}
} else if c.clientType == "olm" {
tokenData = map[string]interface{}{
"olmId": c.config.ID,
"secret": c.config.Secret,
}
} }
jsonData, err := json.Marshal(tokenData) jsonData, err := json.Marshal(tokenData)
if err != nil { if err != nil {
return "", fmt.Errorf("failed to marshal token request data: %w", err) return "", fmt.Errorf("failed to marshal token request data: %w", err)
} }
@@ -212,7 +224,7 @@ func (c *Client) getToken() (string, error) {
// Create a new request // Create a new request
req, err := http.NewRequest( req, err := http.NewRequest(
"POST", "POST",
baseEndpoint+"/api/v1/auth/newt/get-token", baseEndpoint+"/api/v1/auth/"+c.clientType+"/get-token",
bytes.NewBuffer(jsonData), bytes.NewBuffer(jsonData),
) )
if err != nil { if err != nil {
@@ -310,7 +322,7 @@ func (c *Client) establishConnection() error {
// Add token to query parameters // Add token to query parameters
q := u.Query() q := u.Query()
q.Set("token", token) q.Set("token", token)
q.Set("clientType", "newt") q.Set("clientType", c.clientType)
u.RawQuery = q.Encode() u.RawQuery = q.Encode()
// Connect to WebSocket // Connect to WebSocket

View File

@@ -27,7 +27,7 @@ func getConfigPath() string {
} }
func (c *Client) loadConfig() error { func (c *Client) loadConfig() error {
if c.config.NewtID != "" && c.config.Secret != "" && c.config.Endpoint != "" { if c.config.ID != "" && c.config.Secret != "" && c.config.Endpoint != "" {
return nil return nil
} }
@@ -45,8 +45,8 @@ func (c *Client) loadConfig() error {
return err return err
} }
if c.config.NewtID == "" { if c.config.ID == "" {
c.config.NewtID = config.NewtID c.config.ID = config.ID
} }
if c.config.Secret == "" { if c.config.Secret == "" {
c.config.Secret = config.Secret c.config.Secret = config.Secret

View File

@@ -1,7 +1,7 @@
package websocket package websocket
type Config struct { type Config struct {
NewtID string `json:"newtId"` ID string `json:"id"`
Secret string `json:"secret"` Secret string `json:"secret"`
Endpoint string `json:"endpoint"` Endpoint string `json:"endpoint"`
TlsClientCert string `json:"tlsClientCert"` TlsClientCert string `json:"tlsClientCert"`