Welcome to mirror list, hosted at ThFree Co, Russian Federation.

http_request.hpp « platform - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: af3e055b8676a0099861ddbf7d9e241c137c9a04 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#pragma once

#include "../std/stdint.hpp"
#include "../std/function.hpp"
#include "../std/string.hpp"
#include "../std/vector.hpp"
#include "../std/utility.hpp"

namespace downloader
{

/// Request in progress will be canceled on delete
class HttpRequest
{
public:
  enum StatusT
  {
    EInProgress,
    ECompleted,
    EFailed
  };

  /// <current, total>, total can be -1 if size is unknown
  typedef pair<int64_t, int64_t> ProgressT;
  typedef function<void (HttpRequest &)> CallbackT;

protected:
  StatusT m_status;
  ProgressT m_progress;
  CallbackT m_onFinish;
  CallbackT m_onProgress;

  explicit HttpRequest(CallbackT const & onFinish, CallbackT const & onProgress);

public:
  virtual ~HttpRequest() = 0;

  StatusT Status() const { return m_status; }
  ProgressT Progress() const { return m_progress; }
  /// Either file path (for chunks) or downloaded data
  virtual string const & Data() const = 0;

  /// Response saved to memory buffer and retrieved with Data()
  static HttpRequest * Get(string const & url,
                           CallbackT const & onFinish,
                           CallbackT const & onProgress = CallbackT());

  /// Content-type for request is always "application/json"
  static HttpRequest * PostJson(string const & url, string const & postData,
                                CallbackT const & onFinish,
                                CallbackT const & onProgress = CallbackT());

  /// Download file to filePath.
  /// @param[in]  fileSize  Correct file size (needed for resuming and reserving).
  static HttpRequest * GetFile(vector<string> const & urls,
                               string const & filePath, int64_t fileSize,
                               CallbackT const & onFinish,
                               CallbackT const & onProgress = CallbackT(),
                               int64_t chunkSize = 512 * 1024,
                               bool doCleanOnCancel = true);
};

} // namespace downloader