-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_client.h
More file actions
183 lines (166 loc) · 4.66 KB
/
http_client.h
File metadata and controls
183 lines (166 loc) · 4.66 KB
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
#include <CStringBuilder.h>
#include <WiFiClientSecure.h>
const char* clockify_host = "api.clockify.me";
int clockify_port = 443;
bool startTimer(int i) {
WiFiClientSecure client;
client.setInsecure();
if (!client.connect(clockify_host, clockify_port)) {
#ifdef DEBUG
Serial.println("Connection failed");
#endif
return false;
}
yield();
char buffer[280];
CStringBuilder sb(buffer, sizeof(buffer));
#ifdef DEBUG
Serial.print("ApiKey: ");
Serial.println(clockifyTimers[i].apiKey);
Serial.println("Creating body");
#endif
sb.print(F("{\"start\":\"")); // 10
sb.print(clockifyTimers[i].startTime); // 24
sb.print(F("\"")); // 1
sb.print(F(",\"description\":\"")); // 16
sb.print(clockifyTimers[i].description); // 120
sb.print(F("\",\"projectId\":\"")); // 15
sb.print(clockifyTimers[i].projectId); // 24
if (strlen(clockifyTimers[i].taskId) > 0) {
sb.print(F("\",\"taskId\":\"")); // 12
sb.print(clockifyTimers[i].taskId); // 24
}
sb.print(F("\"")); // 2
sb.print(F("}")); // 1
yield();
#ifdef DEBUG
Serial.println("Body:");
Serial.println(buffer);
Serial.println("Sending request");
#endif
client.print("POST /api/v1/workspaces/");
client.print(clockifyTimers[i].workspaceId);
client.println("/time-entries/ HTTP/1.1");
client.print("Host: ");
client.println(clockify_host);
client.print("X-Api-Key: ");
client.println(clockifyTimers[i].apiKey);
client.println("Content-Type: application/json");
client.println("User-Agent: ESP32");
client.println("Connection: close");
client.print("Content-Length: ");
client.println(sb.length());
client.println();
client.println(buffer);
yield();
updateTimerState(i, TIMER_START_SENT);
String line = client.readStringUntil('\n');
String returnCode = line.substring(9, 12);
yield();
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
#ifdef DEBUG
Serial.println("Headers received");
#endif
break;
}
}
yield();
if (returnCode != "201") {
#ifdef DEBUG
Serial.println("Failed to start timer:");
Serial.println(client.readStringUntil('\n'));
#endif
updateTimerState(i, TIMER_STARTING);
return false;
}
yield();
line = client.readStringUntil(',');
String timerId = line.substring(7, 31);
#ifdef DEBUG
Serial.println("Timer id: " + timerId);
#endif
strcpy(clockifyTimers[i].id, timerId.c_str());
client.stop();
yield();
updateTimerState(i, TIMER_RUNNING);
cancelProgress();
return true;
}
bool stopTimer(int i) {
WiFiClientSecure client;
client.setInsecure();
if (!client.connect(clockify_host, clockify_port)) {
#ifdef DEBUG
Serial.println("Connection failed");
#endif
return false;
}
yield();
char buffer[280];
CStringBuilder sb(buffer, sizeof(buffer));
#ifdef DEBUG
Serial.print("ApiKey: ");
Serial.println(clockifyTimers[i].apiKey);
Serial.println("Creating body");
#endif
sb.print(F("{\"start\":\"")); // 10
sb.print(clockifyTimers[i].startTime); // 24
sb.print(F("\",\"end\":\"")); // 9
sb.print(clockifyTimers[i].endTime); // 24
sb.print(F("\"")); // 1
sb.print(F(",\"description\":\"")); // 16
sb.print(clockifyTimers[i].description); // 120
sb.print(F("\",\"projectId\":\"")); // 15
sb.print(clockifyTimers[i].projectId); // 24
if (strlen(clockifyTimers[i].taskId) > 0) {
sb.print(F("\",\"taskId\":\"")); // 12
sb.print(clockifyTimers[i].taskId); // 24
}
sb.print(F("\"")); // 2
sb.print(F("}")); // 1
yield();
#ifdef DEBUG
Serial.println("Body:");
Serial.println(buffer);
Serial.println("Sending request");
#endif
yield();
client.print("PUT /api/v1/workspaces/");
client.print(clockifyTimers[i].workspaceId);
client.print("/time-entries/");
client.print(clockifyTimers[i].id);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(clockify_host);
client.print("X-Api-Key: ");
client.println(clockifyTimers[i].apiKey);
client.println("Content-Type: application/json");
client.println("User-Agent: ESP32");
client.println("Connection: close");
client.print("Content-Length: ");
client.println(sb.length());
client.println();
client.println(buffer);
yield();
updateTimerState(i, TIMER_STOP_SENT);
yield();
#ifdef DEBUG
Serial.println("Request sent");
#endif
String line = client.readStringUntil('\n');
client.stop();
String returnCode = line.substring(9, 12);
#ifdef DEBUG
Serial.println("Return code: " + returnCode);
#endif
yield();
if (returnCode != "200") {
updateTimerState(i, TIMER_STOPPING);
return false;
}
yield();
updateTimerState(i, TIMER_STOPPED);
return true;
}