From c038933db728cf62288c9153c61af51db186bab6 Mon Sep 17 00:00:00 2001
From: Tobias Bieniek <tobias@bieniek.cloud>
Date: Sat, 15 Apr 2023 16:41:08 +0200
Subject: [PATCH] crates: Use `?include=` to reduce crates.io backend load
 (#9081)

By default the `/crates/:crate_name` API endpoint returns the categories and keywords related to the crates. Unfortunately this causes two additional database queries on the crates.io backend.

This commit change the request URL to use `?include=` to only include the additional metadata that is needed for the requested badges.
---
 services/crates/crates-base.js             | 2 +-
 services/crates/crates-downloads.tester.js | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/services/crates/crates-base.js b/services/crates/crates-base.js
index 63699a581e..d553adb8ea 100644
--- a/services/crates/crates-base.js
+++ b/services/crates/crates-base.js
@@ -44,7 +44,7 @@ class BaseCratesService extends BaseJsonService {
   async fetch({ crate, version }) {
     const url = version
       ? `https://crates.io/api/v1/crates/${crate}/${version}`
-      : `https://crates.io/api/v1/crates/${crate}`
+      : `https://crates.io/api/v1/crates/${crate}?include=versions,downloads`
     return this._requestJson({ schema, url })
   }
 }
diff --git a/services/crates/crates-downloads.tester.js b/services/crates/crates-downloads.tester.js
index 629cd05ed0..34953c23c1 100644
--- a/services/crates/crates-downloads.tester.js
+++ b/services/crates/crates-downloads.tester.js
@@ -34,7 +34,7 @@ t.create('recent downloads (null)')
   .get('/dr/libc.json')
   .intercept(nock =>
     nock('https://crates.io')
-      .get('/api/v1/crates/libc')
+      .get('/api/v1/crates/libc?include=versions,downloads')
       .reply(200, {
         crate: {
           downloads: 42,
-- 
GitLab