/// <summary> /// Parses a failed xml request into an ApiException. /// </summary> /// <param name="nav">The XPath navigator for the XML response.</param> /// <returns>The <see cref="ApiException"/>.</returns> static public ApiException FromXmlErrors(XPathNavigator nav) { ApiException ex = new ApiException(); XPathNodeIterator i = nav.Select("/eBay/Errors/Error"); while (i.MoveNext()) { ErrorType err = new ErrorType(); err.ErrorCode = XmlUtility.GetString(i.Current, "Code"); err.LongMessage = XmlUtility.GetString(i.Current, "LongMessage"); err.ShortMessage =XmlUtility.GetString(i.Current, "ShortMessage"); if (XmlUtility.GetString(i.Current, "Severity") == "Warning") err.SeverityCode = SeverityCodeType.Warning; else err.SeverityCode = SeverityCodeType.Error; ex.Errors.Add(err); } return ex; }
/// <summary> /// Method to upload one picture to eBay picture server, use this method /// if you want to operate on UploadSiteHostedPicture request/response directly /// </summary> /// <param name="request">UploadSiteHostedPicturesRequestType</param> /// <param name="fileName">full path of the picture file</param> /// <returns>UploadSiteHostedPicturesResponseType</returns> public UploadSiteHostedPicturesResponseType UpLoadSiteHostedPicture(UploadSiteHostedPicturesRequestType request, string fileName) { UploadSiteHostedPicturesResponseType respObj = null; try { XmlDocument reqDoc = serializeToXmlDoc(request); fixEncoding(reqDoc); addApiCredentials(reqDoc); updateElementName(reqDoc, "UploadSiteHostedPicturesRequestType", "UploadSiteHostedPicturesRequest"); string reqStr = reqDoc.OuterXml; string respStr = sendFile(fileName, reqStr); XmlDocument respDoc = new XmlDocument(); respDoc.LoadXml(respStr); updateElementName(respDoc, "UploadSiteHostedPicturesResponse", "UploadSiteHostedPicturesResponseType"); respStr = respDoc.OuterXml; respObj = (UploadSiteHostedPicturesResponseType)deserializeFromXml(respStr, typeof(UploadSiteHostedPicturesResponseType)); if (respObj != null && respObj.Errors != null && respObj.Errors.Count > 0) { throw new ApiException(new ErrorTypeCollection(respObj.Errors)); } return respObj; } catch(Exception ex) { ApiException mApiExcetpion = null; if (ex is ApiException) { mApiExcetpion = (ApiException)ex; } else { mApiExcetpion = new ApiException(ex.Message, ex); } MessageSeverity svr = mApiExcetpion.SeverityErrorCount > 0 ? MessageSeverity.Error : MessageSeverity.Warning; LogMessage("EPS Exception : " + mApiExcetpion.Message, MessageType.Exception, svr); if (mApiExcetpion.SeverityErrorCount > 0) throw mApiExcetpion; } return respObj; }
/// <summary> /// /// </summary> internal void SendRequest() { try { if (AbstractRequest == null) throw new ApiException("RequestType reference not set to an instance of an object.", new System.ArgumentNullException()); if (ApiContext == null) throw new ApiException("ApiContext reference not set to an instance of an object.", new System.ArgumentNullException()); if (ApiContext.ApiCredential == null) throw new ApiException("Credentials reference in ApiContext object not set to an instance of an object.", new System.ArgumentNullException()); string apiName = AbstractRequest.GetType().Name.Replace("RequestType",""); if (this.ApiContext.EnableMetrics) { mCallMetrics = this.ApiContext.CallMetricsTable.GetNewEntry(apiName); mCallMetrics.ApiCallStarted = System.DateTime.Now; } CustomSecurityHeaderType secHdr = this.GetSecurityHeader(); // Get default constructor. /* ConstructorInfo svcCCTor = this.mServiceType.GetConstructor( BindingFlags.Instance | BindingFlags.Public, null, CallingConventions.HasThis, null, null); */ ConstructorInfo svcCCTor = this.mServiceType.GetConstructor(new Type[] {}); object svcInst = svcCCTor.Invoke(null); PropertyInfo pi; pi = this.mServiceType.GetProperty("ApiLogManager"); if( pi == null ) throw new SdkException("ApiLogManager was not found in InterfaceServiceType"); pi.SetValue(svcInst, this.mApiContext.ApiLogManager, null); pi = this.mServiceType.GetProperty("EnableComression"); if( pi == null ) throw new SdkException("EnableComression was not found in InterfaceServiceType"); pi.SetValue(svcInst, this.mEnableCompression, null); pi = this.mServiceType.GetProperty("RequesterCredentials"); if( pi == null ) throw new SdkException("RequesterCredentials was not found in InterfaceServiceType"); pi.SetValue(svcInst, secHdr, null); pi = this.mServiceType.GetProperty("WebProxy"); if (pi == null) throw new SdkException("WebProxy was not found in InterfaceServiceType"); pi.SetValue(svcInst, this.mApiContext.WebProxy, null); if (this.mApiContext.WebProxy != null) { LogMessage("Proxy Server is Set", MessageType.Information, MessageSeverity.Informational); } pi = this.mServiceType.GetProperty("CallMetricsEntry"); if( pi == null ) throw new SdkException("CallMetricsEntry was not found in InterfaceServiceType"); if (this.ApiContext.EnableMetrics) pi.SetValue(svcInst, this.mCallMetrics, null); else pi.SetValue(svcInst, null, null); string url = ""; try { if (ApiContext.SoapApiServerUrl != null && ApiContext.SoapApiServerUrl.Length > 0) url = String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}?callname={1}&siteid={2}&client=netsoap", ApiContext.SoapApiServerUrl, apiName, SiteUtility.GetSiteID(Site).ToString(System.Globalization.CultureInfo.InvariantCulture)); else { url = (string)this.mServiceType.GetProperty("Url").GetValue(svcInst, null); url = String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}?callname={1}&siteid={2}&client=netsoap", url, apiName, SiteUtility.GetSiteID(Site).ToString(System.Globalization.CultureInfo.InvariantCulture)); } //svcCCTor.Url = url; this.mServiceType.GetProperty("Url").SetValue(svcInst, url, null); } catch(Exception ex) { throw new ApiException(ex.Message, ex); } LogMessage(url, MessageType.Information, MessageSeverity.Informational); //svcCCTor.Timeout = Timeout; this.mServiceType.GetProperty("Timeout").SetValue(svcInst, Timeout, null); AbstractRequest.Version = Version; if (!mDetailLevelOverride && AbstractRequest.DetailLevel == null) { AbstractRequest.DetailLevel = new DetailLevelCodeTypeCollection(); AbstractRequest.DetailLevel.AddRange(mDetailLevelList); } //Added OutputSelector to base call JIRA-SDK-561 AbstractRequest.OutputSelector = new StringCollection(); AbstractRequest.OutputSelector.AddRange(mOutputSelector); if (ApiContext.ErrorLanguage != ErrorLanguageCodeType.CustomCode) AbstractRequest.ErrorLanguage = ApiContext.ErrorLanguage.ToString(); //Populate the message AbstractRequest.MessageID = System.Guid.NewGuid().ToString(); Type methodtype = svcInst.GetType(); object[] reqparm = new object[] {AbstractRequest}; int retries = 0; int maxRetries = 0; bool doretry = false; CallRetry retry = null; if (mCallRetry != null) { retry = mCallRetry; maxRetries = retry.MaximumRetries; } else if (ApiContext.CallRetry != null) { retry = ApiContext.CallRetry; maxRetries = retry.MaximumRetries; } do { Exception callException = null; try { mResponse = null; mApiException = null; if (retries > 0) { LogMessage("Invoking Call Retry", MessageType.Information, MessageSeverity.Informational); System.Threading.Thread.Sleep(retry.DelayTime); } if( BeforeRequest != null ) BeforeRequest(this, new BeforeRequestEventArgs(AbstractRequest)); //Invoke the Service DateTime start = DateTime.Now; mResponse = (AbstractResponseType) methodtype.GetMethod(apiName).Invoke(svcInst, reqparm); mResponseTime = DateTime.Now - start; if( AfterRequest != null ) AfterRequest(this, new AfterRequestEventArgs(mResponse)); // Catch Token Expiration warning if (mResponse != null && secHdr.HardExpirationWarning != null) { ApiContext.ApiCredential.TokenHardExpirationWarning( System.Convert.ToDateTime(secHdr.HardExpirationWarning, System.Globalization.CultureInfo.CurrentUICulture)); } if (mResponse != null && mResponse.Errors != null && mResponse.Errors.Count > 0) { throw new ApiException(new ErrorTypeCollection(mResponse.Errors)); } } catch (Exception ex) { // this catches soap faults if (ex.GetType() == typeof(TargetInvocationException)) { // we never care about the outer exception Exception iex = ex.InnerException; // Parse Soap Faults if (iex.GetType() == typeof(SoapException)) { ex = ApiException.FromSoapException((SoapException) iex); } else if (iex.GetType() == typeof(InvalidOperationException)) { // Go to innermost exception while (iex.InnerException != null) iex = iex.InnerException; ex = new ApiException(iex.Message, iex); } else if (iex.GetType() == typeof(HttpException)) { HttpException httpEx = (HttpException) iex; String str = "HTTP Error Code: " + httpEx.StatusCode; ex = new ApiException(str, iex); } else { ex = new ApiException(iex.Message, iex); } } callException = ex; // log the message - override current switches - *if* (a) we wouldn't normally log it, and (b) // the exception matches the exception filter. if (retry != null) doretry = retry.ShouldRetry(ex); if (!doretry || retries == maxRetries) { throw ex; } else { string soapReq = (string)this.mServiceType.GetProperty("SoapRequest").GetValue(svcInst, null); string soapResp = (string)this.mServiceType.GetProperty("SoapResponse").GetValue(svcInst, null); LogMessagePayload(soapReq + "\r\n\r\n" + soapResp, MessageSeverity.Informational, ex); MessageSeverity svr = ((ApiException) ex).SeverityErrorCount > 0 ? MessageSeverity.Error : MessageSeverity.Warning; LogMessage(ex.Message, MessageType.Exception, svr); } } finally { string soapReq = (string)this.mServiceType.GetProperty("SoapRequest").GetValue(svcInst, null); string soapResp = (string)this.mServiceType.GetProperty("SoapResponse").GetValue(svcInst, null); if (!doretry || retries == maxRetries) LogMessagePayload(soapReq + "\r\n\r\n" + soapResp, MessageSeverity.Informational, callException); if (mResponse != null && mResponse.TimestampSpecified) ApiContext.CallUpdate(mResponse.Timestamp); else ApiContext.CallUpdate(new DateTime(0)); mSoapRequest = soapReq; mSoapResponse = soapResp; retries++; } } while (doretry && retries <= maxRetries); } catch (Exception ex) { ApiException aex = ex as ApiException; if (aex != null) { mApiException = aex; } else { mApiException = new ApiException(ex.Message, ex); } MessageSeverity svr = mApiException.SeverityErrorCount > 0 ? MessageSeverity.Error : MessageSeverity.Warning; LogMessage(mApiException.Message, MessageType.Exception, svr); if (mApiException.SeverityErrorCount > 0) throw mApiException; } finally { if (this.ApiContext.EnableMetrics) mCallMetrics.ApiCallEnded = DateTime.Now; } }
/// <summary> /// /// </summary> internal void SendRequest() { try { if (AbstractRequest == null) { throw new ApiException("RequestType reference not set to an instance of an object.", new System.ArgumentNullException()); } if (ApiContext == null) { throw new ApiException("ApiContext reference not set to an instance of an object.", new System.ArgumentNullException()); } if (ApiContext.ApiCredential == null) { throw new ApiException("Credentials reference in ApiContext object not set to an instance of an object.", new System.ArgumentNullException()); } string apiName = AbstractRequest.GetType().Name.Replace("RequestType", ""); if (this.ApiContext.EnableMetrics) { mCallMetrics = this.ApiContext.CallMetricsTable.GetNewEntry(apiName); mCallMetrics.ApiCallStarted = System.DateTime.Now; } CustomSecurityHeaderType secHdr = this.GetSecurityHeader(); // Get default constructor. /* * ConstructorInfo svcCCTor = this.mServiceType.GetConstructor( * BindingFlags.Instance | BindingFlags.Public, null, * CallingConventions.HasThis, null, null); */ ConstructorInfo svcCCTor = this.mServiceType.GetConstructor(new Type[] { }); object svcInst = svcCCTor.Invoke(null); PropertyInfo pi; pi = this.mServiceType.GetProperty("ApiLogManager"); if (pi == null) { throw new SdkException("ApiLogManager was not found in InterfaceServiceType"); } pi.SetValue(svcInst, this.mApiContext.ApiLogManager, null); pi = this.mServiceType.GetProperty("EnableComression"); if (pi == null) { throw new SdkException("EnableComression was not found in InterfaceServiceType"); } pi.SetValue(svcInst, this.mEnableCompression, null); //Add oAuth //if (pi == null) // throw new SdkException("RequesterCredentials was not found in InterfaceServiceType"); //pi.SetValue(svcInst, this., null); if (string.IsNullOrEmpty(this.ApiContext.ApiCredential.oAuthToken)) { pi = this.mServiceType.GetProperty("RequesterCredentials"); if (pi == null) { throw new SdkException("RequesterCredentials was not found in InterfaceServiceType"); } pi.SetValue(svcInst, secHdr, null); } pi = this.mServiceType.GetProperty("WebProxy"); if (pi == null) { throw new SdkException("WebProxy was not found in InterfaceServiceType"); } pi.SetValue(svcInst, this.mApiContext.WebProxy, null); if (this.mApiContext.WebProxy != null) { LogMessage("Proxy Server is Set", MessageType.Information, MessageSeverity.Informational); } pi = this.mServiceType.GetProperty("CallMetricsEntry"); if (pi == null) { throw new SdkException("CallMetricsEntry was not found in InterfaceServiceType"); } if (this.ApiContext.EnableMetrics) { pi.SetValue(svcInst, this.mCallMetrics, null); } else { pi.SetValue(svcInst, null, null); } pi = this.mServiceType.GetProperty("OAuthToken"); if (!string.IsNullOrEmpty(this.ApiContext.ApiCredential.oAuthToken)) { this.mOAuth = this.ApiContext.ApiCredential.oAuthToken; pi.SetValue(svcInst, this.OAuth, null); } string url = ""; try { if (ApiContext.SoapApiServerUrl != null && ApiContext.SoapApiServerUrl.Length > 0) { url = String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}?callname={1}&siteid={2}&client=netsoap", ApiContext.SoapApiServerUrl, apiName, SiteUtility.GetSiteID(Site).ToString(System.Globalization.CultureInfo.InvariantCulture)); } else { url = (string)this.mServiceType.GetProperty("Url").GetValue(svcInst, null); url = String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}?callname={1}&siteid={2}&client=netsoap", url, apiName, SiteUtility.GetSiteID(Site).ToString(System.Globalization.CultureInfo.InvariantCulture)); } //svcCCTor.Url = url; this.mServiceType.GetProperty("Url").SetValue(svcInst, url, null); } catch (Exception ex) { throw new ApiException(ex.Message, ex); } LogMessage(url, MessageType.Information, MessageSeverity.Informational); //svcCCTor.Timeout = Timeout; this.mServiceType.GetProperty("Timeout").SetValue(svcInst, Timeout, null); AbstractRequest.Version = Version; if (!mDetailLevelOverride && AbstractRequest.DetailLevel == null) { AbstractRequest.DetailLevel = new DetailLevelCodeTypeCollection(); AbstractRequest.DetailLevel.AddRange(mDetailLevelList); } //Added OutputSelector to base call JIRA-SDK-561 AbstractRequest.OutputSelector = new StringCollection(); AbstractRequest.OutputSelector.AddRange(mOutputSelector); if (ApiContext.ErrorLanguage != ErrorLanguageCodeType.CustomCode) { AbstractRequest.ErrorLanguage = ApiContext.ErrorLanguage.ToString(); } //Populate the message AbstractRequest.MessageID = System.Guid.NewGuid().ToString(); Type methodtype = svcInst.GetType(); object[] reqparm = new object[] { AbstractRequest }; int retries = 0; int maxRetries = 0; bool doretry = false; CallRetry retry = null; if (mCallRetry != null) { retry = mCallRetry; maxRetries = retry.MaximumRetries; } else if (ApiContext.CallRetry != null) { retry = ApiContext.CallRetry; maxRetries = retry.MaximumRetries; } do { Exception callException = null; try { mResponse = null; mApiException = null; if (retries > 0) { LogMessage("Invoking Call Retry", MessageType.Information, MessageSeverity.Informational); System.Threading.Thread.Sleep(retry.DelayTime); } if (BeforeRequest != null) { BeforeRequest(this, new BeforeRequestEventArgs(AbstractRequest)); } //Invoke the Service DateTime start = DateTime.Now; mResponse = (AbstractResponseType)methodtype.GetMethod(apiName).Invoke(svcInst, reqparm); mResponseTime = DateTime.Now - start; if (AfterRequest != null) { AfterRequest(this, new AfterRequestEventArgs(mResponse)); } // Catch Token Expiration warning if (mResponse != null && secHdr.HardExpirationWarning != null) { ApiContext.ApiCredential.TokenHardExpirationWarning( System.Convert.ToDateTime(secHdr.HardExpirationWarning, System.Globalization.CultureInfo.CurrentUICulture)); } if (mResponse != null && mResponse.Errors != null && mResponse.Errors.Count > 0) { throw new ApiException(new ErrorTypeCollection(mResponse.Errors)); } } catch (Exception ex) { // this catches soap faults if (ex.GetType() == typeof(TargetInvocationException)) { // we never care about the outer exception Exception iex = ex.InnerException; // Parse Soap Faults if (iex.GetType() == typeof(SoapException)) { ex = ApiException.FromSoapException((SoapException)iex); } else if (iex.GetType() == typeof(InvalidOperationException)) { // Go to innermost exception while (iex.InnerException != null) { iex = iex.InnerException; } ex = new ApiException(iex.Message, iex); } else if (iex.GetType() == typeof(HttpException)) { HttpException httpEx = (HttpException)iex; String str = "HTTP Error Code: " + httpEx.StatusCode; ex = new ApiException(str, iex); } else { ex = new ApiException(iex.Message, iex); } } callException = ex; // log the message - override current switches - *if* (a) we wouldn't normally log it, and (b) // the exception matches the exception filter. if (retry != null) { doretry = retry.ShouldRetry(ex); } if (!doretry || retries == maxRetries) { throw ex; } else { string soapReq = (string)this.mServiceType.GetProperty("SoapRequest").GetValue(svcInst, null); string soapResp = (string)this.mServiceType.GetProperty("SoapResponse").GetValue(svcInst, null); LogMessagePayload(soapReq + "\r\n\r\n" + soapResp, MessageSeverity.Informational, ex); MessageSeverity svr = ((ApiException)ex).SeverityErrorCount > 0 ? MessageSeverity.Error : MessageSeverity.Warning; LogMessage(ex.Message, MessageType.Exception, svr); } } finally { string soapReq = (string)this.mServiceType.GetProperty("SoapRequest").GetValue(svcInst, null); string soapResp = (string)this.mServiceType.GetProperty("SoapResponse").GetValue(svcInst, null); if (!doretry || retries == maxRetries) { LogMessagePayload(soapReq + "\r\n\r\n" + soapResp, MessageSeverity.Informational, callException); } if (mResponse != null && mResponse.TimestampSpecified) { ApiContext.CallUpdate(mResponse.Timestamp); } else { ApiContext.CallUpdate(new DateTime(0)); } mSoapRequest = soapReq; mSoapResponse = soapResp; retries++; } } while (doretry && retries <= maxRetries); } catch (Exception ex) { ApiException aex = ex as ApiException; if (aex != null) { mApiException = aex; } else { mApiException = new ApiException(ex.Message, ex); } MessageSeverity svr = mApiException.SeverityErrorCount > 0 ? MessageSeverity.Error : MessageSeverity.Warning; LogMessage(mApiException.Message, MessageType.Exception, svr); if (mApiException.SeverityErrorCount > 0) { throw mApiException; } } finally { if (this.ApiContext.EnableMetrics) { mCallMetrics.ApiCallEnded = DateTime.Now; } } }