Skip to content

Commit

Permalink
Send API key in header instead of query param. (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsubox76 authored Dec 18, 2023
1 parent 39e5145 commit 54839f2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-starfishes-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@google/generative-ai": patch
---

Send API key in header instead of query param.
16 changes: 4 additions & 12 deletions packages/main/src/requests/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe("request methods", () => {
true,
);
expect(url.toString()).to.include("generateContent");
expect(url.toString()).to.not.include("key");
expect(url.toString()).to.include("alt=sse");
});
it("non-stream", async () => {
Expand All @@ -54,18 +55,9 @@ describe("request methods", () => {
false,
);
expect(url.toString()).to.include("generateContent");
expect(url.toString()).to.include("key=key");
expect(url.toString()).to.not.include("key");
expect(url.toString()).to.not.include("alt=sse");
});
it("obscured", async () => {
const url = new RequestUrl(
"model-name",
Task.GENERATE_CONTENT,
"key",
false,
);
expect(url.toObscuredString()).to.include("key=__API_KEY__");
});
});
describe("makeRequest", () => {
it("no error", async () => {
Expand All @@ -83,7 +75,7 @@ describe("request methods", () => {
statusText: "Server Error",
} as Response);
await expect(makeRequest(fakeRequestUrl, "")).to.be.rejectedWith(
/key=__API_KEY__.*500 Server Error/,
/500 Server Error/,
);
expect(fetchStub).to.be.calledOnce;
});
Expand All @@ -95,7 +87,7 @@ describe("request methods", () => {
json: () => Promise.resolve({ error: { message: "extra info" } }),
} as Response);
await expect(makeRequest(fakeRequestUrl, "")).to.be.rejectedWith(
/key=__API_KEY__.*500 Server Error.*extra info/,
/500 Server Error.*extra info/,
);
expect(fetchStub).to.be.calledOnce;
});
Expand Down
14 changes: 5 additions & 9 deletions packages/main/src/requests/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,13 @@ export class RequestUrl {
public apiKey: string,
public stream: boolean,
) {}
toString(apiKeyInUrl = this.apiKey): string {
let url =
`${BASE_URL}/${API_VERSION}` +
`/models/${this.model}:${this.task}?key=${apiKeyInUrl}`;
toString(): string {
let url = `${BASE_URL}/${API_VERSION}/models/${this.model}:${this.task}`;
if (this.stream) {
url += "&alt=sse";
url += "?alt=sse";
}
return url;
}
toObscuredString(): string {
return this.toString("__API_KEY__");
}
}

/**
Expand All @@ -75,6 +70,7 @@ export async function makeRequest(
headers: {
"Content-Type": "application/json",
"x-goog-api-client": getClientHeaders(),
"x-goog-api-key": url.apiKey,
},
body,
});
Expand All @@ -93,7 +89,7 @@ export async function makeRequest(
}
} catch (e) {
const err = new GoogleGenerativeAIError(
`Error fetching from ${url.toObscuredString()}: ${e.message}`,
`Error fetching from ${url.toString()}: ${e.message}`,
);
err.stack = e.stack;
throw err;
Expand Down

0 comments on commit 54839f2

Please sign in to comment.