Ejemplo n.º 1
0
        /// <summary>
        /// Gets a custom exception that wraps the SOAP exception thrown
        /// by the server.
        /// </summary>
        /// <param name="ex">SOAPException that was thrown by the server.</param>
        /// <returns>A custom exception object that wraps the SOAP exception.
        /// </returns>
        protected override Exception GetCustomException(SoapException ex)
        {
            string defaultNs = GetDefaultNamespace();

            if (!string.IsNullOrEmpty(defaultNs))
            {
                // Extract the ApiExceptionFault node.
                XmlElement faultNode = GetFaultNode(ex, defaultNs, "ApiExceptionFault");

                if (faultNode != null)
                {
                    try {
                        DfpApiException dfpException = new DfpApiException(
                            SerializationUtilities.DeserializeFromXmlTextCustomRootNs(
                                faultNode.OuterXml,
                                Assembly.GetExecutingAssembly().GetType(
                                    this.GetType().Namespace + ".ApiException"), defaultNs,
                                "ApiExceptionFault"),
                            ex.Message, ex);
                        return(dfpException);
                    } catch (Exception) {
                        // deserialization failed, but we can safely ignore it.
                    }
                }
            }
            return(new DfpApiException(null, ex.Message, ex));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Verifies whether GetAccountAlerts is serializing the request correctly.
        /// </summary>
        /// <param name="requestUri">The request URI.</param>
        /// <param name="headers">The headers.</param>
        /// <param name="body">The body.</param>
        private void VerifyGetAccountAlertsRequest(Uri requestUri, WebHeaderCollection headers,
                                                   string body)
        {
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(body);
            XmlElement    node     = (XmlElement)doc.GetElementsByTagName("selector")[0];
            AlertSelector selector =
                (AlertSelector)SerializationUtilities.DeserializeFromXmlTextCustomRootNs(
                    node.OuterXml, typeof(AlertSelector),
                    "https://adwords.google.com/api/adwords/mcm/v201406", "selector");

            Assert.AreEqual(selector.query.filterSpec, FilterSpec.ALL);
            Assert.AreEqual(selector.query.clientSpec, ClientSpec.ALL);
            Assert.AreEqual(selector.query.triggerTimeSpec, TriggerTimeSpec.ALL_TIME);
            Assert.Contains(AlertSeverity.GREEN, selector.query.severities);
            Assert.Contains(AlertSeverity.YELLOW, selector.query.severities);
            Assert.Contains(AlertSeverity.RED, selector.query.severities);

            foreach (AlertType alertType in Enum.GetValues(typeof(AlertType)))
            {
                Assert.Contains(alertType, selector.query.types);
            }

            Assert.AreEqual(selector.paging.startIndex, 0);
            Assert.AreEqual(selector.paging.numberResults, 500);
        }
        /// <summary>
        /// Gets a custom exception that wraps the SOAP exception thrown
        /// by the server.
        /// </summary>
        /// <param name="ex">SOAPException that was thrown by the server.</param>
        /// <returns>A custom exception object that wraps the SOAP exception.
        /// </returns>
        protected override Exception GetCustomException(SoapException exception)
        {
            string defaultNs = GetDefaultNamespace();

            if (!string.IsNullOrEmpty(defaultNs) && exception.Detail != null)
            {
                // Extract the ApiExceptionFault node.
                XmlElement faultNode = GetFaultNode(exception, defaultNs, "ApiExceptionFault");

                if (faultNode != null)
                {
                    try {
                        AdWordsApiException awapiException = new AdWordsApiException(
                            SerializationUtilities.DeserializeFromXmlTextCustomRootNs(
                                faultNode.OuterXml, Assembly.GetExecutingAssembly().GetType(
                                    this.GetType().Namespace + ".ApiException"), defaultNs, "ApiExceptionFault"),
                            AdWordsErrorMessages.AnApiExceptionOccurred, exception);
                        if (AdWordsErrorHandler.IsOAuthTokenExpiredError(awapiException))
                        {
                            return(new AdWordsCredentialsExpiredException(
                                       (string)ContextStore.GetValue("OAuthHeader")));
                        }
                        else
                        {
                            return(awapiException);
                        }
                    } catch (Exception) {
                        // deserialization failed, but we can safely ignore it.
                    }
                }
            }
            return(new AdWordsApiException(null, AdWordsErrorMessages.AnApiExceptionOccurred, exception));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Throws an AdsException if the response was a SOAP fault.
        /// </summary>
        /// <param name="reply">The response Message</param>
        /// <param name="correlationState">The correlation state returned by BeforeSendRequest</param>
        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            if (reply.IsFault)
            {
                StringBuilder xmlStringBuilder = new StringBuilder();
                using (XmlWriter xmlWriter = XmlWriter.Create(xmlStringBuilder, xmlWriterSettings))
                    using (MessageBuffer buffer = reply.CreateBufferedCopy(Int32.MaxValue)) {
                        // Message can only be read once, so replace it with a copy.
                        reply = buffer.CreateMessage();
                        buffer.CreateMessage().WriteBody(xmlWriter);
                    }

                // Try locating the ApiExceptionFault node and deserializing it. Make sure to ignore
                // the namespace and look only for the local name.
                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(xmlStringBuilder.ToString());
                XmlElement faultNode = (XmlElement)xDoc.SelectSingleNode(FAULT_ELEMENT_XPATH);

                if (faultNode != null)
                {
                    // Deserialize the correct exception type and raise it.
                    string faultNodeNamespaceUri = faultNode.NamespaceURI;
                    string faultNodeContents     = faultNode.OuterXml;
                    object apiError = SerializationUtilities.DeserializeFromXmlTextCustomRootNs(
                        faultNodeContents,
                        ErrorType,
                        faultNodeNamespaceUri,
                        FAULT_ELEMENT_NAME);
                    throw (TException)Activator.CreateInstance(
                              typeof(TException),
                              new object[] { apiError });
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Verifies whether UpdateCampaign example is serializing the request
        /// correctly.
        /// </summary>
        /// <param name="requestUri">The request URI.</param>
        /// <param name="headers">The headers.</param>
        /// <param name="body">The body.</param>
        private void VerifyUpdateCampaignRequest(Uri requestUri, WebHeaderCollection headers,
                                                 string body)
        {
            XmlDocument       doc  = XmlUtilities.CreateDocument(body);
            XmlElement        node = (XmlElement)doc.GetElementsByTagName("operations")[0];
            CampaignOperation campaignOperation =
                (CampaignOperation)SerializationUtilities.DeserializeFromXmlTextCustomRootNs(
                    node.OuterXml, typeof(CampaignOperation),
                    "https://adwords.google.com/api/adwords/cm/v201605", "operations");

            Assert.AreEqual(campaignOperation.@operator, Operator.SET);
            Campaign campaign = campaignOperation.operand;

            Assert.AreEqual(campaign.id, 12345);
            Assert.AreEqual(campaign.status, CampaignStatus.PAUSED);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Throws an AdsException if the response was a SOAP fault.
 /// </summary>
 /// <param name="reply">The response Message</param>
 /// <param name="correlationState">The correlation state returned by BeforeSendRequest</param>
 public void AfterReceiveReply(ref Message reply, object correlationState)
 {
     if (reply.IsFault)
     {
         using (MessageBuffer buffer = reply.CreateBufferedCopy(Int32.MaxValue)) {
             // Message can only be read once, so replace it with a copy.
             reply = buffer.CreateMessage();
             Message   copy   = buffer.CreateMessage();
             XmlReader reader = copy.GetReaderAtBodyContents();
             if (reader.ReadToDescendant(FAULT_ELEMENT_NAME))
             {
                 string ns       = reader.GetAttribute("xmlns");
                 object apiError = SerializationUtilities.DeserializeFromXmlTextCustomRootNs(
                     reader.ReadOuterXml(),
                     ErrorType,
                     ns,
                     FAULT_ELEMENT_NAME);
                 throw (TException)Activator.CreateInstance(
                           typeof(TException),
                           new object[] { apiError });
             }
         }
     }
 }