-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeatherAPI.cpp
87 lines (72 loc) · 3.3 KB
/
WeatherAPI.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
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/json.h>
#include <string>
#include <iostream>
#include "WeatherAPI.h"
using namespace web;
using namespace web::http;
using namespace web::http::client;
WeatherAPI::WeatherAPI(std::string apiKey_, std::string city_, std::string units_)
{
apiKey = apiKey_;
city = city_;
units = units_;
}
// Create HTTP client to send requests
http_client WeatherAPI::httpClient() const {
uri_builder builder(U("https://api.openweathermap.org/data/2.5/weather"));
builder.append_query(U("q"), utility::conversions::to_string_t(city));
builder.append_query(U("appid"), utility::conversions::to_string_t(apiKey));
builder.append_query(U("units"), utility::conversions::to_string_t(units));
http_client client(builder.to_string());
return client;
}
// Send request
void WeatherAPI::SendRequest() {
// Capture units by reference in the lambda function
auto& unitsRef = units;
// HTTP client
http_client client = httpClient();
// Send a GET request asynchronously
client.request(methods::GET).then([&unitsRef](http_response response) {
// Check if the request was successful
if (response.status_code() == status_codes::OK) {
// Extract and parse the JSON response
return response.extract_json();
}
else {
// Print an error message
std::cerr << "Failed to get response. Status code: " << response.status_code() << std::endl;
return pplx::task_from_result(json::value());
}
}).then([&unitsRef](json::value responseBody) {
// Check if the "main" object and "weather" array exist in the response
if (responseBody.has_field(U("main")) && responseBody.has_field(U("weather"))) {
// Get temperature and humidity from the "main" object
auto& mainObject = responseBody[U("main")];
if (mainObject.has_field(U("temp"))) {
double temperature = mainObject[U("temp")].as_double();
if (unitsRef == "metric") {
std::cout << "Temperature: " << temperature << " Celsius" << std::endl;
}
else {
std::cout << "Temperature: " << temperature << " Fahrenheit" << std::endl;
}
}
if (mainObject.has_field(U("humidity"))) {
int humidity = mainObject[U("humidity")].as_integer();
std::cout << "Humidity: " << humidity << "%" << std::endl;
}
// Get description (sky condition) from the first element of the "weather" array
auto& weatherArray = responseBody[U("weather")].as_array();
if (weatherArray.size() > 0) {
auto& weatherObject = weatherArray[0];
if (weatherObject.has_field(U("description"))) {
utility::string_t description = weatherObject[U("description")].as_string();
std::cout << "Sky condition: " << utility::conversions::to_utf8string(description) << std::endl;
}
}
}
}).wait();
}