本文整理汇总了C#中System.Net.Http.HttpClient.Configure方法的典型用法代码示例。如果您正苦于以下问题:C# HttpClient.Configure方法的具体用法?C# HttpClient.Configure怎么用?C# HttpClient.Configure使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.HttpClient
的用法示例。
在下文中一共展示了HttpClient.Configure方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ShopInfo
public async Task<ShopInfo> ShopInfo(string shopUrl, string accessToken)
{
shopUrl.PerCallShopUrlContract();
accessToken.PerCallAccessTokenContract();
using (var httpClient = new HttpClient())
{
httpClient.Configure(shopUrl, accessToken);
var response = await httpClient.GetAsync(ApiRequestResources.GetShopAccountConfiguration);
if (!response.IsSuccessStatusCode)
{
return null;
}
var rawResponseContent = await response.Content.ReadAsStringAsync();
var shopInfo = JsonConvert.DeserializeObject<ShopInfo>(rawResponseContent);
return shopInfo;
}
}
示例2: GetAllAsync
public async Task<IList<Webhook>> GetAllAsync(
string shopUrl,
string accessToken,
string address = "",
DateTime createdBefore = default(DateTime),
DateTime createdAfter = default(DateTime),
WebhookField fields = WebhookField.None,
int limit = 50,
int page = 1,
int idGreaterThan = 0,
WebhookTopic topic = WebhookTopic.None,
DateTime updatedBefore = default(DateTime),
DateTime updatedAfter = default(DateTime))
{
//// Default contracts
shopUrl.PerCallShopUrlContract();
accessToken.PerCallAccessTokenContract();
//// Optional parameter validation
if (!string.IsNullOrWhiteSpace(address) && !address.IsValidUrlAddress())
{
throw new ArgumentException("Address parameter is not a well formed URL");
}
if (250 < limit)
{
throw new ArgumentException("Limit value cannot be more than 250, default is 50 if not specified");
}
if (0 == page)
{
throw new ArgumentException("Page value cannot be zero");
}
//// Build query string
var queryStringBuilder = new QueryStringBuilder();
if (!string.IsNullOrWhiteSpace(address))
{
queryStringBuilder.Add("address", address);
}
if (default(DateTime) != createdBefore)
{
queryStringBuilder.Add("created_at_max", createdBefore.ToString("yyyy-MM-dd HH:mm"));
}
if (default(DateTime) != createdAfter)
{
queryStringBuilder.Add("created_at_min", createdAfter.ToString("yyyy-MM-dd HH:mm"));
}
if (WebhookField.None != fields)
{
queryStringBuilder.Add("fields", fields.BuildWebhookFieldFilter());
}
if (0 != limit)
{
queryStringBuilder.Add("limit", limit.ToString(CultureInfo.InvariantCulture));
}
if (0 != page)
{
queryStringBuilder.Add("page", page.ToString(CultureInfo.InvariantCulture));
}
if (0 != idGreaterThan)
{
queryStringBuilder.Add("since_id", idGreaterThan.ToString(CultureInfo.InvariantCulture));
}
if (WebhookTopic.None != topic)
{
queryStringBuilder.Add("topic", topic.Convert());
}
if (default(DateTime) != updatedBefore)
{
queryStringBuilder.Add("updated_at_min", updatedBefore.ToString("yyyy-MM-dd HH:mm"));
}
if (default(DateTime) != updatedAfter)
{
queryStringBuilder.Add("updated_at_max", updatedAfter.ToString("yyyy-MM-dd HH:mm"));
}
//// Perform HTTP GET call to API
using (var httpClient = new HttpClient())
{
httpClient.Configure(shopUrl, accessToken);
var queryStringParameters = queryStringBuilder.ToString();
var response = await httpClient.GetAsync(string.Format(CultureInfo.InvariantCulture, "{0}{1}", ApiRequestResources.GetWebhooksAll, string.IsNullOrWhiteSpace(queryStringParameters) ? string.Empty : queryStringParameters));
if (!response.IsSuccessStatusCode)
{
return null;
}
var rawResponseContent = await response.Content.ReadAsStringAsync();
var webhooks = JsonConvert.DeserializeObject<WebhooksJson>(rawResponseContent);
return webhooks.Hooks;
//.........这里部分代码省略.........
示例3: DeleteAsync
public async Task<bool> DeleteAsync(string shopUrl, string accessToken, string id)
{
shopUrl.PerCallShopUrlContract();
accessToken.PerCallAccessTokenContract();
using (var httpClient = new HttpClient())
{
httpClient.Configure(shopUrl, accessToken);
var response = await httpClient.DeleteAsync(string.Format(CultureInfo.InvariantCulture, ApiRequestResources.DeleteWebhook, id));
return response.IsSuccessStatusCode;
}
}
示例4: UpdateAsync
public async Task<Webhook> UpdateAsync(string shopUrl, string accessToken, string id, string address)
{
shopUrl.PerCallShopUrlContract();
accessToken.PerCallAccessTokenContract();
using (var httpClient = new HttpClient())
{
httpClient.Configure(shopUrl, accessToken);
var webhookCreate = new WebhookUpdateWrapJson
{
Webhook = new WebhookUpdateJson
{
Id = id,
Address = address
}
};
var content = new StringContent(JsonConvert.SerializeObject(webhookCreate), Encoding.UTF8, "application/json");
var response = await httpClient.PutAsync(string.Format(CultureInfo.InvariantCulture, ApiRequestResources.PutWebhookUpdate, id), content);
if (!response.IsSuccessStatusCode)
{
return null;
}
var rawResponseContent = await response.Content.ReadAsStringAsync();
var webhookJson = JsonConvert.DeserializeObject<WebhookJson>(rawResponseContent);
return webhookJson.Webhook;
}
}
示例5: CreateAsync
public async Task<Webhook> CreateAsync(string shopUrl, string accessToken, string address, WebhookTopic topic, WebhookFormat format = WebhookFormat.Json)
{
shopUrl.PerCallShopUrlContract();
accessToken.PerCallAccessTokenContract();
using (var httpClient = new HttpClient())
{
httpClient.Configure(shopUrl, accessToken);
var webhookCreate = new WebhookCreateWrapJson
{
Webhook = new WebhookCreateJson
{
Topic = topic.Convert(),
Address = address,
Format = format.ToString().ToLowerInvariant()
}
};
var content = new StringContent(JsonConvert.SerializeObject(webhookCreate), Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(ApiRequestResources.PostWebhookCreate, content);
if (!response.IsSuccessStatusCode)
{
return null;
}
var rawResponseContent = await response.Content.ReadAsStringAsync();
var webhookJson = JsonConvert.DeserializeObject<WebhookJson>(rawResponseContent);
return webhookJson.Webhook;
}
}
示例6: CountAsync
public async Task<int> CountAsync(string shopUrl, string accessToken, string address = "", WebhookTopic topic = WebhookTopic.None)
{
shopUrl.PerCallShopUrlContract();
accessToken.PerCallAccessTokenContract();
if (!string.IsNullOrWhiteSpace(address) && !address.IsValidUrlAddress())
{
throw new ArgumentException("Address parameter is not a well formed URL");
}
var queryStringBuilder = new QueryStringBuilder();
if (!string.IsNullOrWhiteSpace(address))
{
queryStringBuilder.Add("address", address);
}
if (WebhookTopic.None != topic)
{
queryStringBuilder.Add("topic", topic.Convert());
}
using (var httpClient = new HttpClient())
{
httpClient.Configure(shopUrl, accessToken);
var queryStringParameters = queryStringBuilder.ToString();
var response = await httpClient.GetAsync(string.Format(CultureInfo.InvariantCulture, "{0}{1}", ApiRequestResources.GetWebhooksAllCount, string.IsNullOrWhiteSpace(queryStringParameters) ? string.Empty : queryStringParameters));
if (!response.IsSuccessStatusCode)
{
return 0;
}
var rawResponseContent = await response.Content.ReadAsStringAsync();
var webhooksCount = JsonConvert.DeserializeObject<WebhooksCountJson>(rawResponseContent);
return webhooksCount.Count;
}
}
示例7: GetSingleAsync
public async Task<Webhook> GetSingleAsync(string shopUrl, string accessToken, string id, WebhookField fields = WebhookField.None)
{
//// Default contracts
shopUrl.PerCallShopUrlContract();
accessToken.PerCallAccessTokenContract();
//// Build query string
var queryStringBuilder = new QueryStringBuilder();
if (WebhookField.None != fields)
{
queryStringBuilder.Add("fields", fields.BuildWebhookFieldFilter());
}
//// Perform HTTP GET call to API
using (var httpClient = new HttpClient())
{
httpClient.Configure(shopUrl, accessToken);
var queryStringParameters = queryStringBuilder.ToString();
var response = await httpClient.GetAsync(string.Format(CultureInfo.InvariantCulture, "{0}{1}", string.Format(CultureInfo.InvariantCulture, ApiRequestResources.GetWebhookSingle, id), string.IsNullOrWhiteSpace(queryStringParameters) ? string.Empty : queryStringParameters));
if (!response.IsSuccessStatusCode)
{
return null;
}
var rawResponseContent = await response.Content.ReadAsStringAsync();
var webhookJson = JsonConvert.DeserializeObject<WebhookJson>(rawResponseContent);
return webhookJson.Webhook;
}
}
示例8: CreateAsync
public async Task<ScriptTag> CreateAsync(string shopUrl, string accessToken, string source)
{
shopUrl.PerCallShopUrlContract();
accessToken.PerCallAccessTokenContract();
using (var httpClient = new HttpClient())
{
httpClient.Configure(shopUrl, accessToken);
var scriptTagCreate = new ScriptTagCreateWrapJson
{
ScriptTag = new ScriptTagCreateJson
{
Event = "onload",
Source = source
}
};
var content = new StringContent(JsonConvert.SerializeObject(scriptTagCreate), Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(ApiRequestResources.PostScriptTagCreate, content);
if (!response.IsSuccessStatusCode)
{
return null;
}
var rawResponseContent = await response.Content.ReadAsStringAsync();
var scriptTagJson = JsonConvert.DeserializeObject<ScriptTagJson>(rawResponseContent);
return scriptTagJson.ScriptTag;
}
}
示例9: CountAsync
public async Task<int> CountAsync(string shopUrl, string accessToken, string source = "")
{
shopUrl.PerCallShopUrlContract();
accessToken.PerCallAccessTokenContract();
if (!string.IsNullOrWhiteSpace(source) && !source.IsValidUrlAddress())
{
throw new ArgumentException("Source parameter is not a well formed URL");
}
var queryStringBuilder = new QueryStringBuilder();
if (!string.IsNullOrWhiteSpace(source))
{
queryStringBuilder.Add("src", source);
}
using (var httpClient = new HttpClient())
{
httpClient.Configure(shopUrl, accessToken);
var queryStringParameters = queryStringBuilder.ToString();
var response = await httpClient.GetAsync(string.Format(CultureInfo.InvariantCulture, "{0}{1}", ApiRequestResources.GetScriptTagsAllCount, string.IsNullOrWhiteSpace(queryStringParameters) ? string.Empty : queryStringParameters));
if (!response.IsSuccessStatusCode)
{
return 0;
}
var rawResponseContent = await response.Content.ReadAsStringAsync();
var scriptTagsCount = JsonConvert.DeserializeObject<ScriptTagsCountJson>(rawResponseContent);
return scriptTagsCount.Count;
}
}
示例10: GetSingleAsync
public async Task<ScriptTag> GetSingleAsync(string shopUrl, string accessToken, string id, ScriptTagField fields = ScriptTagField.None)
{
shopUrl.PerCallShopUrlContract();
accessToken.PerCallAccessTokenContract();
var queryStringBuilder = new QueryStringBuilder();
if (ScriptTagField.None != fields)
{
queryStringBuilder.Add("fields", fields.BuildScriptTagFieldFilter());
}
using (var httpClient = new HttpClient())
{
httpClient.Configure(shopUrl, accessToken);
var queryStringParameters = queryStringBuilder.ToString();
var response = await httpClient.GetAsync(string.Format(CultureInfo.InvariantCulture, "{0}{1}", string.Format(CultureInfo.InvariantCulture, ApiRequestResources.GetScriptTagSingle, id), string.IsNullOrWhiteSpace(queryStringParameters) ? string.Empty : queryStringParameters));
if (!response.IsSuccessStatusCode)
{
return null;
}
var rawResponseContent = await response.Content.ReadAsStringAsync();
var scriptTagJson = JsonConvert.DeserializeObject<ScriptTagJson>(rawResponseContent);
return scriptTagJson.ScriptTag;
}
}