Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send API key in header instead of query param. #21

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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