[PreprocessHttpRequest Sample]: Dynamically adding tokens to URLs with preprocessHttpRequest

  • Web

    • Doc reference: Web Documentation
    • Code sample:
      networkConfig.preprocessHttpRequest = function(type, request) {
        // Append a token to the URL query string
        request.url += (request.url.includes('?') ? '&' : '?') + 'token=your_token_here';
      
        // Return a promise resolving with the modified request object
        return Promise.resolve(request);
      };
      
  • Android

    • Doc reference: Android Documentation
    • Code sample:
      NetworkConfig networkConfig = new NetworkConfig();
      PreprocessHttpRequestCallback preprocessHttpRequestCallback = new PreprocessHttpRequestCallback() {
          @Nullable
          @Override
          public Future<HttpRequest> preprocessHttpRequest(HttpRequestType httpRequestType, HttpRequest httpRequest) {
      
              ExecutorService executorService = Executors.newSingleThreadExecutor();
              Callable<HttpRequest> callable = () -> {
                  // Get the existing URL
                  String url = httpRequest.getUrl();
                  
                  // Add the token as a query parameter
                  String token = "your_token_here"; // Replace with your actual token
                  if (url != null && !url.isEmpty()) {
                      url += (url.contains("?") ? "&" : "?") + "token=" + token;
                      httpRequest.setUrl(url);
                  }
                  
                  return httpRequest;
              };
              Future<HttpRequest> future = executorService.submit(callable);
              return future;
          }
      };
      networkConfig.setPreprocessHttpRequestCallback(preprocessHttpRequestCallback);
      player.getConfig().setNetworkConfig(networkConfig);
      
  • iOS

    • Doc reference: iOS Documentation
    • Code sample:
      import SwiftUI
      import BitmovinPlayer
      
      // Define a class that conforms to PreprocessHttpRequestDelegate
      class RequestDelegate: NSObject, PreprocessHttpRequestDelegate {
        func preprocessHttpRequest(_ type: String, httpRequest: HttpRequest, completionHandler: @escaping (HttpRequest) -> Void) {
        let modifiedRequest = httpRequest
        if var urlComponents = URLComponents(url: httpRequest.url, resolvingAgainstBaseURL: false) {
            let tokenQueryItem = URLQueryItem(name: "token", value: "your_token_here") // Replace with your actual token
            urlComponents.queryItems = (urlComponents.queryItems ?? []) + [tokenQueryItem]
            if let modifiedURL = urlComponents.url {
                modifiedRequest.url = modifiedURL
            }
        }
        completionHandler(modifiedRequest)
      }
      }
      
      struct ContentView: View {
        @State private var player: Player?
        private let delegate = RequestDelegate()
      
        var body: some View {
            VideoPlayer(player: player)
                .onAppear {
                    initializePlayer()
                }
                .onDisappear {
                    player?.destroy()
                }
        }
      
        private func initializePlayer() {
            let config = PlayerConfig()
      
            // Create and configure networkConfig
            let networkConfig = NetworkConfig()
            networkConfig.preprocessHttpRequestDelegate = delegate
            
            // Assign networkConfig to the player configuration
            config.networkConfig = networkConfig
      
            player = PlayerFactory.create(playerConfig: config)
            
            guard let player = player else {
                return
            }
      
            // Create player view
            let playerView = PlayerView(player: player, frame: .zero)
            
            // Create a source item
            guard let sourceConfig = SourceConfig(url: URL(string: "https://your.content.url/manifest.m3u8"),
                                                  type: .hls) else {
                return
            }
      
            let source = SourceFactory.create(from: sourceConfig)
      
            // Load the source
            player.load(source: source)
        }
      }
      
      struct VideoPlayer: UIViewRepresentable {
        let player: Player?
      
        func makeUIView(context: Context) -> UIView {
            return PlayerView(player: player, frame: .zero)
        }
      
        func updateUIView(_ uiView: UIView, context: Context) {
        }
      }
      
      struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
      }
      
2 Likes