/// <summary> /// Executes a request and returns the raw result string. Throws an exception when the response is invalid. /// </summary> public static async Task <string> ExecuteRequestToStringAsync(BizwebRequestMessage requestMsg, IRequestExecutionPolicy execPolicy) { if (requestMsg.Content != null) { //necessary to buffer content for multiple reads await requestMsg.Content.LoadIntoBufferAsync(); } return(await execPolicy.Run(requestMsg, async (reqMsg) => { //Need to create a RequestInfo before send RequestMessage //because after that, HttpClient will dispose RequestMessage var requestInfo = await CreateRequestSimpleInfoAsync(reqMsg); using (var response = await HttpUtils.SendHttpRequestAsync(reqMsg)) { //Check for and throw exception when necessary. await CheckResponseExceptionsAsync(response, requestInfo); var rawResponse = await response.Content.ReadAsStringAsync(); return new RequestResult <string>(response, rawResponse); } })); }
/// <summary> /// Executes a request and returns the JToken result. Throws an exception when the response is invalid. /// </summary> public static async Task <JToken> ExecuteRequestAsync(BizwebRequestMessage requestMsg, IRequestExecutionPolicy execPolicy) { if (requestMsg.Content != null) { //necessary to buffer content for multiple reads await requestMsg.Content.LoadIntoBufferAsync(); } return(await execPolicy.Run(requestMsg, async (reqMsg) => { //Need to create a RequestInfo before send RequestMessage //because after that, HttpClient will dispose RequestMessage var requestInfo = await CreateRequestSimpleInfoAsync(reqMsg); using (var response = await HttpUtils.SendHttpRequestAsync(reqMsg)) { //Check for and throw exception when necessary. await CheckResponseExceptionsAsync(response, requestInfo); // When using JToken make sure that dates are not stripped of any timezone information // if tokens are de-serialised into strings/DateTime/DateTimeZoneOffset using (var reader = new JsonTextReader(new StreamReader(await response.Content.ReadAsStreamAsync())) { DateParseHandling = DateParseHandling.None }) { //Notice: deserialize can fails when response body null or empty var result = Deserialize(reader, reqMsg.RootElement); return new RequestResult <JToken>(response, result); } } })); }
/// <summary> /// Creates a new instance of <see cref="ShopifyService" />. /// </summary> /// <param name="myShopifyUrl">The shop's *.myshopify.com URL.</param> /// <param name="shopAccessToken">An API access token for the shop.</param> protected ShopifyService(string myShopifyUrl, string shopAccessToken) { _ShopUri = BuildShopUri(myShopifyUrl, false); _AccessToken = shopAccessToken; _Client = _HttpClientFactory.CreateClient(); _ExecutionPolicy = _GlobalExecutionPolicy; }
/// <summary> /// Creates a new instance of <see cref="ShopifyService" />. /// </summary> /// <param name="myShopifyUrl">The shop's *.myshopify.com URL.</param> /// <param name="shopAccessToken">An API access token for the shop.</param> protected ShopifyService(string myShopifyUrl, string shopAccessToken) { _ShopUri = BuildShopUri(myShopifyUrl, false); _AccessToken = shopAccessToken; // If there's a global execution policy it should be set as this instance's policy. // User can override it with instance-specific execution policy. _ExecutionPolicy = _GlobalExecutionPolicy ?? new DefaultRequestExecutionPolicy(); }
/// <summary> /// Creates a new instance of <see cref="WebflowService" />. /// </summary> /// <param name="shopAccessToken">access token</param> protected WebflowService(string shopAccessToken) { _accessToken = shopAccessToken; // If there's a global execution policy it should be set as this instance's policy. // User can override it with instance-specific execution policy. _ExecutionPolicy = _GlobalExecutionPolicy ?? new DefaultRequestExecutionPolicy(); }
/// <summary> /// Creates a new instance of <see cref="QuickbutikService" />. /// </summary> /// <param name="apiKey">App Secret Api Key</param> protected QuickbutikService(string apiKey) { _apiKey = apiKey; // If there's a global execution policy it should be set as this instance's policy. // User can override it with instance-specific execution policy. _ExecutionPolicy = _GlobalExecutionPolicy ?? new DefaultRequestExecutionPolicy(); }
/// <summary> /// Creates a new instance of <see cref="SquareSpaceService" />. /// </summary> /// <param name="secretApiKey">App Secret Api Key</param> protected SquareSpaceService(string secretApiKey) { _ShopUri = BuildSquareSpaceApiUri(); _secretApiKey = secretApiKey; // If there's a global execution policy it should be set as this instance's policy. // User can override it with instance-specific execution policy. _ExecutionPolicy = _GlobalExecutionPolicy ?? new DefaultRequestExecutionPolicy(); }
/// <summary> /// Creates a new instance of <see cref="MyCashFlowException" />. /// </summary> /// <param name="storeName">store name</param> /// <param name="userName">User name</param> /// <param name="apiKey">An API access token for the shop.</param> protected MyCashFlowService(string storeName, string userName, string apiKey) { _apiKey = apiKey; _userName = userName; _storeName = storeName; _ShopUri = BuildMyCashFlowApiUri(); // If there's a global execution policy it should be set as this instance's policy. // User can override it with instance-specific execution policy. _ExecutionPolicy = _GlobalExecutionPolicy ?? new DefaultRequestExecutionPolicy(); }
/// <summary> /// Sets the global execution policy for all *new* instances. Current instances are unaffected, but you can call .SetExecutionPolicy on them. /// </summary> public static void SetGlobalExecutionPolicy(IRequestExecutionPolicy globalExecutionPolicy) { _GlobalExecutionPolicy = globalExecutionPolicy; }
/// <summary> /// Sets the execution policy for this instance only. This policy will always be used over the global execution policy. /// The instance will revert back to the global execution policy if you pass null to this method. /// </summary> public void SetExecutionPolicy(IRequestExecutionPolicy executionPolicy) { // If the user passes null, revert to the global execution policy. _ExecutionPolicy = executionPolicy ?? _GlobalExecutionPolicy ?? new DefaultRequestExecutionPolicy(); }
/// <summary> /// Executes a request and returns the given type. Throws an exception when the response is invalid. /// Use this method when the expected response is a single line or simple object that doesn't warrant its own class. /// </summary> public static async Task <T> ExecuteRequestAsync <T>(BizwebRequestMessage requestMsg, IRequestExecutionPolicy execPolicy) where T : new() { return((await ExecuteRequestAsync(requestMsg, execPolicy)).ToObject <T>()); }
/// <summary> /// Sets the global execution policy for all *new* instances. Current instances are unaffected, but you can call .SetExecutionPolicy on them. /// The execution policy will revert back to the <see cref="DefaultRequestExecutionPolicy" /> if you pass null to this method. /// </summary> public static void SetGlobalExecutionPolicy(IRequestExecutionPolicy globalExecutionPolicy) { _GlobalExecutionPolicy = globalExecutionPolicy ?? new DefaultRequestExecutionPolicy(); }
public static void SetExecutionPolicy(IRequestExecutionPolicy executionPolicy) { _executionPolicy = executionPolicy; }