-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSession.cpp
87 lines (62 loc) · 2.05 KB
/
Session.cpp
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
#include "Response.h"
#include "Session.h"
#include "HttpClient.h"
#include "Routes.h"
#include "make_unique.h"
#include "JsonValue.h"
using namespace Routes;
Window Session::focusedWindow()
{
auto response = HttpClient::get(makeUrl(CONTEXT_WINDOW_HANDLE));
return Window(response->sessionId(), std::make_shared<Session>(*this));
}
Windows Session::windows()
{
auto response = HttpClient::get(makeUrl(CONTEXT_WINDOW_HANDLES));
Windows windows;
for (auto& win : response->value().asArray()) {
Window w(win.asStr(), std::make_shared<Session>(*this));
windows.push_back(std::move(w));
}
return windows;
}
// static non-member
std::string makeStaticUrl(const std::string& host, const std::string& path)
{
std::string serverUrl = host + path;
if (host.substr(0, 7) != "http://") {
serverUrl = "http://" + serverUrl;
}
return serverUrl;
}
// static
Session Session::create(const std::string &host, const JsonObject& desiredCapabilities)
{
std::string serverUrl = makeStaticUrl(host, SESSION);
auto ans = HttpClient::post(serverUrl, {{"desiredCapabilities", desiredCapabilities}});
return Session(ans->sessionId(), serverUrl, desiredCapabilities);
}
// static
Status Session::status(const std::string &host)
{
std::string serverUrl = makeStaticUrl(host, STATUS);
auto ans = HttpClient::get(serverUrl);
if ( ans->status() != HttpClient::getStatus("Success")) {
throw;
}
return { ans->value()["build"].asObject(), ans->value()["os"].asObject() };
}
// static
Sessions Session::sessions(const std::string &host)
{
std::string serverUrl = makeStaticUrl(host, SESSIONS);
auto ans = HttpClient::get(serverUrl);
auto sessionArray = ans->value().asArray();
Sessions sessions;
// each session obj == {id:string, capabilities:obj}
for (auto& s : sessionArray) {
Capabilities caps = s["capabilities"].asObject();
sessions.push_back(Session(s["sessionId"].asStr(), host, caps));
}
return sessions;
}