##// END OF EJS Templates
Really basic implementation of Downloader which might replace current...
jeandet -
r1342:91cbf8a85daf
parent child
Show More
@@ -0,0 +1,36
1 #ifndef DOWNLOADER_H
2 #define DOWNLOADER_H
3
4 #include "CoreGlobal.h"
5 #include "Response.h"
6
7 #include <Common/MetaTypes.h>
8 #include <Common/spimpl.h>
9 #include <functional>
10
11 #include <QString>
12 #include <QByteArray>
13
14 /**
15 * @brief The Downloader handles all data donwloads in SciQLOP.
16 */
17 class SCIQLOP_CORE_EXPORT Downloader{
18 public:
19 static Response get(const QString& url);
20 static Response get(const QString& url, const QString& user, const QString& passwd);
21
22 static Downloader& instance()
23 {
24 static Downloader inst;
25 return inst;
26 }
27
28 private:
29 class p_Downloader;
30
31 explicit Downloader();
32
33 spimpl::unique_impl_ptr<Downloader::p_Downloader> impl;
34 };
35
36 #endif // DOWNLOADER_H
@@ -0,0 +1,25
1 #ifndef RESPONSE_H
2 #define RESPONSE_H
3
4 #include <QByteArray>
5 #include <QString>
6
7 /**
8 * @brief The Response class holds a Network request response
9 *
10 */
11 class Response
12 {
13 int _status_code;
14 QByteArray _data;
15 public:
16 Response(){}
17 Response(QByteArray data, int status_code)
18 :_status_code(status_code),_data(data)
19 {
20
21 }
22 int status_code(){return _status_code;}
23 QByteArray& data(){return _data;}
24 };
25 #endif // RESPONSE_H
@@ -0,0 +1,69
1 #include <Network/Downloader.h>
2 #include <memory>
3
4 #include <QNetworkRequest>
5 #include <QNetworkReply>
6 #include <QNetworkAccessManager>
7 #include <QAuthenticator>
8 #include <QVariant>
9 #include <QHash>
10 #include <QPair>
11 #include <QCoreApplication>
12
13 class Downloader::p_Downloader
14 {
15 using login_pair=QPair<QString,QString>;
16 QNetworkAccessManager manager;
17 QHash<QString,login_pair> auth;
18
19 public:
20 explicit p_Downloader()
21 {
22
23 auto login_bambda = [this](QNetworkReply * reply, QAuthenticator * authenticator)
24 {
25 if(auth.contains(reply->url().toString()))
26 {
27 auto login = auth[reply->url().toString()];
28 authenticator->setUser(login.first);
29 authenticator->setPassword(login.second);
30 }
31 };
32 QObject::connect(&manager, &QNetworkAccessManager::authenticationRequired, login_bambda);
33 }
34
35 Response get(const QString& url, const QString &user="", const QString &passwd="")
36 {
37 QNetworkRequest request;
38 request.setUrl(QUrl(url));
39 request.setRawHeader("User-Agent", "SciQLop 1.0");
40 if(user!="" and passwd!="")
41 {
42 //might grow quickly since we can have tons of URLs for the same host
43 auth[url]=login_pair(user,passwd);
44 QString login = "Basic "+user+":"+passwd;
45 request.setRawHeader("Authorization",login.toLocal8Bit());
46 }
47 QNetworkReply *reply = manager.get(request);
48 while (!reply->isFinished())
49 QCoreApplication::processEvents();
50 QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
51 return Response(reply->readAll(), status_code.toInt());
52 }
53 };
54
55 Response Downloader::get(const QString &url)
56 {
57 return Downloader::instance().impl->get(url);
58 }
59
60 Response Downloader::get(const QString &url, const QString &user, const QString &passwd)
61 {
62 return Downloader::instance().impl->get(url, user, passwd);
63 }
64
65 Downloader::Downloader()
66 :impl(spimpl::make_unique_impl<p_Downloader>())
67 {
68 }
69
@@ -0,0 +1,59
1 #include <Network/Downloader.h>
2 #include <QtTest>
3 #include <QObject>
4
5
6 class TestDownloader : public QObject
7
8 {
9 Q_OBJECT
10 public:
11 explicit TestDownloader(QObject *parent = nullptr) : QObject(parent){}
12 signals:
13
14 private slots:
15 void initTestCase(){}
16 void cleanupTestCase(){}
17
18 void simpleGet()
19 {
20 auto resp = Downloader::get("https://httpbin.org/user-agent");
21 QCOMPARE(resp.status_code(), 200);
22 QCOMPARE(resp.data(), QString("{\n \"user-agent\": \"SciQLop 1.0\"\n}\n"));
23 }
24
25 void wrongUrl()
26 {
27 auto resp = Downloader::get("https://lpp.polytechniqe2.fr");
28 QCOMPARE(resp.status_code(), 0);
29 resp = Downloader::get("https://hephaistos.lpp.polytechnique.fr/will_never_exist");
30 QCOMPARE(resp.status_code(), 404);
31
32 }
33
34 void authGet_data()
35 {
36 QTest::addColumn<QString>("url");
37 QTest::addColumn<int>("code");
38
39 QTest::newRow("basic-auth") << "https://httpbin.org/basic-auth/user/passwd" << 200;
40 QTest::newRow("digest-auth") << "https://httpbin.org/digest-auth/auth/user/passwd" << 200;
41 QTest::newRow("hidden-basic-auth") << "https://httpbin.org/hidden-basic-auth/user/passwd" << 404;
42 }
43
44 void authGet()
45 {
46 QFETCH(QString, url);
47 QFETCH(int, code);
48 auto resp = Downloader::get(url, "user", "passwd");
49 QCOMPARE(resp.status_code(), code);
50 }
51
52 private:
53
54 };
55
56
57 QTEST_MAIN(TestDownloader)
58
59 #include "TestDownloader.moc"
@@ -1,126 +1,127
1 FILE (GLOB_RECURSE core_SRCS
1 FILE (GLOB_RECURSE core_SRCS
2 ./include/DataSource/DataSourceItemMergeHelper.h
2 ./include/DataSource/DataSourceItemMergeHelper.h
3 ./include/DataSource/DataSourceItemAction.h
3 ./include/DataSource/DataSourceItemAction.h
4 ./include/DataSource/DataSourceItem.h
4 ./include/DataSource/DataSourceItem.h
5 ./include/DataSource/DataSourceController.h
5 ./include/DataSource/DataSourceController.h
6 ./include/Common/SortUtils.h
6 ./include/Common/SortUtils.h
7 ./include/Common/spimpl.h
7 ./include/Common/spimpl.h
8 ./include/Common/MimeTypesDef.h
8 ./include/Common/MimeTypesDef.h
9 ./include/Common/MetaTypes.h
9 ./include/Common/MetaTypes.h
10 ./include/Common/StringUtils.h
10 ./include/Common/StringUtils.h
11 ./include/Common/SignalWaiter.h
11 ./include/Common/SignalWaiter.h
12 ./include/Common/DateUtils.h
12 ./include/Common/DateUtils.h
13 ./include/Plugin/IPlugin.h
13 ./include/Plugin/IPlugin.h
14 ./include/Data/ArrayDataIterator.h
14 ./include/Data/ArrayDataIterator.h
15 ./include/Data/VariableRequest.h
15 ./include/Data/VariableRequest.h
16 ./include/Data/VectorSeries.h
16 ./include/Data/VectorSeries.h
17 ./include/Data/SqpRange.h
17 ./include/Data/SqpRange.h
18 ./include/Data/ScalarSeries.h
18 ./include/Data/ScalarSeries.h
19 ./include/Data/DataSeriesMergeHelper.h
19 ./include/Data/DataSeriesMergeHelper.h
20 ./include/Data/DataSeries.h
20 ./include/Data/DataSeries.h
21 ./include/Data/AcquisitionDataPacket.h
21 ./include/Data/AcquisitionDataPacket.h
22 ./include/Data/DataSeriesType.h
22 ./include/Data/DataSeriesType.h
23 ./include/Data/AcquisitionRequest.h
23 ./include/Data/AcquisitionRequest.h
24 ./include/Data/SqpIterator.h
24 ./include/Data/SqpIterator.h
25 ./include/Data/ArrayData.h
25 ./include/Data/ArrayData.h
26 ./include/Data/DataSeriesIterator.h
26 ./include/Data/DataSeriesIterator.h
27 ./include/Data/DataSeriesUtils.h
27 ./include/Data/DataSeriesUtils.h
28 ./include/Data/SpectrogramSeries.h
28 ./include/Data/SpectrogramSeries.h
29 ./include/Data/Unit.h
29 ./include/Data/Unit.h
30 ./include/Data/DataProviderParameters.h
30 ./include/Data/DataProviderParameters.h
31 ./include/Data/OptionalAxis.h
31 ./include/Data/OptionalAxis.h
32 ./include/Data/IDataProvider.h
32 ./include/Data/IDataProvider.h
33 ./include/Data/IDataSeries.h
33 ./include/Data/IDataSeries.h
34 ./include/Network/NetworkController.h
34 ./include/Network/NetworkController.h
35 ./include/Version.h
35 ./include/Version.h
36 ./include/CoreGlobal.h
36 ./include/CoreGlobal.h
37 ./include/Catalogue/CatalogueController.h
37 ./include/Catalogue/CatalogueController.h
38 ./include/Visualization/VisualizationController.h
38 ./include/Visualization/VisualizationController.h
39 ./include/PluginManager/PluginManager.h
39 ./include/PluginManager/PluginManager.h
40 ./include/Variable/VariableModel.h
40 ./include/Variable/VariableModel.h
41 ./include/Variable/VariableAcquisitionWorker.h
41 ./include/Variable/VariableAcquisitionWorker.h
42 ./include/Variable/VariableCacheStrategy.h
42 ./include/Variable/VariableCacheStrategy.h
43 ./include/Variable/VariableSynchronizationGroup.h
43 ./include/Variable/VariableSynchronizationGroup.h
44 ./include/Variable/VariableSingleThresholdCacheStrategy.h
44 ./include/Variable/VariableSingleThresholdCacheStrategy.h
45 ./include/Variable/VariableCacheStrategyFactory.h
45 ./include/Variable/VariableCacheStrategyFactory.h
46 ./include/Variable/Variable.h
46 ./include/Variable/Variable.h
47 ./include/Variable/VariableCacheController.h
47 ./include/Variable/VariableCacheController.h
48 ./include/Variable/VariableController.h
48 ./include/Variable/VariableController.h
49 ./include/Time/TimeController.h
49 ./include/Time/TimeController.h
50 ./include/Settings/ISqpSettingsBindable.h
50 ./include/Settings/ISqpSettingsBindable.h
51 ./include/Settings/SqpSettingsDefs.h
51 ./include/Settings/SqpSettingsDefs.h
52
52
53 ./src/DataSource/DataSourceItem.cpp
53 ./src/DataSource/DataSourceItem.cpp
54 ./src/DataSource/DataSourceItemAction.cpp
54 ./src/DataSource/DataSourceItemAction.cpp
55 ./src/DataSource/DataSourceItemMergeHelper.cpp
55 ./src/DataSource/DataSourceItemMergeHelper.cpp
56 ./src/DataSource/DataSourceController.cpp
56 ./src/DataSource/DataSourceController.cpp
57 ./src/Common/DateUtils.cpp
57 ./src/Common/DateUtils.cpp
58 ./src/Common/MimeTypesDef.cpp
58 ./src/Common/MimeTypesDef.cpp
59 ./src/Common/StringUtils.cpp
59 ./src/Common/StringUtils.cpp
60 ./src/Common/SignalWaiter.cpp
60 ./src/Common/SignalWaiter.cpp
61 ./src/Data/ScalarSeries.cpp
61 ./src/Data/ScalarSeries.cpp
62 ./src/Data/DataSeriesIterator.cpp
62 ./src/Data/DataSeriesIterator.cpp
63 ./src/Data/OptionalAxis.cpp
63 ./src/Data/OptionalAxis.cpp
64 ./src/Data/ArrayDataIterator.cpp
64 ./src/Data/ArrayDataIterator.cpp
65 ./src/Data/SpectrogramSeries.cpp
65 ./src/Data/SpectrogramSeries.cpp
66 ./src/Data/DataSeriesUtils.cpp
66 ./src/Data/DataSeriesUtils.cpp
67 ./src/Data/VectorSeries.cpp
67 ./src/Data/VectorSeries.cpp
68 ./src/Network/NetworkController.cpp
68 ./src/Network/NetworkController.cpp
69 ./src/Network/Downloader.cpp
69 ./src/Catalogue/CatalogueController.cpp
70 ./src/Catalogue/CatalogueController.cpp
70 ./src/Visualization/VisualizationController.cpp
71 ./src/Visualization/VisualizationController.cpp
71 ./src/PluginManager/PluginManager.cpp
72 ./src/PluginManager/PluginManager.cpp
72 ./src/Variable/VariableController.cpp
73 ./src/Variable/VariableController.cpp
73 ./src/Variable/VariableModel.cpp
74 ./src/Variable/VariableModel.cpp
74 ./src/Variable/VariableCacheController.cpp
75 ./src/Variable/VariableCacheController.cpp
75 ./src/Variable/VariableSynchronizationGroup.cpp
76 ./src/Variable/VariableSynchronizationGroup.cpp
76 ./src/Variable/Variable.cpp
77 ./src/Variable/Variable.cpp
77 ./src/Variable/VariableAcquisitionWorker.cpp
78 ./src/Variable/VariableAcquisitionWorker.cpp
78 ./src/Version.cpp
79 ./src/Version.cpp
79 ./src/Time/TimeController.cpp
80 ./src/Time/TimeController.cpp
80 ./src/Settings/SqpSettingsDefs.cpp
81 ./src/Settings/SqpSettingsDefs.cpp
81
82
82 )
83 )
83
84
84 add_definitions(-DCORE_STATIC)
85 add_definitions(-DCORE_STATIC)
85
86
86 add_library(sciqlopcore ${core_SRCS})
87 add_library(sciqlopcore ${core_SRCS})
87 SET_TARGET_PROPERTIES(sciqlopcore PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
88 SET_TARGET_PROPERTIES(sciqlopcore PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
88
89
89 target_include_directories(sciqlopcore PUBLIC
90 target_include_directories(sciqlopcore PUBLIC
90 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
91 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
91 $<INSTALL_INTERFACE:include/SciQLOP>
92 $<INSTALL_INTERFACE:include/SciQLOP>
92 )
93 )
93
94
94 target_link_libraries(sciqlopcore PUBLIC
95 target_link_libraries(sciqlopcore PUBLIC
95 Qt5::Core
96 Qt5::Core
96 Qt5::Network
97 Qt5::Network
97 catalogs
98 catalogs
98 )
99 )
99
100
100
101
101 pybind11_add_module(sciqlopqt src/pybind11_wrappers/QtWrappers.cpp)
102 pybind11_add_module(sciqlopqt src/pybind11_wrappers/QtWrappers.cpp)
102 target_link_libraries(sciqlopqt PUBLIC Qt5::Core)
103 target_link_libraries(sciqlopqt PUBLIC Qt5::Core)
103
104
104 pybind11_add_module(pysciqlopcore src/pybind11_wrappers/CoreWrappers.cpp)
105 pybind11_add_module(pysciqlopcore src/pybind11_wrappers/CoreWrappers.cpp)
105 target_link_libraries(pysciqlopcore PUBLIC sciqlopcore)
106 target_link_libraries(pysciqlopcore PUBLIC sciqlopcore)
106
107
107 add_library(pysciqlop src/pybind11_wrappers/pywrappers_common.h)
108 add_library(pysciqlop src/pybind11_wrappers/pywrappers_common.h)
108 target_link_libraries(pysciqlop PUBLIC Qt5::Core)
109 target_link_libraries(pysciqlop PUBLIC Qt5::Core)
109 target_include_directories(pysciqlop PUBLIC
110 target_include_directories(pysciqlop PUBLIC
110 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/pybind11_wrappers/>
111 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/pybind11_wrappers/>
111 $<INSTALL_INTERFACE:include/SciQLOP/py_wrappers>
112 $<INSTALL_INTERFACE:include/SciQLOP/py_wrappers>
112 )
113 )
113
114
114 SET_PROPERTY(GLOBAL PROPERTY CORE_PYTHON_PATH ${CMAKE_CURRENT_BINARY_DIR})
115 SET_PROPERTY(GLOBAL PROPERTY CORE_PYTHON_PATH ${CMAKE_CURRENT_BINARY_DIR})
115
116
116
117
117 install(TARGETS sciqlopcore EXPORT SciQLOPCoreConfig
118 install(TARGETS sciqlopcore EXPORT SciQLOPCoreConfig
118 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
119 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
119 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
120 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
120 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
121 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
121
122
122 install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/SciQLOP)
123 install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/SciQLOP)
123 install(EXPORT SciQLOPCoreConfig DESTINATION share/SciQLOPCore/cmake)
124 install(EXPORT SciQLOPCoreConfig DESTINATION share/SciQLOPCore/cmake)
124 export(TARGETS sciqlopcore FILE SciQLOPCoreConfig.cmake)
125 export(TARGETS sciqlopcore FILE SciQLOPCoreConfig.cmake)
125
126
126 add_subdirectory(tests)
127 add_subdirectory(tests)
@@ -1,33 +1,34
1 include(sciqlop_tests)
1 include(sciqlop_tests)
2
2
3 add_definitions(-DCORE_TESTS_RESOURCES_DIR="${CMAKE_CURRENT_LIST_DIR}/../tests-resources")
3 add_definitions(-DCORE_TESTS_RESOURCES_DIR="${CMAKE_CURRENT_LIST_DIR}/../tests-resources")
4
4
5
5
6 declare_test(TestStringUtils TestStringUtils Common/TestStringUtils.cpp "sciqlopcore;Qt5::Test")
6 declare_test(TestStringUtils TestStringUtils Common/TestStringUtils.cpp "sciqlopcore;Qt5::Test")
7
7
8 declare_test(TestDataSeriesUtils TestDataSeriesUtils Data/TestDataSeriesUtils.cpp "sciqlopcore;Qt5::Test")
8 declare_test(TestDataSeriesUtils TestDataSeriesUtils Data/TestDataSeriesUtils.cpp "sciqlopcore;Qt5::Test")
9 declare_test(TestOptionalAxis TestOptionalAxis Data/TestOptionalAxis.cpp "sciqlopcore;Qt5::Test")
9 declare_test(TestOptionalAxis TestOptionalAxis Data/TestOptionalAxis.cpp "sciqlopcore;Qt5::Test")
10 declare_test(TestSpectrogramSeries TestSpectrogramSeries
10 declare_test(TestSpectrogramSeries TestSpectrogramSeries
11 "Data/TestSpectrogramSeries.cpp;Data/DataSeriesBuilders.h;Data/DataSeriesBuilders.cpp;Data/DataSeriesTestsUtils.h;Data/DataSeriesTestsUtils.cpp"
11 "Data/TestSpectrogramSeries.cpp;Data/DataSeriesBuilders.h;Data/DataSeriesBuilders.cpp;Data/DataSeriesTestsUtils.h;Data/DataSeriesTestsUtils.cpp"
12 "sciqlopcore;Qt5::Test")
12 "sciqlopcore;Qt5::Test")
13 declare_test(TestOneDimArrayData TestOneDimArrayData Data/TestOneDimArrayData.cpp "sciqlopcore;Qt5::Test")
13 declare_test(TestOneDimArrayData TestOneDimArrayData Data/TestOneDimArrayData.cpp "sciqlopcore;Qt5::Test")
14 declare_test(TestScalarSeries TestScalarSeries
14 declare_test(TestScalarSeries TestScalarSeries
15 "Data/TestScalarSeries.cpp;Data/DataSeriesBuilders.h;Data/DataSeriesBuilders.cpp;Data/DataSeriesTestsUtils.h;Data/DataSeriesTestsUtils.cpp"
15 "Data/TestScalarSeries.cpp;Data/DataSeriesBuilders.h;Data/DataSeriesBuilders.cpp;Data/DataSeriesTestsUtils.h;Data/DataSeriesTestsUtils.cpp"
16 "sciqlopcore;Qt5::Test")
16 "sciqlopcore;Qt5::Test")
17 declare_test(TestTwoDimArrayData TestTwoDimArrayData Data/TestTwoDimArrayData.cpp "sciqlopcore;Qt5::Test")
17 declare_test(TestTwoDimArrayData TestTwoDimArrayData Data/TestTwoDimArrayData.cpp "sciqlopcore;Qt5::Test")
18 declare_test(TestVectorSeries TestVectorSeries
18 declare_test(TestVectorSeries TestVectorSeries
19 "Data/TestVectorSeries.cpp;Data/DataSeriesBuilders.h;Data/DataSeriesBuilders.cpp;Data/DataSeriesTestsUtils.h;Data/DataSeriesTestsUtils.cpp"
19 "Data/TestVectorSeries.cpp;Data/DataSeriesBuilders.h;Data/DataSeriesBuilders.cpp;Data/DataSeriesTestsUtils.h;Data/DataSeriesTestsUtils.cpp"
20 "sciqlopcore;Qt5::Test")
20 "sciqlopcore;Qt5::Test")
21
21
22 declare_test(TestDataSourceController TestDataSourceController
22 declare_test(TestDataSourceController TestDataSourceController
23 "DataSource/TestDataSourceController.cpp;DataSource/DataSourceItemBuilder.cpp"
23 "DataSource/TestDataSourceController.cpp;DataSource/DataSourceItemBuilder.cpp"
24 "sciqlopcore;Qt5::Test")
24 "sciqlopcore;Qt5::Test")
25 declare_test(TestDataSourceItem TestDataSourceItem
25 declare_test(TestDataSourceItem TestDataSourceItem
26 "DataSource/TestDataSourceItem.cpp;DataSource/DataSourceItemBuilder.cpp"
26 "DataSource/TestDataSourceItem.cpp;DataSource/DataSourceItemBuilder.cpp"
27 "sciqlopcore;Qt5::Test")
27 "sciqlopcore;Qt5::Test")
28
28
29 declare_test(TestVariable TestVariable Variable/TestVariable.cpp "sciqlopcore;Qt5::Test")
29 declare_test(TestVariable TestVariable Variable/TestVariable.cpp "sciqlopcore;Qt5::Test")
30 declare_test(TestVariableCacheController TestVariableCacheController Variable/TestVariableCacheController.cpp "sciqlopcore;Qt5::Test")
30 declare_test(TestVariableCacheController TestVariableCacheController Variable/TestVariableCacheController.cpp "sciqlopcore;Qt5::Test")
31 declare_test(TestVariableController TestVariableController Variable/TestVariableController.cpp "sciqlopcore;Qt5::Test")
31 declare_test(TestVariableController TestVariableController Variable/TestVariableController.cpp "sciqlopcore;Qt5::Test")
32 declare_test(TestVariableSync TestVariableSync Variable/TestVariableSync.cpp "sciqlopcore;Qt5::Test")
32 declare_test(TestVariableSync TestVariableSync Variable/TestVariableSync.cpp "sciqlopcore;Qt5::Test")
33
33
34 declare_test(TestDownloader TestDownloader Network/TestDownloader.cpp "sciqlopcore;Qt5::Test")
General Comments 0
You need to be logged in to leave comments. Login now