-
Notifications
You must be signed in to change notification settings - Fork 0
/
eTowerPost.cs
126 lines (109 loc) · 4.32 KB
/
eTowerPost.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
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;
public class eTowerPost
{
public eTower.eTowerOrderRespone PostJsonETowerOrder(string smethod, object s, string path, string accept)
{
string wRespStatusCode = string.Empty;
string responseText = string.Empty;
bool bwaserror = false;
string WALLTECH_SERVER = "http://qa-us.etowertech.com" + path;
string ACCESS_TOKEN = "yourtoken";
string SECRET_KEY = "yourkey";
eTower.eTowerOrderRespone[] n = new eTower.eTowerOrderRespone[1];
var oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string sJSON = oSerializer.Serialize(s);
var http = (HttpWebRequest)WebRequest.Create(new Uri(WALLTECH_SERVER));
http.Accept = accept;
http.ContentType = "application/json";
http.Method = smethod;
string walltech_date = DateTime.UtcNow.ToString("ddd, dd MMM yyyy HH:mm:ss") + " GMT";
StringBuilder auth = new StringBuilder();
auth.Append(smethod);
auth.Append("\n");
auth.Append(walltech_date);
auth.Append("\n");
auth.Append("http://qa-us.etowertech.com" + path);
string hash = EncodeHMAC(auth.ToString(), Encoding.UTF8.GetBytes(SECRET_KEY));
http.Headers.Add("X-WallTech-Date", walltech_date);
http.Headers.Add("Authorization", "WallTech " + ACCESS_TOKEN + ':' + hash);
http.KeepAlive = false;
var encoding = new ASCIIEncoding();
Byte[] bytes = encoding.GetBytes(sJSON);
Stream newStream = http.GetRequestStream();
newStream.Write(bytes, 0, bytes.Length);
newStream.Close();
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)http.GetResponse();
}
catch (WebException we)
{
wRespStatusCode = ((HttpWebResponse)we.Response).StatusCode.ToString();
bwaserror = true;
using (var reader = new System.IO.StreamReader(we.Response.GetResponseStream(), encoding))
{
responseText = reader.ReadToEnd();
var sData = JsonConvert.DeserializeObject<List<eTower.eTowerOrderRespone>>(responseText);
n[0] = (eTower.eTowerOrderRespone)sData[0];
}
}
if (bwaserror == false)
{
if (wRespStatusCode == "")
{
wRespStatusCode = response.StatusCode.ToString();
}
using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
{
string data = reader.ReadToEnd();
var sData = JsonConvert.DeserializeObject<List<eTower.eTowerOrderRespone>>(data);
n[0] = (eTower.eTowerOrderRespone)sData[0];
}
}
try
{
response.Close();
}
catch { }
try
{
newStream.Dispose();
}
catch { }
try
{
response.Dispose();
}
catch { }
try
{
bytes = null;
}
catch { }
return n[0];
}
private string EncodeHMAC(string input, byte[] key)
{
byte[] hashedValue;
using (HMACSHA1 hmac = new HMACSHA1(key))
{
byte[] stringBytes = Encoding.UTF8.GetBytes(input);
hashedValue = hmac.ComputeHash(stringBytes);
}
return Convert.ToBase64String(hashedValue);
}
public List<eTower.eTowerLabelRequest> GetEtowerLabelByExternalRef(long external_ref)
{
eTower.eTowerLabelRequest x = new eTower.eTowerLabelRequest();
x.id = external_ref.ToString();
return new List<eTower.eTowerLabelRequest>() { x };
}
}