Skip to content

Commit 10ff54b

Browse files
docs: add HTTP client generation documentation
- Update README.md with client generator in features and quick setup - Add client generator to architecture.md with component diagram - Update getting-started.md with client plugin installation and output - Create comprehensive client-generation.md guide - Update restful-crud example README with client usage examples - Add HTTP client feature to examples comparison table Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 10afefd commit 10ff54b

File tree

6 files changed

+658
-31
lines changed

6 files changed

+658
-31
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ This starts a working HTTP API with JSON endpoints and OpenAPI docs - all genera
3030
## What you get
3131

3232
- **HTTP handlers** from protobuf services (JSON + binary support)
33+
- **Type-safe HTTP clients** with functional options pattern and per-call customization
3334
- **Mock server generation** with realistic field examples for rapid prototyping
3435
- **Automatic request validation** using protovalidate with buf.validate annotations
3536
- **HTTP header validation** with type checking and format validation (UUID, email, datetime)
@@ -85,6 +86,12 @@ sebuf generates:
8586
// HTTP handlers with automatic validation (both headers and body)
8687
api.RegisterUserServiceServer(userService, api.WithMux(mux))
8788

89+
// Type-safe HTTP client with functional options
90+
client := api.NewUserServiceClient("http://localhost:8080",
91+
api.WithUserServiceAPIKey("your-api-key"),
92+
)
93+
user, err := client.CreateUser(ctx, req)
94+
8895
// Mock server with realistic data (optional)
8996
mockService := api.NewMockUserServiceServer()
9097
api.RegisterUserServiceServer(mockService, api.WithMux(mux))
@@ -99,7 +106,8 @@ api.RegisterUserServiceServer(mockService, api.WithMux(mux))
99106

