-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
191 lines (159 loc) · 5.88 KB
/
Program.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Collections.Generic;
namespace MyPrintNode
{
class Program
{
static void Main(string[] args)
{
try
{
new Program().run();
}
catch (Exception e)
{
Console.Error.WriteLine("error connecting to server");
Console.Error.WriteLine(e.Message + "\n\n");
Console.Error.WriteLine(e.StackTrace);
Console.ReadKey();
}
}
private void run()
{
// instance of api client
PrintNodeService service = new PrintNodeService(
"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
public class PrintNodeService
{
private readonly string address;
private readonly string username;
// TODO send base64 image
// TODO remove this
private readonly Dictionary<string, string> Endpoints = new Dictionary<string, string>()
{
{"Computers", "computers"},
{"Printers", "printers"},
{"PrintJobs", "printjobs"}
};
public PrintNodeService(string address, string username)
{
this.address = address;
this.username = username;
}
public string GetComputers()
{
return Get("Computers");
}
public string GetPrinters()
{
return Get("Printers");
}
public string GetPrintJobs()
{
return Get("PrintJobs");
}
/**
* @return string id of printjob
*/
public string SubmitPrintJob()
{
string source = "PrintNodeApi/3.0";
string title = "YukonTestPdf";
string printerId = "1";
var sb = new StringBuilder();
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("}");
string response = Post("PrintJobs", sb.ToString());
// strips off quotes around the id
if (response != null)
{
return response.Substring(1, response.Length - 2);
}
else
{
return response;
}
}
private Uri BuildUri(string dir)
{
if (!Endpoints.ContainsKey(dir))
{
throw new Exception("no endpoint defined for " + dir + ". consider adding the endpoint to the Endpoints dictionary defined in PrintNodeService class");
}
var url = address + '/' + Endpoints[dir];
return new Uri(url);
}
private HttpWebRequest BuildRequest(string dir, String method)
{
var uri = BuildUri(dir);
ASCIIEncoding encoding = new ASCIIEncoding();
var request = (HttpWebRequest)WebRequest.Create(uri);
// headers
request.Method = method;
request.ContentType = "application/json";
// basic authentication
string credentials = String.Format("{0}:", username);
byte[] bytes = Encoding.ASCII.GetBytes(credentials);
string base64 = Convert.ToBase64String(bytes);
string authorization = String.Concat("Basic ", base64);
request.Headers.Add("Authorization", authorization);
return request;
}
private string getResponseBody(HttpWebRequest request)
{
// gets response
HttpWebResponse response = null;
response = (HttpWebResponse)request.GetResponse();
return new StreamReader(response.GetResponseStream()).ReadToEnd();
}
private string Get(string dir)
{
var request = BuildRequest(dir, "GET");
return getResponseBody(request);
}
private string Post(string dir, string postData)
{
var request = BuildRequest(dir, "POST");
// data
var data = Encoding.UTF8.GetBytes(postData);
request.ContentLength = data.Length;
// writes data
var requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
return getResponseBody(request);
}
}
}