Skip to content

Commit

Permalink
Add platform tags in x-goog-api-client (#12680)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewheard authored and paulb777 committed Apr 2, 2024
1 parent cbc3e8c commit f1f2338
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
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)",
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

0 comments on commit f1f2338

Please sign in to comment.