aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/http/client/query.cpp
blob: 36a946074b1d6c8799041a25329b4b2a2b66c2bf (plain) (blame)
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
#include "query.h"
#include "request.h"

namespace NHttp {
    TFetchQuery::TFetchQuery(const TString& url,
                             const TFetchOptions& options)
        : Url_(url)
        , Options_(options)
    {
    }

    TFetchQuery::TFetchQuery(const TString& url,
                             const TVector<TString>& headers,
                             const TFetchOptions& options)
        : Url_(url)
        , Headers_(headers)
        , Options_(options)
    {
    }

    TFetchQuery::~TFetchQuery() = default;

    TString TFetchQuery::GetUrl() const {
        return Url_;
    }

    TFetchQuery& TFetchQuery::OnFail(TOnFail cb) {
        OnFailCb_ = cb;
        return *this;
    }

    TFetchQuery& TFetchQuery::OnRedirect(TOnRedirect cb) {
        OnRedirectCb_ = cb;
        return *this;
    }

    TFetchQuery& TFetchQuery::OnPartialRead(NHttpFetcher::TNeedDataCallback cb) {
        OnPartialReadCb_ = cb;
        return *this;
    }

    TFetchRequestRef TFetchQuery::ConstructRequest() const {
        TFetchRequestRef request = new TFetchRequest(Url_, Headers_, Options_);
        if (OnFailCb_) {
            request->SetOnFail(*OnFailCb_);
        }

        if (OnRedirectCb_) {
            request->SetOnRedirect(*OnRedirectCb_);
        }

        if (OnPartialReadCb_) {
            request->SetOnPartialRead(*OnPartialReadCb_);
        }

        return request;
    }

    TFetchState::TFetchState() {
    }

    TFetchState::TFetchState(const TFetchRequestRef& req)
        : Request_(req)
    {
    }

    void TFetchState::Cancel() const {
        if (Request_) {
            Request_->Cancel();
        }
    }

    NHttpFetcher::TResultRef TFetchState::Get() const {
        if (Request_) {
            WaitI();
            return Request_->MakeResult();
        }
        return NHttpFetcher::TResultRef();
    }

    void TFetchState::WaitI() const {
        WaitT(TDuration::Max());
    }

    bool TFetchState::WaitT(TDuration timeout) const {
        if (Request_) {
            return Request_->WaitT(timeout);
        }
        return false;
    }

}