Skip to content
Draft
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
8 changes: 8 additions & 0 deletions ContentstackSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@
67AA39162EBA065000C0E2C0 /* StackInitializationTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 67AA39152EBA065000C0E2C0 /* StackInitializationTest.swift */; };
67AA39172EBA065000C0E2C0 /* StackInitializationTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 67AA39152EBA065000C0E2C0 /* StackInitializationTest.swift */; };
67AA39182EBA065000C0E2C0 /* StackInitializationTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 67AA39152EBA065000C0E2C0 /* StackInitializationTest.swift */; };
67B30E7B2FB6F98D00C9FA26 /* EntryVariantsAPITest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 67B30E7A2FB6F98D00C9FA26 /* EntryVariantsAPITest.swift */; };
67B30E7C2FB6F98D00C9FA26 /* EntryVariantsAPITest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 67B30E7A2FB6F98D00C9FA26 /* EntryVariantsAPITest.swift */; };
67B30E7D2FB6F98D00C9FA26 /* EntryVariantsAPITest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 67B30E7A2FB6F98D00C9FA26 /* EntryVariantsAPITest.swift */; };
67BA83032F6D36F3005006B8 /* config.json in Resources */ = {isa = PBXBuildFile; fileRef = 67BA83022F6D36ED005006B8 /* config.json */; };
67BA83042F6D36F3005006B8 /* config.json in Resources */ = {isa = PBXBuildFile; fileRef = 67BA83022F6D36ED005006B8 /* config.json */; };
67BA83052F6D36F3005006B8 /* config.json in Resources */ = {isa = PBXBuildFile; fileRef = 67BA83022F6D36ED005006B8 /* config.json */; };
Expand Down Expand Up @@ -472,6 +475,7 @@
67AA390D2EB9D49600C0E2C0 /* CSDefinitionsTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CSDefinitionsTest.swift; sourceTree = "<group>"; };
67AA39112EBA063300C0E2C0 /* TaxonomyUnitTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TaxonomyUnitTest.swift; sourceTree = "<group>"; };
67AA39152EBA065000C0E2C0 /* StackInitializationTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StackInitializationTest.swift; sourceTree = "<group>"; };
67B30E7A2FB6F98D00C9FA26 /* EntryVariantsAPITest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EntryVariantsAPITest.swift; sourceTree = "<group>"; };
67BA83022F6D36ED005006B8 /* config.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = config.json; sourceTree = "<group>"; };
67EE21DE2DDB3FFE005AC119 /* CSURLSessionDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CSURLSessionDelegate.swift; sourceTree = "<group>"; };
67EE222B2DE4868F005AC119 /* GlobalField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GlobalField.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -740,6 +744,7 @@
0F50EA1C244ED88C00E5D705 /* StackCacheAPITest.swift */,
470657532B5E785C00BBFF88 /* ContentTypeQueryAPITest.swift */,
470657572B5E788400BBFF88 /* EntryAPITest.swift */,
67B30E7A2FB6F98D00C9FA26 /* EntryVariantsAPITest.swift */,
4706575B2B5E78B300BBFF88 /* AssetQueryAPITest.swift */,
4706575F2B5E797300BBFF88 /* ContentstackTest.swift */,
47AAE08F2B60420E0098655A /* SyncAPITest.swift */,
Expand Down Expand Up @@ -1211,6 +1216,7 @@
0F60CA2F2428949C00ED67E6 /* EntryTest.swift in Sources */,
0F244FA724406A4A003C3F26 /* EntryQueriableTest.swift in Sources */,
67AA38EB2EB9D44800C0E2C0 /* CSDefinitionsTest.swift in Sources */,
67B30E7C2FB6F98D00C9FA26 /* EntryVariantsAPITest.swift in Sources */,
0F463113243B044F001CE1FA /* SyncTest.swift in Sources */,
67AA39142EBA063300C0E2C0 /* TaxonomyUnitTest.swift in Sources */,
67AA38F72EB9D46D00C0E2C0 /* QueryableRangeTest.swift in Sources */,
Expand Down Expand Up @@ -1307,6 +1313,7 @@
0F60CA302428949C00ED67E6 /* EntryTest.swift in Sources */,
0F244FA824406A4A003C3F26 /* EntryQueriableTest.swift in Sources */,
67AA38EC2EB9D44800C0E2C0 /* CSDefinitionsTest.swift in Sources */,
67B30E7D2FB6F98D00C9FA26 /* EntryVariantsAPITest.swift in Sources */,
0F463114243B044F001CE1FA /* SyncTest.swift in Sources */,
67AA39132EBA063300C0E2C0 /* TaxonomyUnitTest.swift in Sources */,
67AA38F62EB9D46D00C0E2C0 /* QueryableRangeTest.swift in Sources */,
Expand Down Expand Up @@ -1403,6 +1410,7 @@
0F60CA312428949C00ED67E6 /* EntryTest.swift in Sources */,
0F244FA924406A4A003C3F26 /* EntryQueriableTest.swift in Sources */,
67AA38EA2EB9D44800C0E2C0 /* CSDefinitionsTest.swift in Sources */,
67B30E7B2FB6F98D00C9FA26 /* EntryVariantsAPITest.swift in Sources */,
0F463115243B044F001CE1FA /* SyncTest.swift in Sources */,
67AA39122EBA063300C0E2C0 /* TaxonomyUnitTest.swift in Sources */,
67AA38F82EB9D46D00C0E2C0 /* QueryableRangeTest.swift in Sources */,
Expand Down
10 changes: 0 additions & 10 deletions Sources/Entry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,6 @@ public class Entry: EntryQueryable, CachePolicyAccessible {
return query
}

