/// <summary>
 /// Initializes a new instance of the <see cref="MediaServicesClassFactory" /> class.
 /// </summary>
 /// <param name="azureMediaServicesEndpoint">The Windows Azure Media Services endpoint to use.</param>
 /// <param name="dataServiceAdapter">The data service adapter.</param>
 /// <param name="serviceVersionAdapter">The service version adapter.</param>
 /// <param name="mediaContext">The <seealso cref="CloudMediaContext" /> instance.</param>
 /// <param name="userAgentAdapter">The user agent request adapter</param>
 public AzureMediaServicesClassFactory(Uri azureMediaServicesEndpoint, OAuthDataServiceAdapter dataServiceAdapter, ServiceVersionAdapter serviceVersionAdapter, MediaContextBase mediaContext, UserAgentAdapter userAgentAdapter)
 {
     _dataServiceAdapter = dataServiceAdapter;
     _serviceVersionAdapter = serviceVersionAdapter;
     _mediaContext = mediaContext;
     _userAgentAdapter = userAgentAdapter;
     _azureMediaServicesEndpoint = CreateAzureMediaServicesEndPoint(azureMediaServicesEndpoint, mediaContext);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="MediaServicesClassFactory" /> class.
 /// </summary>
 /// <param name="azureMediaServicesEndpoint">The Windows Azure Media Services endpoint to use.</param>
 /// <param name="mediaContext">The <seealso cref="CloudMediaContext" /> instance.</param>
 public AzureMediaServicesClassFactory(Uri azureMediaServicesEndpoint, CloudMediaContext mediaContext)
 {
     _dataServiceAdapter = new OAuthDataServiceAdapter(mediaContext.Credentials, NimbusRestApiCertificateThumbprint,NimbusRestApiCertificateSubject);
     _serviceVersionAdapter = new ServiceVersionAdapter(KnownApiVersions.Current);
     _userAgentAdapter = new UserAgentAdapter(KnownClientVersions.Current);
     _mediaContext = mediaContext;
     _azureMediaServicesEndpoint = CreateAzureMediaServicesEndPoint(azureMediaServicesEndpoint, mediaContext);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="MediaServicesClassFactory" /> class.
 /// </summary>
 /// <param name="azureMediaServicesEndpoint">The Windows Azure Media Services endpoint to use.</param>
 /// <param name="mediaContext">The <seealso cref="CloudMediaContext" /> instance.</param>
 public AzureMediaServicesClassFactory(Uri azureMediaServicesEndpoint, CloudMediaContext mediaContext)
 {
     _dataServiceAdapter = new OAuthDataServiceAdapter(mediaContext.TokenProvider);
     _serviceVersionAdapter = new ServiceVersionAdapter(KnownApiVersions.Current);
     _userAgentAdapter = new UserAgentAdapter(KnownClientVersions.Current);
     _mediaContext = mediaContext;
     _azureMediaServicesEndpoint = CreateAzureMediaServicesEndPoint(azureMediaServicesEndpoint, mediaContext);
 }
        public void UserAgentHeaderPresentAndContainsVersion()
        {
            var mock = new MockRepository(MockBehavior.Loose);
            var request = mock.Create<WebRequest>();
            var headers = new WebHeaderCollection();
            request.SetupProperty(c => c.Headers, headers);
            UserAgentAdapter adapter = new UserAgentAdapter(new Version(1, 0));
            adapter.AddUserAgentToRequest(request.Object);
            Assert.AreEqual(1, request.Object.Headers.Count);

            Assert.IsNotNull(request.Object.Headers[HttpRequestHeader.UserAgent]);
            Assert.AreEqual("Azure Media Services .NET SDK v1.0", request.Object.Headers[HttpRequestHeader.UserAgent]);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MediaServicesClassFactory" /> class.
        /// </summary>
        /// <param name="azureMediaServicesEndpoint">The Windows Azure Media Services endpoint to use.</param>
        /// <param name="dataServiceAdapter">The data service adapter.</param>
        /// <param name="serviceVersionAdapter">The service version adapter.</param>
        /// <param name="mediaContext">The <seealso cref="CloudMediaContext" /> instance.</param>
        /// <param name="userAgentAdapter">The user agent request adapter</param>
        public AzureMediaServicesClassFactory(Uri azureMediaServicesEndpoint, OAuthDataServiceAdapter dataServiceAdapter, ServiceVersionAdapter serviceVersionAdapter, MediaContextBase mediaContext, UserAgentAdapter userAgentAdapter)
        {
            this._dataServiceAdapter = dataServiceAdapter;
            this._serviceVersionAdapter = serviceVersionAdapter;
            this._mediaContext = mediaContext;
            _userAgentAdapter = userAgentAdapter;
            var clientRequestIdAdapter = new ClientRequestIdAdapter();

            string cacheKey = string.Format(
                "{0},{1}",
                mediaContext.Credentials.ClientId,
                azureMediaServicesEndpoint.ToString());

            this._azureMediaServicesEndpoint = _endpointCache.GetOrAdd(
                cacheKey,
                () => GetAccountApiEndpoint(this._dataServiceAdapter, this._serviceVersionAdapter, azureMediaServicesEndpoint, userAgentAdapter,clientRequestIdAdapter),
                () => mediaContext.Credentials.TokenExpiration);
        }
        private Uri GetAccountApiEndpoint(OAuthDataServiceAdapter dataServiceAdapter, ServiceVersionAdapter versionAdapter, Uri apiServer, UserAgentAdapter userAgentAdapter, IWebRequestAdapter clientRequestIdAdapter)
        {
            MediaRetryPolicy retryPolicy = new MediaRetryPolicy(
                GetWebRequestTransientErrorDetectionStrategy(),
                RetryStrategyFactory.DefaultStrategy());

            Uri apiEndpoint = null;
            retryPolicy.ExecuteAction(
                    () =>
                    {
                        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiServer);
                        request.AllowAutoRedirect = false;
                        dataServiceAdapter.AddAccessTokenToRequest(request);
                        versionAdapter.AddVersionToRequest(request);
                        userAgentAdapter.AddUserAgentToRequest(request);
                        clientRequestIdAdapter.AddClientRequestId(request);

                        using (WebResponse response = request.GetResponse())
                        {
                            apiEndpoint = GetAccountApiEndpointFromResponse(response);
                        }
                    }
                );

            return apiEndpoint;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="CloudMediaContext"/> class.
 /// </summary>
 /// <param name="apiServer">A <see cref="Uri"/> representing the API endpoint.</param>
 /// <param name="credentials">Microsoft WindowsAzure Media Services credentials.</param>
 public CloudMediaContext(Uri apiServer, MediaServicesCredentials credentials)
 {
     this.apiServer = apiServer;
     this.ParallelTransferThreadCount = 10;
     this.NumberOfConcurrentTransfers = 2;
     this.Credentials = credentials;
     dataServiceAdapter = new OAuthDataServiceAdapter(credentials, NimbusRestApiCertificateThumbprint, NimbusRestApiCertificateSubject);
     versionAdapter = new ServiceVersionAdapter(KnownApiVersions.Current);
     userAgentAdapter = new UserAgentAdapter(KnownClientVersions.Current);
 }