Skip to content

Commit

Permalink
Merge pull request #8 from StreamAMG/feature/pass-user-agent-header
Browse files Browse the repository at this point in the history
[ZEUS-4356] Pass user-agent header to the SDK
  • Loading branch information
artem-y-pamediagroup authored May 15, 2024
2 parents c73bf28 + 948eab2 commit 745f6cf
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 23 deletions.
5 changes: 3 additions & 2 deletions .swiftpm/xcode/xcshareddata/xcschemes/PlaybackSDK.xcscheme
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1520"
LastUpgradeVersion = "1540"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
Expand Down
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
"repositoryURL": "https://github.com/bitmovin/bitmovin-analytics-collector-ios.git",
"state": {
"branch": null,
"revision": "3feebb1db5f6bc2d3ad0b7241f3baea523e9ab9e",
"version": "3.6.0"
"revision": "3431fdbfb3098c0826b332ec52c869a30f20b947",
"version": "3.6.2"
}
},
{
"package": "BitmovinPlayer",
"repositoryURL": "https://github.com/bitmovin/player-ios.git",
"state": {
"branch": null,
"branch": "3.56.1",
"revision": "31ed8a5fb931bd600c5e1ca6d19c17d77762b56b",
"version": "3.56.1"
"version": null
}
},
{
Expand Down
9 changes: 4 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ let package = Package(
// Declare dependencies

// BitmovinPlayer
.package(name: "BitmovinPlayer",
url: "https://github.com/bitmovin/player-ios.git",
.exact("3.56.1")),

.package(url: "https://github.com/bitmovin/player-ios.git",
revision: "3.56.1"),

// other dependencies
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0")

Expand All @@ -34,7 +33,7 @@ let package = Package(
.target(
name: "PlaybackSDK",
dependencies: [
.product(name: "BitmovinPlayer", package: "BitmovinPlayer"),
.product(name: "BitmovinPlayer", package: "player-ios"),
]
),
.testTarget(
Expand Down
2 changes: 1 addition & 1 deletion Sources/PlaybackSDK/PlayBack API/PlayBackAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal protocol PlayBackAPI {
- andAuthorizationToken: Optional authorization token, can be nil for free videos.
- Returns: A publisher emitting the response model or an error.
*/
func getVideoDetails(forEntryId entryId: String, andAuthorizationToken: String?) -> AnyPublisher<PlaybackResponseModel, Error>
func getVideoDetails(forEntryId entryId: String, andAuthorizationToken: String?, userAgent: String?) -> AnyPublisher<PlaybackResponseModel, Error>
}

#endif
12 changes: 10 additions & 2 deletions Sources/PlaybackSDK/PlayBack API/PlayBackAPIService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ internal class PlayBackAPIService: PlayBackAPI {
- andAuthorizationToken: Optional authorization token, can be nil for free videos.
- Returns: A publisher emitting the response model or an error.
*/
func getVideoDetails(forEntryId entryId: String, andAuthorizationToken: String?) -> AnyPublisher<PlaybackResponseModel, Error> {
func getVideoDetails(
forEntryId entryId: String,
andAuthorizationToken: String?,
userAgent: String?
) -> AnyPublisher<PlaybackResponseModel, Error> {
guard let url = URL(string: "\(PlayBackSDKManager.shared.baseURL)/entry/\(entryId)") else {
return Fail(error: PlayBackAPIError.invalidPlaybackDataURL).eraseToAnyPublisher()
}
Expand All @@ -45,7 +49,11 @@ internal class PlayBackAPIService: PlayBackAPI {
if let authorizationTokenExist = andAuthorizationToken, !authorizationTokenExist.isEmpty {
request.addValue("Bearer \(authorizationTokenExist)", forHTTPHeaderField: "Authorization")
}


if let userAgent, !userAgent.isEmpty {
request.addValue(userAgent, forHTTPHeaderField: "User-Agent")
}

request.addValue(apiKey, forHTTPHeaderField: "x-api-key")

return URLSession.shared.dataTaskPublisher(for: request)
Expand Down
15 changes: 11 additions & 4 deletions Sources/PlaybackSDK/PlayBackSDKManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ public class PlayBackSDKManager {
// MARK: Private properties
private var playerInfoAPI: PlayerInformationAPI?
private var playBackAPI: PlayBackAPI?
private var userAgentHeader: String?
private var cancellables = Set<AnyCancellable>()

// MARK: Internal properties
/// Bitmovin license key.
internal var bitmovinLicense: String?
Expand All @@ -63,9 +64,10 @@ public class PlayBackSDKManager {
/// - Parameters:
/// - apiKey: The API key for initializing the SDK.
/// - baseURL: The base URL for API endpoints. Defaults to `nil`.
/// - userAgent: Custom `User-Agent` header to use with playback requests. Can be used if there was a custom header set to start session request. Defaults to `nil`
/// - completion: A closure to be called after initialization.
/// It receives a result indicating success or failure.
public func initialize(apiKey: String, baseURL: String? = nil, completion: @escaping (Result<String, Error>) -> Void) {
public func initialize(apiKey: String, baseURL: String? = nil, userAgent: String? = nil, completion: @escaping (Result<String, Error>) -> Void) {
guard !apiKey.isEmpty else {
completion(.failure(SDKError.initializationError))
return
Expand All @@ -76,6 +78,7 @@ public class PlayBackSDKManager {
}

amgAPIKey = apiKey
userAgentHeader = userAgent
playerInfoAPI = PlayerInformationAPIService(apiKey: apiKey)
let playBackAPIService = PlayBackAPIService(apiKey: apiKey)
self.playBackAPI = playBackAPIService
Expand Down Expand Up @@ -113,7 +116,7 @@ public class PlayBackSDKManager {
return
}

playerInfoAPIExist.getPlayerInformation()
playerInfoAPIExist.getPlayerInformation(userAgent: userAgentHeader)
.sink(receiveCompletion: { result in
switch result {
case .failure(let error):
Expand Down Expand Up @@ -154,7 +157,11 @@ public class PlayBackSDKManager {
}

// Call the /entry endpoint for the given entry ID
playBackAPIExist.getVideoDetails(forEntryId: entryId, andAuthorizationToken: andAuthorizationToken)
playBackAPIExist.getVideoDetails(
forEntryId: entryId,
andAuthorizationToken: andAuthorizationToken,
userAgent: userAgentHeader
)
.sink(receiveCompletion: { result in
switch result {
case .failure(let error):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import Foundation
import Combine

internal protocol PlayerInformationAPI {
func getPlayerInformation() -> AnyPublisher<PlayerInformationResponseModel, Error>
func getPlayerInformation(userAgent: String?) -> AnyPublisher<PlayerInformationResponseModel, Error>
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ internal class PlayerInformationAPIService: PlayerInformationAPI {
self.apiKey = apiKey
}

func getPlayerInformation() -> AnyPublisher<PlayerInformationResponseModel, Error> {
func getPlayerInformation(userAgent: String?) -> AnyPublisher<PlayerInformationResponseModel, Error> {
guard let url = URL(string: "\(PlayBackSDKManager.shared.baseURL)/player") else {
return Fail(error: PlayBackAPIError.invalidPlayerInformationURL).eraseToAnyPublisher()
}

var request = URLRequest(url: url)
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue(apiKey, forHTTPHeaderField: "x-api-key")

if let userAgent {
request.addValue(userAgent, forHTTPHeaderField: "User-Agent")
}

return URLSession.shared.dataTaskPublisher(for: request)
.map { $0.data }
.decode(type: PlayerInformationResponseModel.self, decoder: JSONDecoder())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import BitmovinPlayer
import SwiftUI

public class BitmovinPlayerPlugin: VideoPlayerPlugin {

private let playerConfig: PlayerConfig
private var player: BitMovinPlayerView?

Expand All @@ -25,7 +25,7 @@ public class BitmovinPlayerPlugin: VideoPlayerPlugin {
playerConfig.key = PlayBackSDKManager.shared.bitmovinLicense
self.playerConfig = playerConfig
self.name = "BitmovinPlayerPlugin"
self.version = "1.0"
self.version = "1.0.1"
}

func getPlayer() -> Player? {
Expand Down

0 comments on commit 745f6cf

Please sign in to comment.