Skip to content

Commit f68f703

Browse files
committed
Add Downloader class
1 parent fdd98cc commit f68f703

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ target_link_libraries(scratchcpp PRIVATE nlohmann_json::nlohmann_json)
7171
target_link_libraries(scratchcpp PRIVATE utf8cpp)
7272
target_link_libraries(scratchcpp PRIVATE zip)
7373

74+
if (LIBSCRATCHCPP_NETWORK_SUPPORT)
75+
include(FetchContent)
76+
FetchContent_Declare(cpr GIT_REPOSITORY https://github.com/libcpr/cpr.git
77+
GIT_TAG 3b15fa82ea74739b574d705fea44959b58142eb8) # 1.10.5
78+
FetchContent_MakeAvailable(cpr)
79+
target_link_libraries(scratchcpp PRIVATE cpr::cpr)
80+
endif()
81+
7482
target_compile_definitions(scratchcpp PRIVATE LIBSCRATCHCPP_LIBRARY)
7583

7684
if (LIBSCRATCHCPP_BUILD_UNIT_TESTS)

src/internal/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,13 @@ target_sources(scratchcpp
88
zipreader.h
99
projecturl.cpp
1010
projecturl.h
11+
idownloader.h
1112
)
13+
14+
if (LIBSCRATCHCPP_NETWORK_SUPPORT)
15+
target_sources(scratchcpp
16+
PRIVATE
17+
downloader.cpp
18+
downloader.h
19+
)
20+
endif()

src/internal/downloader.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include "downloader.h"
4+
5+
using namespace libscratchcpp;
6+
7+
Downloader::Downloader()
8+
{
9+
}
10+
11+
void Downloader::startDownload(const std::string &url)
12+
{
13+
if (m_asyncResponse)
14+
cancel();
15+
16+
m_asyncResponse = std::make_unique<cpr::AsyncResponse>(cpr::GetAsync(cpr::Url(url)));
17+
}
18+
19+
void Downloader::cancel()
20+
{
21+
if (m_asyncResponse) {
22+
if (!m_asyncResponse->IsCancelled())
23+
(void)(m_asyncResponse->Cancel());
24+
m_asyncResponse.reset();
25+
}
26+
}
27+
28+
void Downloader::wait()
29+
{
30+
if (m_asyncResponse) {
31+
m_asyncResponse->wait();
32+
m_response = m_asyncResponse->get();
33+
}
34+
}
35+
36+
const std::string &Downloader::text() const
37+
{
38+
return m_response.text;
39+
}

src/internal/downloader.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include <cpr/api.h>
6+
#include <cpr/response.h>
7+
8+
#include "idownloader.h"
9+
10+
namespace libscratchcpp
11+
{
12+
13+
class Downloader : public IDownloader
14+
{
15+
public:
16+
Downloader();
17+
18+
void startDownload(const std::string &url) override;
19+
void cancel() override;
20+
void wait() override;
21+
22+
const std::string &text() const override;
23+
24+
private:
25+
std::unique_ptr<cpr::AsyncResponse> m_asyncResponse;
26+
cpr::Response m_response;
27+
};
28+
29+
} // namespace libscratchcpp

src/internal/idownloader.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include <string>
6+
7+
namespace libscratchcpp
8+
{
9+
10+
class IDownloader
11+
{
12+
public:
13+
virtual ~IDownloader() { }
14+
15+
virtual void startDownload(const std::string &url) = 0;
16+
virtual void cancel() = 0;
17+
virtual void wait() = 0;
18+
19+
virtual const std::string &text() const = 0;
20+
};
21+
22+
} // namespace libscratchcpp

0 commit comments

Comments
 (0)