-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpecToolClient.cs
153 lines (117 loc) · 4.77 KB
/
SpecToolClient.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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace MolioDocEF6
{
public class SpecToolClient
{
static HttpClient http = new HttpClient();
public static async Task<SpecToolClient> Login(Uri baseUrl, string username, string password)
{
using (var request = new HttpRequestMessage())
{
var content = new StringContent(JsonConvert.SerializeObject(new { username, password }), Encoding.UTF8, "application/json");
var response = await http.PostAsync(UrlTo(baseUrl, "api/spectool/user/login"), content);
var authCookie = GetAspxAuthCookie(response);
if (authCookie == null)
throw new Exception("Invalid credentials");
return new SpecToolClient(baseUrl, authCookie);
}
}
static HttpCookie GetAspxAuthCookie(HttpResponseMessage response)
{
if (response.Headers.TryGetValues("set-cookie", out var cookies))
return cookies
.Select(c => HttpCookie.TryParse(c, out var cookie) ? cookie : null)
.Where(c => c != null & c.Name == ".ASPXAUTH")
.FirstOrDefault();
return null;
}
public async Task<IEnumerable<SpecToolWorkArea>> GetWorkAreas()
{
using (var request = AuthenticatedRequestMessage("api/spectool/workarea/getworkareas"))
{
var response = await http.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<IEnumerable<SpecToolWorkArea>>(content);
}
}
public async Task<IEnumerable<SpecToolDocument>> GetDocuments(string id)
{
using (var request = AuthenticatedRequestMessage("api/spectool/workarea/getdocuments", "id=" + id))
{
var response = await http.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<IEnumerable<SpecToolDocument>>(content);
}
}
public async Task<SpecToolDocument> GetDocument(Guid id)
{
using (var request = AuthenticatedRequestMessage("api/spectool/workarea/getdocument", "id=" + id))
{
var response = await http.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<SpecToolDocument>(content);
}
}
SpecToolClient(Uri baseUrl, HttpCookie authCookie)
{
this.baseUrl = baseUrl;
this.authCookie = authCookie;
}
HttpRequestMessage AuthenticatedRequestMessage(string path, string queryString = "")
{
var request = new HttpRequestMessage();
request.RequestUri = UrlTo(baseUrl, path, queryString);
request.Headers.Add("Cookie", $"{authCookie.Name}={authCookie.Value}");
return request;
}
static Uri UrlTo(Uri baseUrl, string path, string queryString = "") =>
new UriBuilder(baseUrl.Scheme, baseUrl.Host, baseUrl.Port, path, "?" + queryString).Uri;
Uri baseUrl;
HttpCookie authCookie;
}
public class SpecToolWorkArea
{
public string Id { get; set; }
public string Name { get; set; }
public string Access { get; set; }
}
public class SpecToolDocument
{
public Guid Id { get; set; }
public string Name { get; set; }
public string WorkAreaId { get; set; }
public string WorkAreaName { get; set; }
public string Type { get; set; }
public IEnumerable<SpecToolSection> Sections { get; set; }
public string DocumentType { get; set; }
public SpecToolDocument Document { get; set; }
}
public class SpecToolSection
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Number { get; set; }
public IEnumerable<SpecToolContentGroup> Groups { get; set; }
public IEnumerable<SpecToolSection> Sections { get; set; }
}
public class SpecToolContentGroup
{
public string Name { get; set; }
public IEnumerable<SpecToolContentItem> Contents { get; set; }
}
public class SpecToolContentItem
{
public string Id { get; set; }
public bool IsMasterText { get; set; }
public string MasterTextId { get; set; }
public string Text { get; set; }
public string MasterText { get; set; }
}
}