public func variants(uid: String) -> Self {
self.headers["x-cs-variant-uid"] = uid
return self
}

public func variants(uids: [String]) -> Self {
self.headers["x-cs-variant-uid"] = uids.joined(separator: ",")
return self
}

/// The Query parameters dictionary that are converted to `URLComponents`.
/// - Parameters:
/// - key: The key for header parameter,
Expand Down
91 changes: 88 additions & 3 deletions Sources/EntryQuaryable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,92 @@ extension EntryQueryable {
}
return query
}




/// Sets the variant UID header so the request fetches a specific entry variant.
/// - Parameter uid: A single variant UID.
/// - Returns: A reference to the receiving object to enable chaining.
///
/// Example usage:
/// ```
/// stack.contentType(uid: contentTypeUID).entry(uid: entryUID)
/// .variants(uid: "variant_uid")
/// .fetch { ... }
///
/// stack.contentType(uid: contentTypeUID).entry().query()
/// .variants(uid: "variant_uid")
/// .find { ... }
/// ```
@discardableResult
public func variants(uid: String) -> Self {
self.headers["x-cs-variant-uid"] = uid
return self
}

/// Sets the variant UID header for multiple variant UIDs.
/// - Parameter uids: An array of variant UIDs.
/// - Returns: A reference to the receiving object to enable chaining.
///
/// Example usage:
/// ```
/// stack.contentType(uid: contentTypeUID).entry(uid: entryUID)
/// .variants(uids: ["v1", "v2"])
/// .fetch { ... }
///
/// stack.contentType(uid: contentTypeUID).entry().query()
/// .variants(uids: ["v1", "v2"])
/// .find { ... }
/// ```
@discardableResult
public func variants(uids: [String]) -> Self {
self.headers["x-cs-variant-uid"] = uids.joined(separator: ",")
return self
}

/// Sets the variant UID and branch headers so the request fetches a specific entry variant
/// scoped to the given branch.
/// - Parameters:
/// - uid: A single variant UID.
/// - branch: The branch name to scope the request to.
/// - Returns: A reference to the receiving object to enable chaining.
///
/// Example usage:
/// ```
/// stack.contentType(uid: contentTypeUID).entry(uid: entryUID)
/// .variants(uid: "variant_uid", branch: "staging")
/// .fetch { ... }
///
/// stack.contentType(uid: contentTypeUID).entry().query()
/// .variants(uid: "variant_uid", branch: "staging")
/// .find { ... }
/// ```
@discardableResult
public func variants(uid: String, branch: String) -> Self {
self.headers["x-cs-variant-uid"] = uid
self.headers["branch"] = branch
return self
}

