summaryrefslogtreecommitdiff
path: root/src/discord/httpclient.hpp
blob: 841ce11f096678a27afb8d5d35a3e4ba9f151482 (plain)
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
#pragma once
#include <functional>
#include <future>
#include <string>
#include <unordered_map>
#include <memory>
#include <mutex>
#include <queue>
#include <glibmm.h>
#include "http.hpp"

class HTTPClient {
public:
    HTTPClient();

    void SetBase(const std::string &url);

    void SetUserAgent(std::string agent);
    void SetAuth(std::string auth);
    void SetPersistentHeader(std::string name, std::string value);

    void MakeDELETE(const std::string &path, const std::function<void(http::response_type r)> &cb);
    void MakeGET(const std::string &path, const std::function<void(http::response_type r)> &cb);
    void MakePATCH(const std::string &path, const std::string &payload, const std::function<void(http::response_type r)> &cb);
    void MakePOST(const std::string &path, const std::string &payload, const std::function<void(http::response_type r)> &cb);
    void MakePUT(const std::string &path, const std::string &payload, const std::function<void(http::response_type r)> &cb);

private:
    void AddHeaders(http::request &r);

    void OnResponse(const http::response_type &r, const std::function<void(http::response_type r)> &cb);
    void CleanupFutures();

    mutable std::mutex m_mutex;
    Glib::Dispatcher m_dispatcher;
    std::queue<std::function<void()>> m_queue;
    void RunCallbacks();

    std::vector<std::future<void>> m_futures;
    std::string m_api_base;
    std::string m_authorization;
    std::string m_agent;
    std::unordered_map<std::string, std::string> m_headers;
};