Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathProgram.cs
More file actions
Latest commit
191 lines (159 loc) · 5.88 KB
/
Copy pathProgram.cs
File metadata and controls
191 lines (159 loc) · 5.88 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Globalization;
usingSystem.IO;
usingSystem.Net;
usingSystem.Security.Cryptography;
usingSystem.Text.RegularExpressions;
usingSystem.Collections.Generic;
namespaceMyPrintNode
{
classProgram
{
staticvoidMain(string[]args)
{
try
{
newProgram().run();
}
catch(Exceptione)
{
Console.Error.WriteLine("error connecting to server");
Console.Error.WriteLine(e.Message+"\n\n");
Console.Error.WriteLine(e.StackTrace);
Console.ReadKey();
}
}
privatevoidrun()
{
// instance of api client
PrintNodeServiceservice=newPrintNodeService(
"https://api.printnode.com/",
"<api key>");
// submits a print job and prints out the printjob ID
Console.WriteLine("Print Job ID is "+service.SubmitPrintJob());
// gets a list of all submitted print jobs
Console.WriteLine("Below is list of Print Jobs in json:");
Console.WriteLine(service.GetPrintJobs());
// gets a list of all computers registered with PrintNode account
Console.WriteLine("\n\n\n\n Below is list of Computers: ");
Console.WriteLine(service.GetComputers());
// gets a list of all printers registered with PrintNode account
Console.WriteLine("\n\n\n\n Below is list of Printers: ");
Console.WriteLine(service.GetPrinters());
// haults program from exiting
Console.ReadKey();
}
}
// PrintNodeService.cs
publicclassPrintNodeService
{
privatereadonlystringaddress;
privatereadonlystringusername;
// TODO send base64 image
// TODO remove this
privatereadonlyDictionary<string,string>Endpoints=newDictionary<string,string>()
{
{"Computers","computers"},
{"Printers","printers"},
{"PrintJobs","printjobs"}
};
publicPrintNodeService(stringaddress,stringusername)
{
this.address=address;
this.username=username;
}
publicstringGetComputers()
{
returnGet("Computers");
}
publicstringGetPrinters()
{
returnGet("Printers");
}
publicstringGetPrintJobs()
{
returnGet("PrintJobs");
}
/**
* @return string id of printjob
*/
publicstringSubmitPrintJob()
{
stringsource="PrintNodeApi/3.0";
stringtitle="YukonTestPdf";
stringprinterId="1";
varsb=newStringBuilder();
sb.Append("{");
sb.Append("\"printer\": "+printerId+",");
sb.Append("\"title\": \""+title+"\",");
sb.Append("\"contentType\": \"pdf_uri\",");
sb.Append("\"title\": \""+title+"\",");
sb.Append("\"content\": \""+"http://www.education.gov.yk.ca/pdf/pdf-test.pdf"+"\",");
sb.Append("\"source\": \"PrintNodeApi/1.0\"");
sb.Append("}");
stringresponse=Post("PrintJobs",sb.ToString());
// strips off quotes around the id
if(response!=null)
{
returnresponse.Substring(1,response.Length-2);
}
else
{
returnresponse;
}
}
privateUriBuildUri(stringdir)
{
if(!Endpoints.ContainsKey(dir))
{
thrownewException("no endpoint defined for "+dir+". consider adding the endpoint to the Endpoints dictionary defined in PrintNodeService class");
}
varurl=address+'/'+Endpoints[dir];
returnnewUri(url);
}
privateHttpWebRequestBuildRequest(stringdir,Stringmethod)
{
varuri=BuildUri(dir);
ASCIIEncodingencoding=newASCIIEncoding();
varrequest=(HttpWebRequest)WebRequest.Create(uri);
// headers
request.Method=method;
request.ContentType="application/json";
// basic authentication
stringcredentials=String.Format("{0}:",username);
byte[]bytes=Encoding.ASCII.GetBytes(credentials);
stringbase64=Convert.ToBase64String(bytes);
stringauthorization=String.Concat("Basic ",base64);
request.Headers.Add("Authorization",authorization);
returnrequest;
}
privatestringgetResponseBody(HttpWebRequestrequest)
{
// gets response
HttpWebResponseresponse=null;
response=(HttpWebResponse)request.GetResponse();
returnnewStreamReader(response.GetResponseStream()).ReadToEnd();
}
privatestringGet(stringdir)
{
varrequest=BuildRequest(dir,"GET");
returngetResponseBody(request);
}
privatestringPost(stringdir,stringpostData)
{
varrequest=BuildRequest(dir,"POST");
// data
vardata=Encoding.UTF8.GetBytes(postData);
request.ContentLength=data.Length;
// writes data
varrequestStream=request.GetRequestStream();
requestStream.Write(data,0,data.Length);
requestStream.Close();
returngetResponseBody(request);
}
}
}