/// Sets the variant UIDs and branch headers so the request fetches specific entry variants
/// scoped to the given branch.
/// - Parameters:
/// - uids: An array of variant UIDs.
/// - branch: The branch name to scope the request to.
/// - Returns: A reference to the receiving object to enable chaining.
///
/// Example usage:
/// ```
/// stack.contentType(uid: contentTypeUID).entry(uid: entryUID)
/// .variants(uids: ["v1", "v2"], branch: "staging")
/// .fetch { ... }
///
/// stack.contentType(uid: contentTypeUID).entry().query()
/// .variants(uids: ["v1", "v2"], branch: "staging")
/// .find { ... }
/// ```
@discardableResult
public func variants(uids: [String], branch: String) -> Self {
self.headers["x-cs-variant-uid"] = uids.joined(separator: ",")
self.headers["branch"] = branch
return self
}
}
71 changes: 71 additions & 0 deletions Tests/DVRRecordings/EntryVariants.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"name" : "EntryVariants",
"interactions" : [
{
"request" : {
"url" : "https:\/\/cdn.contentstack.io\/v3\/content_types\/blog\/entries?environment=environment",
"headers" : {
"access_token" : "delivery_token",
"api_key" : "api_key",
"X-User-Agent" : "contentstack-swift\/1.0.0",
"x-cs-variant-uid" : "cs7587bd1cb52b5479"
},
"method" : "GET"
},
"recorded_at" : 1715769600.0,
"response" : {
"url" : "https:\/\/cdn.contentstack.io\/v3\/content_types\/blog\/entries?environment=environment",
"status" : 200,
"headers" : {
"Content-Type" : "application\/json",
"Access-Control-Allow-Origin" : "*"
},
"body" : {
"entries" : [
{
"uid" : "blog_entry_uid_1",
"title" : "Variant Test Blog Post",
"locale" : "en-us",
"created_at" : "2024-01-10T05:47:46.833Z",
"updated_at" : "2024-04-20T07:05:15.592Z",
"created_by" : "user_uid_1",
"updated_by" : "user_uid_1"
}
]
}
}
},
{
"request" : {
"url" : "https:\/\/cdn.contentstack.io\/v3\/content_types\/blog\/entries\/blog_entry_uid_1?environment=environment",
"headers" : {
"access_token" : "delivery_token",
"api_key" : "api_key",
"X-User-Agent" : "contentstack-swift\/1.0.0",
"x-cs-variant-uid" : "cs7587bd1cb52b5479"
},
"method" : "GET"
},
"recorded_at" : 1715769600.0,
"response" : {
"url" : "https:\/\/cdn.contentstack.io\/v3\/content_types\/blog\/entries\/blog_entry_uid_1?environment=environment",
"status" : 200,
"headers" : {
"Content-Type" : "application\/json",
"Access-Control-Allow-Origin" : "*"
},
"body" : {
"entry" : {
"uid" : "blog_entry_uid_1",
"title" : "Variant Test Blog Post",
"locale" : "en-us",
"created_at" : "2024-01-10T05:47:46.833Z",
"updated_at" : "2024-04-20T07:05:15.592Z",
"created_by" : "user_uid_1",
"updated_by" : "user_uid_1"
}
}
}
}
]
}
14 changes: 14 additions & 0 deletions Tests/EntryTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,19 @@ class EntryTest: XCTestCase {
XCTAssertEqual(entry.headers.keys.count, 1)
XCTAssertEqual(entry.headers["x-cs-variant-uid"], "variant1,variant2,variant3")
}

func testSingleVariantWithBranch() {
let entry = makeEntrySut(contentTypeuid: "Content_type", entryUid: "entry_uid").variants(uid: "variant1", branch: "staging")
XCTAssertEqual(entry.headers.keys.count, 2)
XCTAssertEqual(entry.headers["x-cs-variant-uid"], "variant1")
XCTAssertEqual(entry.headers["branch"], "staging")
}

func testMultipleVariantsWithBranch() {
let entry = makeEntrySut(contentTypeuid: "Content_type", entryUid: "entry_uid").variants(uids: ["variant1", "variant2"], branch: "staging")
XCTAssertEqual(entry.headers.keys.count, 2)
XCTAssertEqual(entry.headers["x-cs-variant-uid"], "variant1,variant2")
XCTAssertEqual(entry.headers["branch"], "staging")
}
}

Loading