-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathShopifyObject.cs
137 lines (119 loc) · 4.88 KB
/
ShopifyObject.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Dynamic;
using System.Web.Script.Serialization;
namespace Shopify {
public class ShopifyObject:DynamicObject {
string _objectType;
string _apiKey;
string _password;
string _shopUrl;
/// <summary>
/// This is a special, psuedo-wrapper for Shopify objects such as Products, Customers, and so on
/// </summary>
public ShopifyObject(string objectType, string storeUrl, string apiKey, string password) {
_objectType = objectType;
_shopUrl = storeUrl;
_apiKey = apiKey;
_password = password;
}
/// <summary>
/// A Dynamic catcher - allows you to invoke Save, Delete, Add, etc on this object
/// </summary>
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) {
//the args should be expandos
//loop them and spin up some JSON
var sb = new StringBuilder();
var name = _objectType;
if (name.EndsWith("s"))
name = name.TrimEnd('s');
//what are we doing?
if (binder.Name == "Save") {
var item = args[0];
//var dc = (IDictionary<string, object>)item;
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(item);
//wrap this as we need an outer identifier
json = "{ " + name + ": " + json + "}";
bool isNew = !item.GetType().GetProperties().Any(x => x.Name == "id");
//adjust the root to be the name here
//var outer = new Dictionary<string, object>();
//outer.Add(name, item);
if (isNew) {
Post(json);
Console.WriteLine("{0} added...", name);
} else {
//pull the id
var id = item.GetType().GetProperty("id").GetValue(item, null).ToString();
Put(id, json);
Console.WriteLine("{0} updated...", name);
}
} else if (binder.Name == "Delete" || binder.Name == "Destroy") {
Delete(args[0].ToString());
Console.WriteLine("Blog {0} deleted ...", args[0].ToString());
} else {
throw new InvalidDataException("Can't tell what it is you want to do - try using Save or Delete instead");
}
result = this;
return true;
}
/// <summary>
/// Executes a PUT to Shopify - used for Updates
/// </summary>
dynamic Put(string id, string json) {
//build the URL
var url = _shopUrl + this._objectType + "/" + id + ".json";
var result = ExecuteRequest(url, "PUT", json);
return JsonHelper.Decode(result);
}
/// <summary>
/// Executes an HTTP DELETE to Shopify... guess what it does!
/// </summary>
void Delete(string id) {
var url = _shopUrl + this._objectType + "/" + id + ".json";
ExecuteRequest(url, "DELETE", "");
}
/// <summary>
/// Executes an HTTP POST - which adds an item to the Shopify DB
/// </summary>
dynamic Post(string json) {
//build the URL
var url = _shopUrl + this._objectType + ".json";
var result = ExecuteRequest(url, "POST", json);
//the result will be a pile of JSON
//deserialize it and return
return JsonHelper.Decode(result);
}
/// <summary>
/// The core executor for sending off requests
/// </summary>
string ExecuteRequest(string url, string verb, string data) {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = verb;
request.ContentType = "application/json";
var creds = new NetworkCredential(userName: _apiKey, password: _password);
request.Credentials = creds;
//add the data if needed
if (!String.IsNullOrEmpty(data)) {
using (var ms = new MemoryStream()) {
using (var writer = new StreamWriter(request.GetRequestStream())) {
writer.Write(data);
writer.Close();
}
}
}
var response = (HttpWebResponse)request.GetResponse();
string result = "";
using (Stream stream = response.GetResponseStream()) {
StreamReader sr = new StreamReader(stream);
result = sr.ReadToEnd();
sr.Close();
}
return result;
}
}
}