public XeroHttpClient(string baseUri, IAuthenticator auth, IConsumer consumer, IUser user, IJsonObjectMapper jsonMapper, IXmlObjectMapper xmlMapper, IRateLimiter rateLimiter) : this(jsonMapper, xmlMapper) { Client = new HttpClient(baseUri, auth, consumer, user, rateLimiter); EventHandler <ApiCallEventArgs> apiCalledEventHander = (sender, e) => ApiCalled?.Invoke(this, e); Client.ApiCalled += apiCalledEventHander; auth.ApiCalled += apiCalledEventHander; }
public IToken GetToken(string baseUri, IToken consumer, string endPoint, string header) { var req = new HttpClient(baseUri) { UserAgent = "Xero Api wrapper - " + consumer.ConsumerKey }; EventHandler <ApiCallEventArgs> apiCalledHandler = (sender, e) => ApiCalled?.Invoke(this, e); req.ApiCalled += apiCalledHandler; req.AddHeader("Authorization", header); var response = req.Post(endPoint, string.Empty); req.ApiCalled -= apiCalledHandler; if (response.StatusCode != HttpStatusCode.OK) { if (response.Body.Contains("oauth_problem")) { throw new OAuthException(response.Body); } throw new UnexpectedOauthResponseException(response.StatusCode, response.Body); } var qs = HttpUtility.ParseQueryString(response.Body); var expires = qs["oauth_expires_in"]; var session = qs["oauth_session_handle"]; var token = new Token(consumer.ConsumerKey, consumer.ConsumerSecret) { TokenKey = qs["oauth_token"], TokenSecret = qs["oauth_token_secret"], OrganisationId = qs["xero_org_muid"] }; if (!string.IsNullOrWhiteSpace(expires)) { token.ExpiresAt = DateTime.UtcNow.AddSeconds(int.Parse(expires)); } if (!string.IsNullOrWhiteSpace(session)) { token.Session = session; token.SessionExpiresAt = DateTime.UtcNow.AddSeconds(int.Parse(qs["oauth_authorization_expires_in"])); } return(token); }
private Response MakeCall(string endpoint, Func <HttpWebRequest> createHttpWebRequest) { var method = "Unknown"; string responseCode; var stopwatch = Stopwatch.StartNew(); try { var request = createHttpWebRequest(); method = request.Method; Logger.LogDebug("Sending data to Xero. {method}, {requestUri}", method, request.RequestUri); Response response; using (var webResponse = (HttpWebResponse)request.GetResponse()) { responseCode = webResponse.StatusCode.ToString(); stopwatch.Stop(); ApiCalled?.Invoke(this, new ApiCallEventArgs(endpoint, method, stopwatch.ElapsedMilliseconds, responseCode, null)); response = new Response(webResponse); } return(response); } catch (WebException we) { Logger.LogWarning(we, "WebException occured during calling Xero API."); if (we.Response != null) { var response = new Response((HttpWebResponse)we.Response); we.Response.Close(); if (response.StatusCode == HttpStatusCode.ServiceUnavailable && response.Body.Contains("oauth_problem")) { responseCode = "RateExceeded"; } else { responseCode = response.StatusCode.ToString(); } stopwatch.Stop(); ApiCalled?.Invoke(this, new ApiCallEventArgs(endpoint, method, stopwatch.ElapsedMilliseconds, responseCode, response.Body)); return(response); } throw; } finally { stopwatch.Stop(); } }
protected XeroApi(string baseUri, IAuthenticator auth, IConsumer consumer, IUser user, IJsonObjectMapper readMapper, IXmlObjectMapper writeMapper, IRateLimiter rateLimiter) : this(baseUri) { Client = new XeroHttpClient(baseUri, auth, consumer, user, readMapper, writeMapper, rateLimiter); Client.ApiCalled += (sender, e) => ApiCalled?.Invoke(this, e); }