2014-10-28 59 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; } 
}