Skip to content

Commit 448b981

Browse files
author
Fahad Zubair
committed
Update examples to use http-1
1 parent 53cd75e commit 448b981

59 files changed

Lines changed: 4307 additions & 203 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/legacy/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pokemon-service-client/
2+
pokemon-service-server-sdk/
3+
Cargo.lock

examples/legacy/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Without this configuration, the workspace will be read from `rust-runtime`, causing the build to fail.
2+
[workspace]
3+
resolver = "2"
4+
members = [
5+
"pokemon-service-common",
6+
"pokemon-service",
7+
"pokemon-service-tls",
8+
"pokemon-service-lambda",
9+
"pokemon-service-server-sdk",
10+
"pokemon-service-client",
11+
"pokemon-service-client-usage",
12+
]
13+
14+
[profile.release]
15+
lto = true

examples/legacy/Makefile

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
SRC_DIR := $(shell git rev-parse --show-toplevel)
2+
CUR_DIR := $(shell pwd)
3+
GRADLE := $(SRC_DIR)/gradlew
4+
SERVER_SDK_DST := $(CUR_DIR)/pokemon-service-server-sdk
5+
CLIENT_SDK_DST := $(CUR_DIR)/pokemon-service-client
6+
SERVER_SDK_SRC := $(SRC_DIR)/codegen-server-test/build/smithyprojections/codegen-server-test/pokemon-service-server-sdk-http0x/rust-server-codegen
7+
CLIENT_SDK_SRC := $(SRC_DIR)/codegen-client-test/build/smithyprojections/codegen-client-test/pokemon-service-client-http0x/rust-client-codegen
8+
9+
all: codegen
10+
11+
codegen:
12+
$(GRADLE) --project-dir $(SRC_DIR) -P modules='pokemon-service-server-sdk-http0x,pokemon-service-client-http0x' :codegen-client-test:assemble :codegen-server-test:assemble
13+
mkdir -p $(SERVER_SDK_DST) $(CLIENT_SDK_DST)
14+
cp -av $(SERVER_SDK_SRC)/* $(SERVER_SDK_DST)/
15+
cp -av $(CLIENT_SDK_SRC)/* $(CLIENT_SDK_DST)/
16+
17+
build: codegen
18+
cargo build
19+
20+
run: codegen
21+
cargo run
22+
23+
clippy: codegen
24+
cargo clippy
25+
26+
test: codegen
27+
cargo test
28+
29+
doc-open: codegen
30+
cargo doc --no-deps --open
31+
32+
clean:
33+
cargo clean || echo "Unable to run cargo clean"
34+
35+
lambda_watch:
36+
cargo lambda watch
37+
38+
lambda_invoke:
39+
cargo lambda invoke pokemon-service-lambda --data-file pokemon-service/tests/fixtures/example-apigw-request.json
40+
41+
distclean: clean
42+
rm -rf $(SERVER_SDK_DST) $(CLIENT_SDK_DST) Cargo.lock
43+
44+
.PHONY: all

examples/legacy/README.md

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
# Legacy HTTP 0.x Examples
2+
3+
This directory contains examples for Smithy-rs using HTTP 0.x (hyper 0.14, http 0.2). These examples use the legacy HTTP stack with `aws-smithy-legacy-http` and `aws-smithy-legacy-http-server`.
4+
5+
For HTTP 1.x examples (hyper 1.x, http 1.x), see the parent [examples](../) directory.
6+
7+
## Building
8+
9+
### 1. Generate the SDKs
10+
11+
From this directory, run:
12+
13+
```bash
14+
make codegen
15+
```
16+
17+
This will generate:
18+
- `pokemon-service-server-sdk-http0x` - Server SDK using HTTP 0.x
19+
- `pokemon-service-client-http0x` - Client SDK using HTTP 0.x
20+
21+
The generated SDKs are copied to:
22+
- `pokemon-service-server-sdk/`
23+
- `pokemon-service-client/`
24+
25+
### 2. Build all examples
26+
27+
```bash
28+
cargo build
29+
```
30+
31+
Or to check without building artifacts:
32+
33+
```bash
34+
cargo check
35+
```
36+
37+
## Running the Examples
38+
39+
### Start the Pokemon Service
40+
41+
In one terminal, start the server:
42+
43+
```bash
44+
cargo run --bin pokemon-service
45+
```
46+
47+
The server will start on `http://localhost:13734`
48+
49+
### Run Client Examples
50+
51+
In another terminal, from the `pokemon-service-client-usage/` directory:
52+
53+
```bash
54+
cd pokemon-service-client-usage
55+
cargo run --example simple-client
56+
```
57+
58+
#### Available Client Examples
59+
60+
| Example | Description |
61+
|---------|-------------|
62+
| `simple-client` | Basic client usage - creates a client and calls an operation |
63+
| `endpoint-resolver` | Custom endpoint resolver configuration |
64+
| `handling-errors` | Sending input parameters and handling errors |
65+
| `custom-header` | Adding custom headers to requests |
66+
| `custom-header-using-interceptor` | Accessing operation name in an interceptor |
67+
| `response-header-interceptor` | Getting operation name and accessing response before deserialization |
68+
| `use-config-bag` | Using the property bag to pass data across interceptors |
69+
| `retry-customize` | Customizing retry settings |
70+
| `timeout-config` | Configuring timeouts |
71+
| `mock-request` | Using custom HttpConnector for mock responses |
72+
| `trace-serialize` | Tracing request/response during serialization |
73+
| `client-connector` | Changing TLS configuration |
74+
75+
To list all available examples:
76+
77+
```bash
78+
cd pokemon-service-client-usage
79+
cargo run --example
80+
```
81+
82+
### Other Services
83+
84+
#### Pokemon Service with TLS
85+
86+
```bash
87+
cargo run --bin pokemon-service-tls
88+
```
89+
90+
#### Pokemon Service on AWS Lambda
91+
92+
```bash
93+
cargo run --bin pokemon-service-lambda
94+
```
95+
96+
## Project Structure
97+
98+
```
99+
legacy/
100+
├── pokemon-service/ # Main HTTP service implementation
101+
├── pokemon-service-tls/ # TLS-enabled service
102+
├── pokemon-service-lambda/ # AWS Lambda service
103+
├── pokemon-service-common/ # Shared service logic
104+
├── pokemon-service-client-usage/ # Client usage examples
105+
├── pokemon-service-server-sdk/ # Generated server SDK (HTTP 0.x)
106+
└── pokemon-service-client/ # Generated client SDK (HTTP 0.x)
107+
```
108+
109+
## Key Dependencies (HTTP 0.x)
110+
111+
- `hyper = "0.14"`
112+
- `http = "0.2"`
113+
- `aws-smithy-legacy-http`
114+
- `aws-smithy-legacy-http-server`
115+
116+
## Regenerating SDKs
117+
118+
If you need to regenerate the SDKs from scratch:
119+
120+
```bash
121+
rm -rf pokemon-service-server-sdk pokemon-service-client
122+
make codegen
123+
```
124+
125+
## Testing
126+
127+
Run all tests:
128+
129+
```bash
130+
cargo test
131+
```
132+
133+
Run tests for a specific package:
134+
135+
```bash
136+
cargo test -p pokemon-service
137+
```
138+
139+
## Troubleshooting
140+
141+
### Port Already in Use
142+
143+
If port 13734 is already in use, you can specify a different port:
144+
145+
```bash
146+
cargo run --bin pokemon-service -- --port 8080
147+
```
148+
149+
Then update the client examples to use the new port by setting the environment variable:
150+
151+
```bash
152+
POKEMON_SERVICE_URL=http://localhost:8080 cargo run --example simple-client
153+
```
154+
155+
### SDK Generation Issues
156+
157+
If the generated SDKs have issues, try cleaning and regenerating:
158+
159+
```bash
160+
# Clean generated SDKs
161+
rm -rf pokemon-service-server-sdk pokemon-service-client
162+
163+
# Clean gradle cache
164+
cd ../..
165+
./gradlew clean
166+
167+
# Regenerate
168+
cd examples/legacy
169+
make codegen
170+
```
171+
172+
## Migration to HTTP 1.x
173+
174+
For new projects, we recommend using the HTTP 1.x examples in the parent [examples](../) directory. These legacy examples are maintained for backward compatibility and for projects that need to use the HTTP 0.x stack.
175+
176+
The main differences:
177+
- HTTP 1.x uses `hyper 1.x`, `http 1.x`
178+
- HTTP 1.x uses `aws-smithy-http`, `aws-smithy-http-server` (not legacy versions)
179+
- HTTP 1.x has better performance and modern async runtime support
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[package]
2+
name = "pokemon-service-client-usage"
3+
version = "0.1.0"
4+
edition = "2021"
5+
publish = false
6+
7+
[features]
8+
9+
10+
[dependencies]
11+
# The generated client utilizes types defined in other crates, such as `aws_smithy_types`
12+
# and `aws_smithy_http`. However, most of these types are re-exported by the generated client,
13+
# eliminating the need to directly depend on the crates that provide them. In rare instances,
14+
# you may still need to include one of these crates as a dependency. Examples that require this
15+
# are specifically noted in comments above the corresponding dependency in this file.
16+
pokemon-service-client = { path = "../pokemon-service-client/", package = "pokemon-service-client-http0x", features = ["behavior-version-latest"] }
17+
18+
# Required for getting the operation name from the `Metadata`.
19+
aws-smithy-legacy-http = { path = "../../../rust-runtime/aws-smithy-legacy-http/" }
20+
21+
# Required for `Storable` and `StoreReplace` in `response-header-interceptor` example.
22+
aws-smithy-types = { path = "../../../rust-runtime/aws-smithy-types/" }
23+
24+
# Required for `HyperClientBuilder` in `client-connector` example.
25+
aws-smithy-runtime = { path = "../../../rust-runtime/aws-smithy-runtime/", features=["test-util"] }
26+
27+
# Required for `Metadata` in `custom-header-using-interceptor` example.
28+
aws-smithy-runtime-api = { path = "../../../rust-runtime/aws-smithy-runtime-api/", features=["client"] }
29+
30+
31+
hyper = { version = "0.14.25", features = ["client", "full"] }
32+
tokio = {version = "1.26.0", features=["full"]}
33+
tracing = "0.1.37"
34+
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
35+
rustls = "0.21.8"
36+
hyper-rustls = "0.24.1"
37+
http = "0.2.9"
38+
uuid = {version="1.4.1", features = ["v4"]}
39+
thiserror = "1.0.49"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# smithy-rs Client Examples
2+
3+
This package contains some examples on how to use the Smithy Client to communicate
4+
with a Smithy-based service.
5+
6+
## Pre-requisites
7+
8+
1. Build the `pokemon-service-client` and `pokemon-service` by invoking `make` in the
9+
[examples](https://github.com/smithy-lang/smithy-rs/tree/main/examples) folder.
10+
11+
```console
12+
make
13+
```
14+
15+
2. Run the Pokemon service locally by issuing the following command from the
16+
[examples](https://github.com/smithy-lang/smithy-rs/tree/main/examples) folder. This
17+
will launch the Smithy-Rs based service on TCP port 13734.
18+
19+
```console
20+
cargo run --bin pokemon-service
21+
```
22+
23+
## Running the examples
24+
25+
You can view a list of examples by running `cargo run --example` from the
26+
[pokemon-service-client-usage](https://github.com/smithy-lang/smithy-rs/tree/main/examples/pokemon-service-client-usage)
27+
folder. To run an example, pass its name to the `cargo run --example` command, e.g.:
28+
29+
```console
30+
cargo run --example simple-client
31+
```
32+
33+
## List of examples
34+
35+
| Rust Example | Description |
36+
|--------------------------------|-------------------------------------------------------------------------|
37+
| simple-client | Creates a Smithy Client and calls an operation on it. |
38+
| endpoint-resolver | How to set a custom endpoint resolver. |
39+
| handling-errors | How to send an input parameter to an operation, and to handle errors. |
40+
| custom-header | How to add headers to a request. |
41+
| custom-header-using-interceptor| How to access operation name being called in an interceptor. |
42+
| response-header-interceptor | How to get operation name and access response before it is deserialized.|
43+
| use-config-bag | How to use the property bag to pass data across interceptors. |
44+
| retries-customize | Customize retry settings. |
45+
| retries-disable | How to disable retries. |
46+
| timeout-config | How to configure timeouts. |
47+
| mock-request | Use a custom HttpConnector / Client to generate mock responses. |
48+
| trace-serialize | Trace request and response as they are serialized / deserialized. |
49+
| client-connector | Shows how to change TLS related configuration. |

0 commit comments

Comments
 (0)