-
-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathProductService.cs
156 lines (139 loc) · 5.69 KB
/
ProductService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System.Net.Http;
using ShopifySharp.Filters;
using System.Threading.Tasks;
using ShopifySharp.Infrastructure;
using ShopifySharp.Lists;
namespace ShopifySharp
{
/// <summary>
/// A service for manipulating Shopify products.
/// </summary>
public class ProductService : ShopifyService
{
/// <summary>
/// Creates a new instance of <see cref="ProductService" />.
/// </summary>
/// <param name="myShopifyUrl">The shop's *.myshopify.com URL.</param>
/// <param name="shopAccessToken">An API access token for the shop.</param>
public ProductService(string myShopifyUrl, string shopAccessToken) : base(myShopifyUrl, shopAccessToken) { }
/// <summary>
/// Gets a count of all of the shop's products.
/// </summary>
/// <returns>The count of all products for the shop.</returns>
public virtual async Task<int> CountAsync(ProductCountFilter filter = null)
{
return await ExecuteGetAsync<int>("products/count.json", "count", filter);
}
/// <summary>
/// Gets a list of up to 250 of the shop's products.
/// </summary>
public virtual async Task<ListResult<Product>> ListAsync(ListFilter<Product> filter)
{
return await ExecuteGetListAsync("products.json", "products", filter);
}
/// <summary>
/// Gets a list of up to 250 of the shop's products.
/// </summary>
public virtual async Task<ListResult<Product>> ListAsync(ProductListFilter filter = null)
{
return await ListAsync(filter?.AsListFilter());
}
/// <summary>
/// Retrieves the <see cref="Product"/> with the given id.
/// </summary>
/// <param name="productId">The id of the product to retrieve.</param>
/// <param name="fields">A comma-separated list of fields to return.</param>
/// <returns>The <see cref="Product"/>.</returns>
public virtual async Task<Product> GetAsync(long productId, string fields = null)
{
return await ExecuteGetAsync<Product>($"products/{productId}.json", "product", fields);
}
/// <summary>
/// Creates a new <see cref="Product"/> on the store.
/// </summary>
/// <param name="product">A new <see cref="Product"/>. Id should be set to null.</param>
/// <param name="options">Options for creating the product.</param>
/// <returns>The new <see cref="Product"/>.</returns>
public virtual async Task<Product> CreateAsync(Product product, ProductCreateOptions options = null)
{
var req = PrepareRequest("products.json");
var body = product.ToDictionary();
if (options != null)
{
foreach (var option in options.ToDictionary())
{
body.Add(option);
}
}
var content = new JsonContent(new
{
product = body
});
var response = await ExecuteRequestAsync<Product>(req, HttpMethod.Post, content, "product");
return response.Result;
}
/// <summary>
/// Updates the given <see cref="Product"/>.
/// </summary>
/// <param name="productId">Id of the object being updated.</param>
/// <param name="product">The <see cref="Product"/> to update.</param>
/// <returns>The updated <see cref="Product"/>.</returns>
public virtual async Task<Product> UpdateAsync(long productId, Product product)
{
var req = PrepareRequest($"products/{productId}.json");
var content = new JsonContent(new
{
product = product
});
var response = await ExecuteRequestAsync<Product>(req, HttpMethod.Put, content, "product");
return response.Result;
}
/// <summary>
/// Deletes a product with the given Id.
/// </summary>
/// <param name="productId">The product object's Id.</param>
public virtual async Task DeleteAsync(long productId)
{
var req = PrepareRequest($"products/{productId}.json");
await ExecuteRequestAsync(req, HttpMethod.Delete);
}
/// <summary>
/// Publishes an unpublished <see cref="Product"/>.
/// </summary>
/// <param name="id">The product's id.</param>
/// <returns>The published <see cref="Product"/></returns>
public virtual async Task<Product> PublishAsync(long id)
{
var req = PrepareRequest($"products/{id}.json");
var content = new JsonContent(new
{
product = new
{
id = id,
published = true
}
});
var response = await ExecuteRequestAsync<Product>(req, HttpMethod.Put, content, "product");
return response.Result;
}
/// <summary>
/// Unpublishes an published <see cref="Product"/>.
/// </summary>
/// <param name="id">The product's id.</param>
/// <returns>The unpublished <see cref="Product"/></returns>
public virtual async Task<Product> UnpublishAsync(long id)
{
var req = PrepareRequest($"products/{id}.json");
var content = new JsonContent(new
{
product = new
{
id = id,
published = false
}
});
var response = await ExecuteRequestAsync<Product>(req, HttpMethod.Put, content, "product");
return response.Result;
}
}
}