-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Add doc about creating new endpoints (#645)
<!-- 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
1 parent
c4a88de
commit f8f4ccc
Showing
1 changed file
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |