2014-10-28 118 views
0

我正在努力构建一个非常简单的Web应用程序,它可以从Shopify的API获取订单数据并以漂亮的格式显示它。通过JSON获取Shopify订单数据与asp.net c#

我是白天的前端开发人员,在当天我写了很多经典的asp,但asp.net和JSON对我来说都是新的。

我在过去的周末,网上淘的,可以给我一个快速教程,或者怎么做以下一些非常简单的示例代码中的任何文章:

  • 请对Shopify API的调用( http://docs.shopify.com/api/order#show)检索订单记录
  • 转换JSON响应以获取客户的名字和姓氏,并在订单上列出订单项。然后,很好地格式化它并在html中显示它。

我发现json.net和我读了一些关于httpclient在asp.net中。

有没有人有任何真正简单的示例代码,或任何教程链接,像我这样的初学者可以用来学习如何使用asp.net c#来拉取shopify数据并显示它?

谢谢!

回答

1

我最近与shopify进行了整合。我发现最好的方法是使用RestSharp。我相信可能会有围绕Shopify API的开源项目,但我发现他们在调用/响应方面做了很多可疑的事情。

我创建围绕RestSharp基本执行包装execute方法

public T Execute<T>(RestRequest request) where T : new() 
{ 
    var client = new RestClient(GetHost()); 
    if (AuthToken != null) 
     client.Authenticator = new ShopifyAuthenticator(AuthToken); 

    var result = client.Execute<T>(request); 

    if(result.StatusCode == System.Net.HttpStatusCode.Unauthorized) 
     throw new ShopifyUnauthorizedException(result.StatusDescription); 

    if (result.ErrorException != null) 
    { 
     const string message = "Error retrieving response. Check inner details for more information."; 
     throw new ShopifyException(message, result.ErrorException); 
    } 

    return result.Data; 
} 

public string GetHost() 
{ 
    return Uri.UriSchemeHttps + Uri.SchemeDelimiter + Store + _shopifyHost; 
} 

的authToken是的authToken为您的商店。 商店是shopify的子域。 你可以删除异常传播的东西,直到你有一个更好的理解。

还创建了一个基本的RestSharp OAuth2Authenticator。

class ShopifyAuthenticator : OAuth2Authenticator 
{ 
    public ShopifyAuthenticator(string accessToken) 
     : base(accessToken) 
    { 

    } 

    public override void Authenticate(IRestClient client, IRestRequest request) 
    { 
     // only add the Authorization parameter if it hasn't been added. 
     if (!request.Parameters.Any(p => p.Name.Equals("X-Shopify-Access-Token", StringComparison.OrdinalIgnoreCase))) 
     { 
      request.AddParameter("X-Shopify-Access-Token", AccessToken, ParameterType.HttpHeader); 
     } 
    } 
} 

然后,所有你必须调用的是包装方法。

RestRequest request = new RestRequest(_shopEndpoint); 
return _client.Execute<ShopResult>(request, parameters).Shop; 

类和常量:

const string _shopEndpoint = "/admin/shop.json"; 
const string _shopifyHost = ".myshopify.com"; 

class ShopResult 
{ 
    public Shop Shop { get; set; } 
} 

/// <summary> 
/// The Shopify API's shop object is a collection of the general settings and information about the shop. 
/// </summary> 
public class Shop 
{ 
    /// <summary> 
    /// The shop's street address. 
    /// </summary> 
    public string Address1 { get; set; } 

    /// <summary> 
    /// The city in which the shop is located. 
    /// </summary> 
    public string City { get; set; } 

    /// <summary> 
    /// The shop's country (by default equal to the two-letter country code). 
    /// </summary> 
    public string Country { get; set; } 

    /// <summary> 
    /// The two-letter country code corresponding to the shop's country. 
    /// </summary> 
    public string CountryCode { get; set; } 

    /// <summary> 
    /// The shop's normalized country name. 
    /// </summary> 
    public string CountryName { get; set; } 

    /// <summary> 
    /// The date and time when the shop was created. 
    /// </summary> 
    public DateTime CreatedAt { get; set; } 

    /// <summary> 
    /// The customer's email. 
    /// </summary> 
    public string CustomerEmail { get; set; } 

    /// <summary> 
    /// The three-letter code for the currency that the shop accepts. 
    /// </summary> 
    public string Currency { get; set; } 

    /// <summary> 
    /// The shop's domain. 
    /// </summary> 
    public string Domain { get; set; } 

    /// <summary> 
    /// The contact email address for the shop. 
    /// </summary> 
    public string Email { get; set; } 

    /// <summary> 
    /// Feature is present when a shop has a google app domain. It will be returned as a URL. If 
    /// the shop does not have this feature enabled it will default to "null." 
    /// </summary> 
    public string GoogleAppsDomain { get; set; } 

    /// <summary> 
    /// Feature is present if a shop has google apps enabled. Those shops with this feature 
    /// will be able to login to the google apps login. Shops without this feature enabled will default to "null." 
    /// </summary> 
    public string GoogleAppsLoginEnabled { get; set; } 

    /// <summary> 
    /// A unique numeric identifier for the shop. 
    /// </summary> 
    public int Id { get; set; } 

    /// <summary> 
    /// Geographic coordinate specifying the north/south location of a shop. 
    /// </summary> 
    public string Latitude { get; set; } 

    /// <summary> 
    /// Geographic coordinate specifying the east/west location of a shop. 
    /// </summary> 
    public string Logitude { get; set; } 

    /// <summary> 
    /// A string representing the way currency is formatted when the currency isn't specified. 
    /// </summary> 
    public string MoneyFormat { get; set; } 

    /// <summary> 
    /// A string representing the way currency is formatted when the currency is specified. 
    /// </summary> 
    public string MoneyWithCurrencyFormat { get; set; } 

    /// <summary> 
    /// The shop's 'myshopify.com' domain. 
    /// </summary> 
    public string MyshopifyDomain { get; set; } 

    /// <summary> 
    /// The name of the shop. 
    /// </summary> 
    public string Name { get; set; } 

    /// <summary> 
    /// The name of the Shopify plan the shop is on. 
    /// </summary> 
    public string PlanName { get; set; } 

    /// <summary> 
    /// The display name of the Shopify plan the shop is on. 
    /// </summary> 
    public string DisplayPlanName { get; set; } 

    /// <summary> 
    /// Indicates whether the Storefront password protection is enabled. 
    /// </summary> 
    public string PasswordEnabled { get; set; } 

    /// <summary> 
    /// The contact phone number for the shop. 
    /// </summary> 
    public string Phone { get; set; } 

    /// <summary> 
    /// The shop's normalized province or state name. 
    /// </summary> 
    public string Province { get; set; } 

    /// <summary> 
    /// The two-letter code for the shop's province or state. 
    /// </summary> 
    public string ProvinceCode { get; set; } 

    /// <summary> 
    /// The username of the shop owner. 
    /// </summary> 
    public string ShopOwner { get; set; } 

    /// <summary> 
    /// The setting for whether applicable taxes are included in product prices: Valid values are: "true" or "null." 
    /// </summary> 
    public string TaxShipping { get; set; } 

    /// <summary> 
    /// The setting for whether applicable taxes are included in product prices. Valid values are: "true" or "null." 
    /// </summary> 
    public string TaxesIncluded { get; set; } 

    /// <summary> 
    /// The setting for whether the shop is applying taxes on a per-county basis or not (US-only). Valid values are: "true" or "null." 
    /// </summary> 
    public string CountyTaxes { get; set; } 

    /// <summary> 
    /// The name of the timezone the shop is in. 
    /// </summary> 
    public string Timezone { get; set; } 

    /// <summary> 
    /// The zip or postal code of the shop's address. 
    /// </summary> 
    public string Zip { get; set; } 
}