Skip to content

Commit d7ce769

Browse files
authored
Update bindings to use the reqwest-trait template (#343)
## 🎟️ Tracking https://bitwarden.atlassian.net/browse/PM-25542 ## 📔 Objective Note: The PR is absolutely massive because the generated code is very verbose (something that I plan to optimize in the future) and github is pretty bad about filtering out whitespace changes 😞. To facilitate review I include some recommendations at the bottom of this block. This template is trait based which should allow easier mocking. It also follows a similar pattern to our clients which I think would make it easier to use: ```rust let config = client.internal.get_api_configurations().await; // Old API let res = bitwarden_api_api::apis::projects_api::projects_create( &config.api, input.organization_id, project, ).await?; // New API let res = config.api_client .projects_api() .create(input.organization_id, project) .await?; ``` The provided mocks are also typesafe and easy to use: ```rust let api_client = ApiClient::new_mocked(move |mock| { mock.folders_api .expect_post() .returning(move |model| { Ok(FolderResponseModel { id: Some(folder_id), name: Some(model.unwrap().name), revision_date: Some("2025-01-01T00:00:00Z".to_string()), object: Some("folder".to_string()), }) }) .once(); }); ``` ### PR structure The PR is split in four commits: - `Update OpenAPI and templates to latest version`: Updates the openapi generator and the templates to the latest version. These files should match with the files at https://github.com/OpenAPITools/openapi-generator/tree/ee40887d47f6d7a16318772f49c63b8eb0553488/modules/openapi-generator/src/main/resources/rust I've also gone ahead and deleted some unused files to make it clearer which files are being used. - `Add our changes to the templates` - Updates to templates - `README.mustache`: Added the server commit hash to the readme - `model.mustache` and `model_mod.mustache`: Remove double option pattern, as we don't differentiate between not required and nullable, and they just add noise. - `api.mustache`: Made async-traits ?Send to support WASM - `api.mustache`: Used our custom `x-action-name` instead of `operationID`, so that function names are just `functionName` instead of `ControllerName_FunctionName` - `api.mustache`: Filtered deprecated operations out, no need to generate code for them when they shouldn't be used - `api.mustache`: Use `uuid` instead of `&str` for request parameters - `api.mustache`: Replaced all the function lifetimes by a single one. Previously it would create one per parameter, which is more flexible but made for some terrible function signatures. - `api_mod.mustache`: Replaced the root Api trait and implementations by a single ApiClient enum with both real and mock impls. This is functionally the same but allows the compiler to better optimize out the unused functions (from 3.7MB to 2.2MB WASM blob size) - `🤖 Generate bindings`: Generate the bindings using the template from the previous step. this is based on the server bindings from bitwarden/server@d384c0c - `Update code to new api`. Updated our code to build with the new template, most changes are very simple API adjustments, the biggest changes are in the `client/internal.rs` file and consist in modifications to how we initialize and update the `ApiConfigurations` struct to match the newer API. ### PR review recommendation As this PR edits the generated bindings, the total changes are huge and difficult to review. As such, I've written a suggested review guide for the teams. #### For vault/auth Filter by team owned, the vault/auth related changes are fairly small: - small update in the /sync code to use the new API - updated the folder API code to use the new API and new mocks - updated the send access code to use the new configuration If filtering by team owned doesn't work, the non-platform changes are contained entirely in the fourth commit: 320ea80 #### For platform Due to the big autogenerated changes, I recommend reviewing commit by commit. The first, third and fourth commits should be reviewed as normal. The second commit, marked as 🤖, should instead be reviewed by regenerating the bindings and making sure that nothing is changed: ```sh cd server git checkout c93c3464732c93c9be593a3a55b032c029c4bd6f pwsh ./dev/generate_openapi_files.ps1 cd ../sdk-internal git pull git checkout ps/migrate-to-openapi-rust-trait ./support/build-api.sh git status # This should report a clean work tree ``` ## ⏰ Reminders before review - Contributor guidelines followed - All formatters and local linters executed and passed - Written new unit and / or integration tests where applicable - Protected functional changes with optionality (feature flags) - Used internationalization (i18n) for all UI strings - CI builds passed - Communicated to DevOps any deployment requirements - Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team ## 🦮 Reviewer guidelines <!-- Suggested interactions but feel free to use (or not) as you desire! --> - 👍 (`:+1:`) or similar for great changes - 📝 (`:memo:`) or ℹ️ (`:information_source:`) for notes or general info - ❓ (`:question:`) for questions - 🤔 (`:thinking:`) or 💭 (`:thought_balloon:`) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion - 🎨 (`:art:`) for suggestions / improvements - ❌ (`:x:`) or ⚠️ (`:warning:`) for more significant problems or concerns needing attention - 🌱 (`:seedling:`) or ♻️ (`:recycle:`) for future improvements or indications of technical debt - ⛏ (`:pick:`) for minor or nitpick changes
1 parent 9947387 commit d7ce769

File tree

136 files changed

+37022
-35726
lines changed

Some content is hidden

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

136 files changed

+37022
-35726
lines changed

Cargo.lock

Lines changed: 74 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bitwarden_license/bitwarden-sm/src/projects/create.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ pub(crate) async fn create_project(
4040
});
4141

