# Centrifugo Go RPC Client

Auto-generated Go client for Centrifugo WebSocket RPC communication.

## Installation

```bash
# Dependencies are fetched via Go module proxy (proxy.golang.org by default)
go mod tidy
```

### Dependencies

This client uses **nhooyr.io/websocket** - a pure Go WebSocket library with no GitHub dependencies:

- `nhooyr.io/websocket` v1.8.10 - WebSocket client library
- Standard library: `crypto/rand`, `encoding/json`, `context`, `sync`, `time`

All dependencies are fetched via Go module proxy (proxy.golang.org) by default.

### Using Custom Go Proxy

If you need to use a custom proxy or private registry:

```bash
# Set custom proxy
export GOPROXY=https://your-proxy.com,direct

# Or use Athens, JFrog Artifactory, etc.
export GOPROXY=https://athens.your-company.com

# Then install
go mod tidy
```

### Offline/Air-gapped Installation

For offline environments:

```bash
# On a machine with internet, vendor dependencies
go mod vendor

# Copy the entire directory (including vendor/) to offline machine
# Then build with vendor
go build -mod=vendor
```

### Why nhooyr.io/websocket?

This client uses `nhooyr.io/websocket` instead of GitHub-hosted libraries for several reasons:

- ✅ **No GitHub dependencies** - hosted at nhooyr.io, not github.com
- ✅ **Clean module path** - better for enterprise proxies and air-gapped environments
- ✅ **Minimal dependencies** - only one external dependency
- ✅ **Modern API** - context-aware, clean concurrent design
- ✅ **Production ready** - used by major projects

UUID generation uses `crypto/rand` from stdlib (no external UUID library needed).

## Usage

### Basic Example

```go
package main

import (
    "context"
    "fmt"
    "log"
    "time"

    "{{ module_path }}"
)

func main() {
    ctx := context.Background()

    // Create client
    client := {{ package_name }}.NewAPIClient(
        "ws://localhost:8000/connection/websocket",
        "your-jwt-token",
        "user123",
    )

    // Connect
    if err := client.Connect(ctx); err != nil {
        log.Fatalf("Failed to connect: %v", err)
    }
    defer client.Disconnect()

    fmt.Println("✅ Connected to Centrifugo")

    {% if methods %}
    // Example RPC calls
    {% for method in methods %}

    // Call {{ method.name }}
    result{{ loop.index }}, err := client.{{ method.name_go }}(ctx, params)
    if err != nil {
        log.Printf("Error calling {{ method.name }}: %v", err)
    } else {
        fmt.Printf("Result: %+v\n", result{{ loop.index }})
    }
    {% endfor %}
    {% endif %}
}
```

### With Context Timeout

```go
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

result, err := client.SomeMethod(ctx, params)
if err != nil {
    log.Printf("RPC call failed: %v", err)
}
```

### Error Handling

```go
result, err := client.SomeMethod(ctx, params)
if err != nil {
    if rpcErr, ok := err.(*{{ package_name }}.RPCError); ok {
        fmt.Printf("RPC error %d: %s\n", rpcErr.Code, rpcErr.Message)
    } else {
        fmt.Printf("Connection error: %v\n", err)
    }
    return
}

fmt.Printf("Success: %+v\n", result)
```

## Architecture

### Files

- `types.go` - Generated Go structs from Pydantic models
- `rpc_client.go` - Base RPC client with correlation ID pattern
- `client.go` - Thin wrapper with typed methods
- `go.mod` - Go module dependencies

### How It Works

1. **Connect**: Establishes WebSocket connection to Centrifugo
2. **Subscribe**: Subscribes to user-specific reply channel
3. **Call**: Publishes RPC request with correlation ID
4. **Wait**: Waits for response on reply channel
5. **Return**: Returns typed result or error

### Type Safety

All RPC methods are fully typed:

```go
// Request and response types are generated from Pydantic models
func (api *APIClient) GetUserProfile(ctx context.Context, params UserProfileRequest) (*UserProfileResponse, error)
```

### Correlation ID Pattern

Each RPC call:
1. Generates unique correlation ID (UUID)
2. Publishes request to `rpc.requests` channel
3. Waits for response on `user#{userID}` channel
4. Matches response by correlation ID
5. Returns result or error

## Dependencies

- `github.com/centrifugal/centrifuge-go` - Centrifugo Go client
- `github.com/google/uuid` - UUID generation

## Generated Code

This client was auto-generated by django-cfg centrifugo codegen.
Do not edit generated files manually - regenerate instead.
