This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateCloud.xaml.cs
106 lines (94 loc) · 4.12 KB
/
CreateCloud.xaml.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Cloudsdale.Managers;
using Cloudsdale.Models;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Cloudsdale {
public partial class CreateCloud {
public CreateCloud() {
InitializeComponent();
}
private void DoneClick(object sender, RoutedEventArgs e) {
CloudName.IsEnabled = false;
DoneBtn.IsEnabled = false;
PostData(CloudName.Text);
}
private void PostData(string name) {
var data = Encoding.UTF8.GetBytes(JObject.FromObject(new {
cloud = new { name }
}).ToString());
var request = WebRequest.CreateHttp("http://www.cloudsdale.org/v1/clouds");
request.Accept = "application/json";
request.ContentType = "application/json";
request.Headers["X-Auth-Token"] = Connection.CurrentCloudsdaleUser.auth_token;
request.Method = "POST";
request.BeginGetRequestStream(ar => {
using (var stream = request.EndGetRequestStream(ar)) {
stream.Write(data, 0, data.Length);
stream.Close();
}
request.BeginGetResponse(OnResponse, request);
}, null);
}
private void OnResponse(IAsyncResult asyncResult) {
try {
var request = (HttpWebRequest)asyncResult.AsyncState;
Cloud cloud;
using (var response = request.EndGetResponse(asyncResult))
using (var responseStream = response.GetResponseStream())
using (var responseReader = new StreamReader(responseStream)) {
cloud = JObject.Parse(responseReader.ReadToEnd())["result"].ToObject<Cloud>();
}
Dispatcher.BeginInvoke(() => {
if (cloud.is_transient ?? true) {
MessageBox.Show("Failure creating cloud (maybe one with the same name already exists?)");
CloudName.IsEnabled = true;
DoneBtn.IsEnabled = true;
return;
}
Connection.CurrentCloudsdaleUser.clouds = Connection.CurrentCloudsdaleUser.clouds.Concat(new[] { PonyvilleDirectory.RegisterCloud(cloud) }).ToArray();
Connection.CurrentCloud = PonyvilleDirectory.RegisterCloud(cloud);
NavigationService.Navigate(new Uri("/Clouds.xaml", UriKind.Relative));
});
} catch (WebException ex) {
ProcessError(ex);
}
}
private void ProcessError(WebException exception) {
if (exception.Response == null) {
return;
}
var response = (HttpWebResponse)exception.Response;
JObject data;
using (var responseStream = response.GetResponseStream())
using (var responseReader = new StreamReader(responseStream)) {
data = JObject.Parse(responseReader.ReadToEnd());
}
Dispatcher.BeginInvoke(() => {
if (data["flash"] != null) {
var message = data["flash"]["message"].ToString();
message = data["errors"].Aggregate(message, (current, error) => current +
("\n - " + error["ref_node"].ToString().UppercaseFirst() + " \"" +
data["result"][error["ref_node"].ToString()] + "\" " + error["message"]));
MessageBox.Show(message, data["flash"]["title"].ToString(),
MessageBoxButton.OK);
} else {
MessageBox.Show("An unknown error occurred trying to register.");
}
CloudName.IsEnabled = true;
DoneBtn.IsEnabled = true;
});
}
}
}