From 3099799b01e97918e51ba70ac4dab1f642ea8c5e Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Tue, 2 Apr 2024 17:11:08 +0000 Subject: [PATCH] Add platform tags in `x-goog-api-client` (#12680) --- FirebaseCore/Sources/FIRApp.m | 2 +- FirebaseVertexAI/Sources/GenerativeAIService.swift | 13 ++++++++++--- .../Tests/Unit/GenerativeModelTests.swift | 5 +++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/FirebaseCore/Sources/FIRApp.m b/FirebaseCore/Sources/FIRApp.m index a8cc38c12de3..12afc1343211 100644 --- a/FirebaseCore/Sources/FIRApp.m +++ b/FirebaseCore/Sources/FIRApp.m @@ -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); diff --git a/FirebaseVertexAI/Sources/GenerativeAIService.swift b/FirebaseVertexAI/Sources/GenerativeAIService.swift index e1df1010c06e..b3687c3c0134 100644 --- a/FirebaseVertexAI/Sources/GenerativeAIService.swift +++ b/FirebaseVertexAI/Sources/GenerativeAIService.swift @@ -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-/" where version may be blank. + static let languageTag = "gl-swift/" + + /// The Firebase SDK version in the format "fire/". + static let firebaseVersionTag = "fire/\(FirebaseVersion())" + /// Gives permission to talk to the backend. private let apiKey: String @@ -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)", + forHTTPHeaderField: "x-goog-api-client" + ) urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type") if let appCheck { diff --git a/FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift b/FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift index 123299ac1a72..8031fb95dcf5 100644 --- a/FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift +++ b/FirebaseVertexAI/Tests/Unit/GenerativeModelTests.swift @@ -13,6 +13,7 @@ // limitations under the License. import FirebaseAppCheckInterop +import FirebaseCore import XCTest @testable import FirebaseVertexAI @@ -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,