Skip to content

Commit

Permalink
docs: Add doc about creating new endpoints (#645)
Browse files Browse the repository at this point in the history
<!-- Please make sure there is an issue that this PR is correlated to. -->

## Changes

<!-- If there are frontend changes, please include screenshots. -->
  • Loading branch information
AngelOnFira committed Apr 25, 2024
1 parent c4a88de commit f8f4ccc
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions docs/getting_started/ADDING_AN_ENDPOINT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Adding a new endpoint

## Adding a new endpoint to Rivet Core

1. Update the respective `__package.yml__` file with the new endpoint you want
to add. This may require adding something to the `common.yml` file that
should be at `../common.yml` relative to the package file.

1. Run the script `./scripts/fern/gen.sh`. Currently that might look something
like:

`FERN_REPO_PATH=/home/forest/data/git/fern ./scripts/fern/gen.sh`

This will generate new files in `sdks/`. We'll mostly care about the new Rust
files in `sdks/rust/full/rust/src/...`.

1. If you added any enums or structs to the `common.yml` file, you'll need to
add/update the impls in `lib/convert/src/impls`. This allows enums to be
mapped from the generated API type to an internal mapping. For example, in
`lib/convert/src/impls/admin.rs`:

```rust
impl ApiFrom<models::AdminProvider> for backend::cluster::Provider {
fn api_from(value: models::AdminProvider) -> backend::cluster::Provider {
match value {
models::AdminProvider::Linode => backend::cluster::Provider::Linode,
}
}
}
```

1. Add the api endpoint in the related route file for the api. Say
`svc/api/admin/src/route/mod.rs`:

```rust
define_router! {
routes: {
"cluster": {
POST: cluster::create(
body: models::AdminClusterCreateRequest,
),
},
},
}
```

1. Create the handler for the new endpoint in the related handler file. Say
`svc/api/admin/src/handler/cluster.rs`:

```rust
pub async fn create(
ctx: Ctx<Auth>,
body: models::AdminClusterCreateRequest,
) -> GlobalResult<models::AdminClusterCreateResponse> {
// Your handler code here
}
```

Some tips for this:

- Mapping from an enum in the model to an integer in the protobuf-generated
model can be done with `as i32` (or conversely `from_i32`)

## Connecting to the endpoint with Bolt

If Bolt needs a new CLI command that will interface with this api, these are the
steps you'll need to take:

1. Add the new command to the respective existing or new bolt command.

1. Openapi requests are generated for each endpoint defined in the
`__package.yml__` file. Here's an example of calling one of these requests:

_Note: These current docs only make use of the cloud token, hub and cli
endpoint docs to come later._

```rust
admin_clusters_api::admin_clusters_create(
&ctx.openapi_config_cloud().await?,
models::AdminClustersCreateRequest {
name_id: cluster_name_id,
owner_team_id: Some(Uuid::parse_str(&owner_team_id).unwrap()),
},
)
.await?;
```

This also makes use of the same `models` that are generated by the Openapi,
allowing ser/de data to be passed across the wire.

Some tips for this:

- In some cases, you might need to write a custom request instead of using a
generated one. For example, Fern allows get requests to `allow-multiple` of
the same repeated parameter. The generated request does not interpret this
correctly, so you'll need to write a custom request for this case.

0 comments on commit f8f4ccc

Please sign in to comment.