/// <summary>
        /// Initializes the service before MakeApiCall.
        /// </summary>
        /// <param name="methodName">Name of the method.</param>
        /// <param name="parameters">The method parameters.</param>
        protected override void InitForCall(string methodName, object[] parameters)
        {
            DfaAppConfig config      = this.User.Config as DfaAppConfig;
            string       oAuthHeader = null;

            if (this.GetType().Name == "LoginRemoteService")
            {
                // The choice of OAuth comes only when calling LoginRemoteService.
                // All other services will still use the login token.
                if (config.AuthorizationMethod == DfaAuthorizationMethod.OAuth2)
                {
                    if (this.User.OAuthProvider != null)
                    {
                        oAuthHeader = this.User.OAuthProvider.GetAuthHeader();
                    }
                    else
                    {
                        throw new DfaApiException(null, DfaErrorMessages.OAuthProviderCannotBeNull);
                    }
                }
            }
            else
            {
                if (this.Token == null)
                {
                    this.Token = LoginUtil.GetAuthenticationToken(this.User, this.Signature.Version);
                }
            }

            ContextStore.AddKey("OAuthHeader", oAuthHeader);
            ContextStore.AddKey("RequestHeader", requestHeader);
            ContextStore.AddKey("Token", Token);

            base.InitForCall(methodName, parameters);
        }
        public void TestHandleMessage()
        {
            try {
                ContextStore.AddKey("SoapMethod", "mutate");

                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(SoapMessages_v201506.UpdateCampaign);
                XmlElement xRequest = (XmlElement)xDoc.SelectSingleNode("/Example/SOAP/Response");
                xDoc.LoadXml(xRequest.InnerText);
                CampaignService service = (CampaignService)user.GetService(
                    AdWordsService.v201506.CampaignService);

                AdWordsCallListener.Instance.HandleMessage(xDoc, service, SoapMessageDirection.IN);

                // API no longer returns operation count.
                Assert.AreEqual(user.GetTotalOperationCount(), 0);
                Assert.AreEqual(user.GetOperationCountForLastCall(), 0);

                ApiCallEntry[] callEntries = user.GetCallDetails();
                Assert.AreEqual(callEntries.Length, 1);
                ApiCallEntry callEntry = user.GetCallDetails()[0];

                // API no longer returns operation count.
                Assert.AreEqual(callEntry.OperationCount, 0);

                Assert.AreEqual(callEntry.Method, "mutate");
                Assert.AreEqual(callEntry.Service.Signature.ServiceName, "CampaignService");
            } finally {
                ContextStore.RemoveKey("SoapMethod");
            }
        }
 public void TestCleanupAfterCall()
 {
     listener = new MockTraceListener(config);
     ContextStore.AddKey("SoapRequest", "SoapRequest");
     ContextStore.AddKey("SoapResponse", "SoapResponse");
     ContextStore.AddKey("FormattedSoapLog", "FormattedSoapLog");
     ContextStore.AddKey("FormattedRequestLog", "FormattedRequestLog");
     listener.CleanupAfterCall();
     Assert.Null(ContextStore.GetValue("SoapRequest"));
     Assert.Null(ContextStore.GetValue("SoapResponse"));
     Assert.Null(ContextStore.GetValue("FormattedSoapLog"));
     Assert.Null(ContextStore.GetValue("FormattedRequestLog"));
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Initializes the service before MakeApiCall.
        /// </summary>
        /// <param name="methodName">Name of the method.</param>
        /// <param name="parameters">The method parameters.</param>
        protected override void InitForCall(string methodName, object[] parameters)
        {
            DfpAppConfig  config      = this.User.Config as DfpAppConfig;
            string        oAuthHeader = null;
            RequestHeader header      = GetRequestHeader();

            if (header == null)
            {
                throw new DfpApiException(null, DfpErrorMessages.FailedToSetAuthorizationHeader);
            }

            if (!(this.GetType().Name == "NetworkService" && (methodName == "getAllNetworks" ||
                                                              methodName == "makeTestNetwork")))
            {
                if (string.IsNullOrEmpty(header.networkCode))
                {
                    throw new SoapHeaderException("networkCode header is required in all API versions. " +
                                                  "The only exceptions are NetworkService.getAllNetworks and " +
                                                  "NetworkService.makeTestNetwork.", XmlQualifiedName.Empty);
                }
            }

            if (string.IsNullOrEmpty(header.applicationName) || header.applicationName.Contains(
                    DfpAppConfig.DEFAULT_APPLICATION_NAME))
            {
                throw new ApplicationException(DfpErrorMessages.RequireValidApplicationName);
            }

            if (config.AuthorizationMethod == DfpAuthorizationMethod.OAuth2)
            {
                if (this.User.OAuthProvider != null)
                {
                    oAuthHeader = this.User.OAuthProvider.GetAuthHeader();
                }
                else
                {
                    throw new DfpApiException(null, DfpErrorMessages.OAuthProviderCannotBeNull);
                }
            }
            else if (config.AuthorizationMethod == DfpAuthorizationMethod.ClientLogin)
            {
                string authToken = (!string.IsNullOrEmpty(config.AuthToken)) ? config.AuthToken :
                                   new AuthToken(config, SERVICE_NAME).GetToken();
                ClientLogin clientLogin = (header.authentication as ClientLogin) ?? new ClientLogin();
                clientLogin.token     = authToken;
                header.authentication = clientLogin;
            }

            ContextStore.AddKey("OAuthHeader", oAuthHeader);
            base.InitForCall(methodName, parameters);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Initializes the service before MakeApiCall.
        /// </summary>
        /// <param name="methodName">Name of the method.</param>
        /// <param name="parameters">The method parameters.</param>
        protected override void InitForCall(string methodName, object[] parameters)
        {
            AdWordsAppConfig config = this.User.Config as AdWordsAppConfig;
            RequestHeader    header = GetRequestHeader();

            if (string.IsNullOrEmpty(header.developerToken))
            {
                throw new ArgumentNullException(AdWordsErrorMessages.DeveloperTokenCannotBeEmpty);
            }

            if (string.IsNullOrEmpty(header.clientCustomerId))
            {
                TraceUtilities.WriteGeneralWarnings(AdWordsErrorMessages.ClientCustomerIdIsEmpty);
            }

            // Validate Express business headers.
            if (this.Signature.ServiceName == "PromotionService")
            {
                if (header.expressBusinessIdSpecified && header.pageIdSpecified)
                {
                    throw new ArgumentException(AdWordsErrorMessages.OnlyOneExpressHeaderShouldBeSpecified);
                }
                else if (!header.expressBusinessIdSpecified && !header.pageIdSpecified)
                {
                    throw new ArgumentException(AdWordsErrorMessages.MissingExpressHeaders + ' ' +
                                                AdWordsErrorMessages.OnlyOneExpressHeaderShouldBeSpecified);
                }
            }
            else
            {
                if (header.expressBusinessIdSpecified || header.pageIdSpecified)
                {
                    throw new ArgumentException(AdWordsErrorMessages.ExpressHeadersShouldNotBeSpecified);
                }
            }

            header.userAgent = config.GetUserAgent();

            string oAuthHeader = null;

            if (this.User.OAuthProvider != null)
            {
                oAuthHeader = this.User.OAuthProvider.GetAuthHeader();
            }
            else
            {
                throw new AdWordsApiException(null, AdWordsErrorMessages.OAuthProviderCannotBeNull);
            }
            ContextStore.AddKey("OAuthHeader", oAuthHeader);
            base.InitForCall(methodName, parameters);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Performs the SOAP and HTTP logging.
        /// </summary>
        /// <param name="request">The request information.</param>
        /// <param name="response">The response information.</param>
        private void PerformLogging(RequestInfo request, ResponseInfo response)
        {
            LogEntry logEntry = new LogEntry(config, dateTimeProvider, TraceWriter);

            PopulateRequestInfo(ref request);
            logEntry.LogRequest(request, GetFieldsToMask(), new SoapTraceFormatter());

            PopulateResponseInfo(ref response);
            logEntry.LogResponse(response, GetFieldsToMask(), new SoapTraceFormatter());
            logEntry.Flush();

            ContextStore.AddKey("FormattedSoapLog", logEntry.DetailedLog);
            ContextStore.AddKey("FormattedRequestLog", logEntry.SummaryLog);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Initializes the service before MakeApiCall.
        /// </summary>
        /// <param name="methodName">Name of the method.</param>
        /// <param name="parameters">The method parameters.</param>
        protected override void InitForCall(string methodName, object[] parameters)
        {
            DfpAppConfig  config      = this.User.Config as DfpAppConfig;
            string        oAuthHeader = null;
            RequestHeader header      = GetRequestHeader();

            if (header == null)
            {
                throw new DfpApiException(null, DfpErrorMessages.FailedToSetAuthorizationHeader);
            }

            if (!(this.GetType().Name == "NetworkService" && (methodName == "getAllNetworks" ||
                                                              methodName == "makeTestNetwork")))
            {
                if (string.IsNullOrEmpty(header.networkCode))
                {
                    throw new SoapHeaderException("networkCode header is required in all API versions. " +
                                                  "The only exceptions are NetworkService.getAllNetworks and " +
                                                  "NetworkService.makeTestNetwork.", XmlQualifiedName.Empty);
                }
            }

            // TODO: (cseeley) add tests for this MOE:strip_line
            if (string.IsNullOrWhiteSpace(header.applicationName) || header.applicationName.Contains(
                    DfpAppConfig.DEFAULT_APPLICATION_NAME))
            {
                throw new ApplicationException(DfpErrorMessages.RequireValidApplicationName);
            }

            if (config.AuthorizationMethod == DfpAuthorizationMethod.OAuth2)
            {
                if (this.User.OAuthProvider != null)
                {
                    oAuthHeader = this.User.OAuthProvider.GetAuthHeader();
                }
                else
                {
                    throw new DfpApiException(null, DfpErrorMessages.OAuthProviderCannotBeNull);
                }
            }
            else
            {
                throw new DfpApiException(null, DfpErrorMessages.UnsupportedAuthorizationMethod);
            }

            ContextStore.AddKey("OAuthHeader", oAuthHeader);
            base.InitForCall(methodName, parameters);
        }
 /// <summary>
 /// Handles the SOAP message.
 /// </summary>
 /// <param name="soapMessage">The SOAP message.</param>
 /// <param name="service">The SOAP service.</param>
 /// <param name="direction">The direction of message.</param>
 public void HandleMessage(XmlDocument soapMessage, AdsClient service,
                           SoapMessageDirection direction)
 {
     if (direction == SoapMessageDirection.OUT)
     {
         ContextStore.AddKey("SoapRequest", soapMessage.OuterXml);
     }
     else
     {
         ContextStore.AddKey("SoapResponse", soapMessage.OuterXml);
     }
     if (direction == SoapMessageDirection.IN)
     {
         PerformLogging(service, (string)ContextStore.GetValue("SoapRequest"),
                        (string)ContextStore.GetValue("SoapResponse"));
     }
 }
        /// <summary>
        /// Performs the SOAP and HTTP logging.
        /// </summary>
        /// <param name="service">The SOAP service.</param>
        /// <param name="soapResponse">The SOAP response xml.</param>
        /// <param name="soapRequest">The SOAP request xml.</param>
        private void PerformLogging(AdsClient service, string soapRequest, string soapResponse)
        {
            if (service == null || service.User == null || soapRequest == null || soapResponse == null)
            {
                return;
            }

            bool isFailure = service.LastResponse != null && service.LastResponse is HttpWebResponse &&
                             (service.LastResponse as HttpWebResponse).StatusCode ==
                             HttpStatusCode.InternalServerError;

            LogEntry logEntry = new LogEntry(config, dateTimeProvider);

            logEntry.LogRequestDetails(service.LastRequest, soapRequest, GetFieldsToMask(),
                                       new SoapTraceFormatter());
            logEntry.LogResponseDetails(service.LastResponse, soapResponse, new HashSet <string>(),
                                        new DefaultBodyFormatter());
            logEntry.LogRequestSummary(service.LastRequest, GetSummaryRequestLogs(soapRequest));
            logEntry.LogResponseSummary(isFailure, GetSummaryResponseLogs(soapResponse));
            logEntry.Flush();

            ContextStore.AddKey("FormattedSoapLog", logEntry.DetailedLog);
            ContextStore.AddKey("FormattedRequestLog", logEntry.SummaryLog);
        }