100107
```bash
101108
# Install the tools
102-
go install github.com/SebastienMelki/sebuf/cmd/protoc-gen-go-http@latest
109+
go install github.com/SebastienMelki/sebuf/cmd/protoc-gen-go-http@latest
110+
go install github.com/SebastienMelki/sebuf/cmd/protoc-gen-go-client@latest
103111
go install github.com/SebastienMelki/sebuf/cmd/protoc-gen-openapiv3@latest
104112

105113
# Try the complete example

docs/architecture.md

Lines changed: 70 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,30 @@ This document provides a comprehensive technical overview of sebuf's architectur
1717

1818
## System Overview
1919

20-
sebuf is a collection of two specialized protoc plugins that work together to enable modern HTTP API development from protobuf definitions:
20+
sebuf is a collection of three specialized protoc plugins that work together to enable modern HTTP API development from protobuf definitions:
2121

2222
```
23-
┌─────────────────────────────────────────────────────────────────┐
24-
│ sebuf Toolkit │
25-
├─────────────────────────────┬───────────────────────────────────┤
26-
│ protoc-gen-go-http │ protoc-gen-openapiv3 │
27-
│ │ │
28-
│ │ │
29-
│ ┌─────────────────────────┐ │ ┌─────────────────────────────────┐ │
30-
│ │HTTP Handlers │ │ │ OpenAPI v3.1 │ │
31-
│ │ + Binding │ │ │ Specifications │ │
32-
│ │ + Routing │ │ │ + Validation Rules │ │
33-
│ │ + Validation │ │ │ + Header Parameters │ │
34-
│ └─────────────────────────┘ │ └─────────────────────────────────┘ │
35-
└─────────────────────────────┴───────────────────────────────────┘
36-
37-
38-
┌─────────────────────────────────────────────────────────────────┐
39-
│ Standard Go HTTP Stack │
40-
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────────┐ │
41-
│ │ net/http │ │ Gin/Echo/ │ │ Client Libraries │ │
42-
│ │ │ │ Chi/Fiber │ │ (Generated from │ │
43-
│ │ │ │ │ │ OpenAPI specs) │ │
44-
│ └─────────────┘ └─────────────┘ └─────────────────────────┘ │
45-
└─────────────────────────────────────────────────────────────────┘
23+
┌─────────────────────────────────────────────────────────────────────────────────┐
24+
│ sebuf Toolkit │
25+
├─────────────────────────┬─────────────────────────┬─────────────────────────────┤
26+
│ protoc-gen-go-http │ protoc-gen-go-client │ protoc-gen-openapiv3 │
27+
│ │ │ │
28+
│ ┌─────────────────────┐ │ ┌─────────────────────┐ │ ┌─────────────────────────┐ │
29+
│ │HTTP Handlers │ │ │HTTP Clients │ │ │ OpenAPI v3.1 │ │
30+
│ │ + Binding │ │ │ + Type-safe calls │ │ │ Specifications │ │
31+
│ │ + Routing │ │ │ + Functional opts │ │ │ + Validation Rules │ │
32+
│ │ + Validation │ │ │ + Error handling │ │ │ + Header Parameters │ │
33+
│ └─────────────────────┘ │ └─────────────────────┘ │ └─────────────────────────┘ │
34+
└─────────────────────────┴─────────────────────────┴─────────────────────────────┘
35+
36+
37+
┌─────────────────────────────────────────────────────────────────────────────────┐
38+
│ Standard Go HTTP Stack │
39+
│ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────────────────────┐ │
40+
│ │ net/http │ │ Gin/Echo/ │ │ Generated Type-Safe Clients │ │
41+
│ │ │ │ Chi/Fiber │ │ (Direct protobuf integration) │ │
42+
│ └─────────────────┘ └─────────────────┘ └─────────────────────────────────┘ │
43+
└─────────────────────────────────────────────────────────────────────────────────┘
4644
```
4745

4846
## Plugin Architecture
@@ -73,6 +71,7 @@ func main() {
7371
| Plugin | Primary Function | Output | Dependencies |
7472
|--------|-----------------|---------|--------------|
7573
| `protoc-gen-go-http` | Generate HTTP handlers, routing & validation | `*_http*.pb.go` | `protoc-gen-go`, sebuf annotations |
74+
| `protoc-gen-go-client` | Generate type-safe HTTP clients | `*_client.pb.go` | `protoc-gen-go`, sebuf annotations |
7675
| `protoc-gen-openapiv3` | Generate OpenAPI specifications | `*.yaml`, `*.json` | None (standalone) |
7776

7877
## Code Generation Pipeline
@@ -84,10 +83,12 @@ graph TD
8483
A[.proto files] --> B[protoc compiler]
8584
B --> C[CodeGeneratorRequest]
8685
C --> D[protoc-gen-go]
86+
C --> E[protoc-gen-go-client]
8787
C --> F[protoc-gen-go-http]
8888
C --> G[protoc-gen-openapiv3]
89-
89+
9090
D --> H[.pb.go files]
91+
E --> I[*_client.pb.go]
9192
F --> J[*_http*.pb.go]
9293
G --> K[OpenAPI specs]
9394
```
@@ -170,7 +171,48 @@ func (g *HTTPGenerator) Generate() error {
170171
HTTP RequestContent-Type DetectionBinding MiddlewareService MethodResponse MarshalingHTTP Response
171172
```
172173

173-
### 2. OpenAPI Generator
174+
### 2. HTTP Client Generator
175+
176+
**Location**: `internal/clientgen/`
177+
178+
**Architecture**:
179+
```go
180+
type Generator struct {
181+
plugin *protogen.Plugin
182+
}
183+
184+
func (g *Generator) Generate() error {
185+
for _, file := range g.plugin.Files {
186+
// Generate client file for services
187+
// *_client.pb.go with interface and implementation
188+
g.generateClientFile(file)
189+
}
190+
}
191+
```
192+
193+
**Generated Components**:
194+
195+
1. **Client Interface** - Type-safe client contract
196+
2. **Client Implementation** - HTTP client with request/response handling
197+
3. **Client Options** - Functional options for configuration
198+
4. **Call Options** - Per-request customization options
199+
5. **Header Helpers** - Generated from service/method header annotations
200+
201+
**Request Flow**:
202+
```go
203+
// Generated client request pipeline
204+
Method CallBuild URLApply HeadersSerialize BodyHTTP RequestDeserialize ResponseReturn
205+
```
206+
207+
**Key Features**:
208+
- Functional options pattern for client configuration
209+
- Per-call options for request customization
210+
- Automatic path parameter substitution
211+
- Query parameter encoding for GET/DELETE
212+
- JSON and protobuf content type support
213+
- Typed error handling with `ClientError`
214+
215+
### 3. OpenAPI Generator
174216

175217
**Location**: `internal/openapiv3/`
176218

@@ -185,12 +227,12 @@ type Generator struct {
185227
func (g *Generator) ProcessFile(file *protogen.File) {
186228
// 1. Extract document metadata
187229
g.updateDocumentInfo(file)
188-
230+
189231
// 2. Process all messages → schemas
190232
for _, message := range file.Messages {
191233
g.processMessage(message)
192234
}
193-
235+
194236
// 3. Process all services → paths
195237
for _, service := range file.Services {
196238
g.processService(service)

0 commit comments

Comments
 (0)