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

Add platform tags in x-goog-api-client #12680

Merged
merged 1 commit into from
Apr 2, 2024
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
2 changes: 1 addition & 1 deletion FirebaseCore/Sources/FIRApp.m
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ + (void)registerSwiftComponents {
@"FIRSessions" : @"fire-ses",
@"FIRFunctionsComponent" : @"fire-fun",
@"FIRStorageComponent" : @"fire-str",
@"FIRVertexAIComponent" : @"fire-vtx",
@"FIRVertexAIComponent" : @"fire-vertex",
};
for (NSString *className in swiftComponents.allKeys) {
Class klass = NSClassFromString(className);
Expand Down
13 changes: 10 additions & 3 deletions FirebaseVertexAI/Sources/GenerativeAIService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ import Foundation

@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
struct GenerativeAIService {
/// The language of the SDK in the format "gl-<language>/<version>" where version may be blank.
static let languageTag = "gl-swift/"

/// The Firebase SDK version in the format "fire/<version>".
static let firebaseVersionTag = "fire/\(FirebaseVersion())"

/// Gives permission to talk to the backend.
private let apiKey: String

Expand Down Expand Up @@ -155,9 +161,10 @@ struct GenerativeAIService {
var urlRequest = URLRequest(url: request.url)
urlRequest.httpMethod = "POST"
urlRequest.setValue(apiKey, forHTTPHeaderField: "x-goog-api-key")
// TODO: Determine the right client header to use.
// urlRequest.setValue("genai-swift/\(GenerativeAISwift.version))",
// forHTTPHeaderField: "x-goog-api-client")
urlRequest.setValue(
"\(GenerativeAIService.languageTag) \(GenerativeAIService.firebaseVersionTag)",
andrewheard marked this conversation as resolved.
Show resolved Hide resolved
forHTTPHeaderField: "x-goog-api-client"
)
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")

if let appCheck {
Expand Down
5 changes: 5 additions & 0 deletions FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

import FirebaseAppCheckInterop
import FirebaseCore
import XCTest

@testable import FirebaseVertexAI
Expand Down Expand Up @@ -1058,6 +1059,10 @@ final class GenerativeModelTests: XCTestCase {
let requestURL = try XCTUnwrap(request.url)
XCTAssertEqual(requestURL.path.occurrenceCount(of: "models/"), 1)
XCTAssertEqual(request.timeoutInterval, timeout)
let apiClientTags = try XCTUnwrap(request.value(forHTTPHeaderField: "x-goog-api-client"))
.components(separatedBy: " ")
XCTAssert(apiClientTags.contains(GenerativeAIService.languageTag))
XCTAssert(apiClientTags.contains(GenerativeAIService.firebaseVersionTag))
XCTAssertEqual(request.value(forHTTPHeaderField: "X-Firebase-AppCheck"), appCheckToken)
let response = try XCTUnwrap(HTTPURLResponse(
url: requestURL,
Expand Down
Loading