My favorites | Sign in
Project Logo
                
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
192
193
194
195
196
197
198
199
200
201
202
203
/*
* tasklist-cmd
* 2008-01-29 (mca)
* 2008-02-01 (mca) : cleaned up UI, added arg display
*
* sample command-line app that uses HTTPClient to execute against server
* uses the http://exyus.com/xcs/tasklist endpoint as a target.
* list, add, update, delete tasks via commandline
*
* usage:
* tasklist-cmd cmd [arg1] [arg2]
* -list : returns list of tasks at the public site
* -add "my task" : creates new task in pending state
* -add "my task" 1 : creates new task in completed state
* -toggle x1234abcd : toggles the state of the task (using id)
* -delete x1234abcd : deletes existing task (using id)
* -clear : removes all tasks from the list
*/

using System;
using System.Collections.Generic;
using System.Text;

using Exyus.Web;
using System.Xml;

namespace tasklist_cmd
{
class Program
{
static void Main(string[] args)
{
TaskList tl = new TaskList("http://exyus.com/xcs/tasklist/");

Console.WriteLine("\nTaskList Utility\n2008-02-02 (mca)\n" + tl.Uri + "\n");

if (args.Length == 0)
{
ShowHelp();
return;
}

Console.WriteLine("Request:");
ShowCommand(args);
Console.WriteLine("Response:");

try
{
switch (args[0].ToLower())
{
case "-list":
break;
case "-add":
if (args.Length > 2)
tl.AddItem(args[1], args[2]);
else
tl.AddItem(args[1]);
break;
case "-toggle":
tl.ToggleItem(args[1]);
break;
case "-delete":
tl.DeleteItem(args[1]);
break;
case "-clear":
tl.DeleteAll();
break;
default:
throw new IndexOutOfRangeException("Unknown command [" + args[0] + "]");
}

// show current list
Console.WriteLine(tl.ShowList());
}
catch (Exception ex)
{
Console.WriteLine("ERROR: "+ex.Message);
ShowHelp();
}

return;
}

static void ShowCommand(string[] args)
{
for (int i = 0; i < args.Length; i++)
{
Console.Write(args[i] + " ");
}
Console.WriteLine("\n");
}

static void ShowHelp()
{
Console.WriteLine("\nvalid commands:\n-list\n-add [name]\n-toggle [id]\n-delete [id]\n-clear");
}
}

public class TaskList
{
string p_etag = string.Empty;
string p_ua = "tasklist-cmd/1.0";
string p_done = "<is-completed>1</is-completed>";
string p_pending = "<is-completed>0</is-completed>";
string p_new_task = "<task><name>{0}</name><is-completed>{1}</is-completed></task>";
HTTPClient client = new HTTPClient();

public string Uri = "http://exyus.com/xcs/tasklist/";

public TaskList() { }
public TaskList(string uri)
{
this.Uri = uri;
client.UserAgent = p_ua;
}

public XmlDocument GetList()
{
client.RequestHeaders.Set("cache-control", "no-cache");
string results = client.Execute(Uri, "get", "text/xml");
XmlDocument doc = new XmlDocument();
doc.LoadXml(results);
return doc;
}

public XmlDocument GetItem(string id)
{
string results = client.Execute(Uri + id, "get", "text/xml");
p_etag = client.ResponseHeaders["etag"];
XmlDocument doc = new XmlDocument();
doc.LoadXml(results);
return doc;
}

public void AddItem(string name)
{
AddItem(name, "0");
}

public void AddItem(string name, string completed)
{
client.Execute(Uri, "post", "text/xml", string.Format(p_new_task, name, completed));
}

public void ToggleItem(string id)
{
XmlDocument doc = GetItem(id);
string results = doc.OuterXml;
XmlNode completed = doc.SelectSingleNode("//is-completed");

if (completed.InnerText == "0")
results = results.Replace(p_pending, p_done);
else
results = results.Replace(p_done, p_pending);

client.RequestHeaders.Set("if-match", p_etag);
client.Execute(Uri + id, "put", "text/xml", results);
}

public void DeleteItem(string id)
{
client.Execute(Uri + id, "delete", "text/xml");
}

public void DeleteAll()
{
XmlDocument doc = GetList();
XmlNodeList tasks = doc.SelectNodes("//task");
for (int i = 0; i < tasks.Count; i++)
{
try
{
DeleteItem(tasks[i].Attributes["href"].Value);
}
catch (Exception ex)
{
Console.WriteLine("ERROR: "+ex.Message);
}
}
}

public string ShowList()
{
XmlDocument doc = GetList();
XmlNodeList tasks = doc.SelectNodes("//task");
StringBuilder sb = new StringBuilder();

if (tasks.Count == 0)
return "list is empty.";

for (int i = 0; i < tasks.Count; i++)
{
sb.AppendFormat(
"{0} {1}({2})\n",
tasks[i].Attributes["href"].Value,
tasks[i].SelectSingleNode("name").InnerText,
tasks[i].SelectSingleNode("is-completed").InnerText
);
}
return sb.ToString();
}
}
}
Show details Hide details

Change log

r120 by m...@amundsen.com on Feb 02, 2008   Diff
Program.cs
  *  cleaned up UI for article
Go to: 
Project members, sign in to write a code review

Older revisions

r112 by m...@amundsen.com on Jan 29, 2008   Diff
added inline documents
cleaned up AssemblyInfo.cs comments
r111 by m...@amundsen.com on Jan 29, 2008   Diff
Program.cs
  * removed sample call w/ credentials
r110 by m...@amundsen.com on Jan 29, 2008   Diff
Program.cs
  * added constructor for TaskList
  * added calls to set custom
UserAgent for HTTPClient
  * changed .Add to .Set for
...
All revisions of this file

File info

Size: 6204 bytes, 203 lines

File properties

svn:mime-type
text/plain
svn:eol-style
native
svn:keywords
Id Rev Date Author
Hosted by Google Code