4242
let config = client.internal.get_api_configurations().await;
43-
let res = bitwarden_api_api::apis::projects_api::projects_create(
44-
&config.api,
45-
input.organization_id,
46-
project,
47-
)
48-
.await?;
43+
let res = config
44+
.api_client
45+
.projects_api()
46+
.create(input.organization_id, project)
47+
.await?;
4948

5049
ProjectResponse::process_response(res, &mut key_store.context())
5150
}

bitwarden_license/bitwarden-sm/src/projects/delete.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ pub(crate) async fn delete_projects(
2121
input: ProjectsDeleteRequest,
2222
) -> Result<ProjectsDeleteResponse, SecretsManagerError> {
2323
let config = client.internal.get_api_configurations().await;
24-
let res =
25-
bitwarden_api_api::apis::projects_api::projects_bulk_delete(&config.api, Some(input.ids))
26-
.await?;
24+
let res = config
25+
.api_client
26+
.projects_api()
27+
.bulk_delete(Some(input.ids))
28+
.await?;
2729

2830
ProjectsDeleteResponse::process_response(res)
2931
}

bitwarden_license/bitwarden-sm/src/projects/get.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub(crate) async fn get_project(
1919
) -> Result<ProjectResponse, SecretsManagerError> {
2020
let config = client.internal.get_api_configurations().await;
2121

22-
let res = bitwarden_api_api::apis::projects_api::projects_get(&config.api, input.id).await?;
22+
let res = config.api_client.projects_api().get(input.id).await?;
2323

2424
let key_store = client.internal.get_key_store();
2525

bitwarden_license/bitwarden-sm/src/projects/list.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ pub(crate) async fn list_projects(
2020
input: &ProjectsListRequest,
2121
) -> Result<ProjectsResponse, SecretsManagerError> {
2222
let config = client.internal.get_api_configurations().await;
23-
let res = bitwarden_api_api::apis::projects_api::projects_list_by_organization(
24-
&config.api,
25-
input.organization_id,
26-
)
27-
.await?;
23+
let res = config
24+
.api_client
25+
.projects_api()
26+
.list_by_organization(input.organization_id)
27+
.await?;
2828

2929
let key_store = client.internal.get_key_store();
3030

bitwarden_license/bitwarden-sm/src/projects/update.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ pub(crate) async fn update_project(
4242
});
4343

4444
let config = client.internal.get_api_configurations().await;
45-
let res =
46-
bitwarden_api_api::apis::projects_api::projects_update(&config.api, input.id, project)
47-
.await?;
45+
let res = config
46+
.api_client
47+
.projects_api()
48+
.update(input.id, project)
49+
.await?;
4850

4951
ProjectResponse::process_response(res, &mut key_store.context())
5052
}

bitwarden_license/bitwarden-sm/src/secrets/create.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,11 @@ pub(crate) async fn create_secret(
5555
};
5656

5757
let config = client.internal.get_api_configurations().await;
58-
let res = bitwarden_api_api::apis::secrets_api::secrets_create(
59-
&config.api,
60-
input.organization_id,
61-
secret,
62-
)
63-
.await?;
58+
let res = config
59+
.api_client
60+
.secrets_api()
61+
.create(input.organization_id, secret)
62+
.await?;
6463

6564
SecretResponse::process_response(res, &mut key_store.context())
6665
}

bitwarden_license/bitwarden-sm/src/secrets/delete.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ pub(crate) async fn delete_secrets(
2121
input: SecretsDeleteRequest,
2222
) -> Result<SecretsDeleteResponse, SecretsManagerError> {
2323
let config = client.internal.get_api_configurations().await;
24-
let res =
25-
bitwarden_api_api::apis::secrets_api::secrets_bulk_delete(&config.api, Some(input.ids))
26-
.await?;
24+
let res = config
25+
.api_client
26+
.secrets_api()
27+
.bulk_delete(Some(input.ids))
28+
.await?;
2729

2830
SecretsDeleteResponse::process_response(res)
2931
}

bitwarden_license/bitwarden-sm/src/secrets/get.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub(crate) async fn get_secret(
1818
input: &SecretGetRequest,
1919
) -> Result<SecretResponse, SecretsManagerError> {
2020
let config = client.internal.get_api_configurations().await;
21-
let res = bitwarden_api_api::apis::secrets_api::secrets_get(&config.api, input.id).await?;
21+
let res = config.api_client.secrets_api().get(input.id).await?;
2222

2323
let key_store = client.internal.get_key_store();
2424

bitwarden_license/bitwarden-sm/src/secrets/get_by_ids.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ pub(crate) async fn get_secrets_by_ids(
2222

2323
let config = client.internal.get_api_configurations().await;
2424

25-
let res =
26-
bitwarden_api_api::apis::secrets_api::secrets_get_secrets_by_ids(&config.api, request)
27-
.await?;
25+
let res = config
26+
.api_client
27+
.secrets_api()
28+
.get_secrets_by_ids(request)
29+
.await?;
2830

2931
let key_store = client.internal.get_key_store();
3032

0 commit comments

Comments
 (0)