Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
user.cpp
Go to the documentation of this file.
1/**
2 * @file user.cpp
3 * @brief Implements the User class for representing and retrieving GitHub user
4 * data.
5 *
6 * This file defines the behavior of the User class, which includes printing
7 * user information and fetching data from the GitHub API using HTTPS. The
8 * implementation uses Boost.Asio, Boost.Beast, and nlohmann::json for
9 * networking and JSON parsing.
10 */
11
12#include <boost/asio.hpp>
13#include <boost/asio/ssl.hpp>
14#include <boost/beast.hpp>
15#include <iostream>
16#include <nlohmann/json.hpp>
17#include <string>
18
19namespace net = boost::asio;
20namespace ssl = net::ssl;
21namespace beast = boost::beast;
22namespace http = beast::http;
23using tcp = net::ip::tcp;
24
25#include "user.hpp"
26
27/**
28 * @brief Constructs a User instance with provided GitHub profile fields.
29 *
30 * @param login The GitHub login (username).
31 * @param name The user's full name.
32 * @param company The company the user is associated with.
33 * @param location The user's geographic location.
34 */
35User::User(std::string login, std::string name, std::string company,
36 std::string location)
37 : login(std::move(login)), name(std::move(name)),
38 company(std::move(company)), location(std::move(location)) {}
39
40/**
41 * @brief Prints the user information to standard output.
42 *
43 * Displays the login, name, company, and location fields, each on its own line.
44 */
45void User::print() const {
46 std::cout << "Login: " << login << "\n"
47 << "Name: " << name << "\n"
48 << "Company: " << company << "\n"
49 << "Location: " << location << "\n";
50}
51
52/**
53 * @brief Fetches GitHub user information via HTTPS and returns it as a User
54 * object.
55 *
56 * This function connects to the GitHub API
57 * (`https://api.github.com/users/{username}`), performs a GET request, parses
58 * the JSON response, and constructs a User instance containing the login, name,
59 * company, and location fields.
60 *
61 * @throws std::runtime_error if:
62 * - The HTTP response is not 200 OK.
63 * - JSON parsing fails.
64 * - The SSL shutdown process fails.
65 *
66 * @param username The GitHub username to look up.
67 * @return User A populated User object with the retrieved information.
68 */
69User User::fetch_github_user(const std::string &username) {
70 const std::string host = "api.github.com";
71 const std::string target = "/users/" + username;
72
73 net::io_context ioc;
74 ssl::context ctx(ssl::context::sslv23_client);
75 ctx.set_default_verify_paths();
76
77 ssl::stream<tcp::socket> stream(ioc, ctx);
78 tcp::resolver resolver(ioc);
79
80 beast::get_lowest_layer(stream).connect(
81 *resolver.resolve(host, "443").begin());
82 stream.handshake(ssl::stream_base::client);
83
84 http::request<http::string_body> req{http::verb::get, target, 11};
85 req.set(http::field::host, host);
86 req.set(http::field::user_agent, "github_user_fetcher");
87 http::write(stream, req);
88
89 beast::flat_buffer buffer;
90 http::response<http::string_body> res;
91 http::read(stream, buffer, res); // Flawfinder: ignore
92
93 if (res.result() != http::status::ok) {
94 throw std::runtime_error("GitHub request failed: HTTP " +
95 std::to_string(res.result_int()));
96 }
97
98 const auto json = nlohmann::json::parse(res.body(), nullptr, false);
99 if (json.is_discarded()) {
100 throw std::runtime_error("Failed to parse JSON");
101 }
102
103 beast::error_code error_code;
104 if (stream.shutdown(error_code)) {
105 throw beast::system_error{error_code};
106 }
107
108 return User{json.value("login", ""), json.value("name", ""),
109 json.value("company", ""), json.value("location", "")};
110}
const char * name
Definition lsqlite3.c:2154
Definition doctest.h:522
net::ip::tcp tcp
Definition server.cpp:26
Structure representing a GitHub user.
Definition user.h:9
const char * name
Full name.
Definition user.h:11
const char * login
GitHub username.
Definition user.h:10
const char * location
User's location.
Definition user.h:13
const char * company
Company name.
Definition user.h:12