Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
captainwong
jlib
Commits
c9a1ab5a
Commit
c9a1ab5a
authored
3 years ago
by
captainwong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
retry and timeout
parent
9ea33f82
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
85 additions
and
9 deletions
+85
-9
HttpDlg.cpp
jlib/qt/View/HttpDlg.cpp
+68
-6
HttpDlg.h
jlib/qt/View/HttpDlg.h
+17
-3
No files found.
jlib/qt/View/HttpDlg.cpp
View file @
c9a1ab5a
...
...
@@ -11,9 +11,11 @@ using namespace jlib::qt;
//namespace HBVideoPlatform {
//namespace common {
HttpDlg
::
HttpDlg
(
QWidget
*
parent
,
int
timeout
,
HttpDlgGif
gif
)
HttpDlg
::
HttpDlg
(
QWidget
*
parent
,
int
timeout
,
int
retries
,
HttpDlgGif
gif
)
:
QDialog
(
parent
)
,
timeout
(
timeout
)
,
time_out_sec_
(
timeout
)
,
retrys
(
retrys
)
,
gif_
(
gif
)
{
setWindowModality
(
Qt
::
WindowModal
);
...
...
@@ -52,7 +54,10 @@ HttpDlg::~HttpDlg()
void
HttpDlg
::
get
(
const
QUrl
&
url
)
{
get
(
QNetworkRequest
(
url
));
lastRequest_
=
QNetworkRequest
(
url
);
lastMethod_
=
Method
::
Get
;
lastData_
.
clear
();
get
(
lastRequest_
);
}
void
HttpDlg
::
get
(
const
QNetworkRequest
&
request
)
...
...
@@ -61,6 +66,9 @@ void HttpDlg::get(const QNetworkRequest& request)
disconnect
(
connection_
);
}
connection_
=
connect
(
mgr
,
&
QNetworkAccessManager
::
finished
,
this
,
&
HttpDlg
::
onFinished
);
lastRequest_
=
request
;
lastMethod_
=
Method
::
Get
;
lastData_
.
clear
();
reply_
=
mgr
->
get
(
request
);
run
();
...
...
@@ -69,7 +77,10 @@ void HttpDlg::get(const QNetworkRequest& request)
void
HttpDlg
::
post
(
const
QUrl
&
url
)
{
QNetworkRequest
request
(
url
);
request
.
setHeader
(
QNetworkRequest
::
ContentTypeHeader
,
"application/x-www-form-urlencoded"
);
request
.
setHeader
(
QNetworkRequest
::
ContentTypeHeader
,
"application/x-www-form-urlencoded"
);
lastRequest_
=
request
;
lastMethod_
=
Method
::
Post
;
lastData_
.
clear
();
post
(
request
,
QByteArray
());
}
...
...
@@ -79,6 +90,9 @@ void HttpDlg::post(const QNetworkRequest& request)
disconnect
(
connection_
);
}
connection_
=
connect
(
mgr
,
&
QNetworkAccessManager
::
finished
,
this
,
&
HttpDlg
::
onFinished
);
lastRequest_
=
request
;
lastMethod_
=
Method
::
Post
;
lastData_
.
clear
();
reply_
=
mgr
->
post
(
request
,
QByteArray
());
run
();
...
...
@@ -90,6 +104,9 @@ void HttpDlg::post(const QNetworkRequest & request, const QByteArray & data)
disconnect
(
connection_
);
}
connection_
=
connect
(
mgr
,
&
QNetworkAccessManager
::
finished
,
this
,
&
HttpDlg
::
onFinished
);
lastRequest_
=
request
;
lastMethod_
=
Method
::
Post
;
lastData_
=
data
;
reply_
=
mgr
->
post
(
request
,
data
);
run
();
}
...
...
@@ -100,6 +117,9 @@ void HttpDlg::put(const QNetworkRequest& request, const QByteArray& data)
disconnect
(
connection_
);
}
connection_
=
connect
(
mgr
,
&
QNetworkAccessManager
::
finished
,
this
,
&
HttpDlg
::
onFinished
);
lastRequest_
=
request
;
lastMethod_
=
Method
::
Put
;
lastData_
=
data
;
reply_
=
mgr
->
put
(
request
,
data
);
run
();
}
...
...
@@ -110,6 +130,9 @@ void HttpDlg::patch(const QNetworkRequest& request, const QByteArray& data)
disconnect
(
connection_
);
}
connection_
=
connect
(
mgr
,
&
QNetworkAccessManager
::
finished
,
this
,
&
HttpDlg
::
onFinished
);
lastRequest_
=
request
;
lastMethod_
=
Method
::
Patch
;
lastData_
=
data
;
reply_
=
mgr
->
sendCustomRequest
(
request
,
"PATCH"
,
data
);
run
();
}
...
...
@@ -120,6 +143,9 @@ void HttpDlg::deleteResource(const QNetworkRequest& request)
disconnect
(
connection_
);
}
connection_
=
connect
(
mgr
,
&
QNetworkAccessManager
::
finished
,
this
,
&
HttpDlg
::
onFinished
);
lastRequest_
=
request
;
lastMethod_
=
Method
::
Delete
;
lastData_
.
clear
();
reply_
=
mgr
->
deleteResource
(
request
);
run
();
}
...
...
@@ -145,6 +171,8 @@ void HttpDlg::run()
label_
->
setMovie
(
movie
);
movie
->
start
();
time_out_sec_
=
timeout
;
retry_counter
=
retrys
;
timer_
.
start
();
timer_id_
=
startTimer
(
1000
);
...
...
@@ -166,8 +194,40 @@ void HttpDlg::timerEvent(QTimerEvent * e)
MYQCRITICAL
<<
"timeout"
;
disconnect
(
connection_
);
killTimer
(
timer_id_
);
result_
=
HttpDlgErrorCode
::
Timeout
;
QDialog
::
reject
();
if
(
reply_
)
{
reply_
->
deleteLater
();
}
if
(
retry_counter
<=
0
)
{
result_
=
HttpDlgErrorCode
::
Timeout
;
QDialog
::
reject
();
}
else
{
retry_counter
--
;
MYQDEBUG
<<
"retry_counter ="
<<
retry_counter
;
connection_
=
connect
(
mgr
,
&
QNetworkAccessManager
::
finished
,
this
,
&
HttpDlg
::
onFinished
);
switch
(
lastMethod_
)
{
case
HttpDlg
:
:
Method
::
Get
:
reply_
=
mgr
->
get
(
lastRequest_
);
break
;
case
HttpDlg
:
:
Method
::
Post
:
reply_
=
mgr
->
post
(
lastRequest_
,
lastData_
);
break
;
case
HttpDlg
:
:
Method
::
Put
:
reply_
=
mgr
->
put
(
lastRequest_
,
lastData_
);
break
;
case
HttpDlg
:
:
Method
::
Patch
:
reply_
=
mgr
->
sendCustomRequest
(
lastRequest_
,
"PATCH"
,
lastData_
);
break
;
case
HttpDlg
:
:
Method
::
Delete
:
reply_
=
mgr
->
deleteResource
(
lastRequest_
);
break
;
}
time_out_sec_
=
timeout
;
retry_counter
=
retrys
;
timer_
.
start
();
timer_id_
=
startTimer
(
1000
);
}
}
}
...
...
@@ -207,7 +267,9 @@ void HttpDlg::onFinished(QNetworkReply * reply)
Json
::
Reader
reader
;
root_
.
clear
();
if
(
!
reader
.
parse
(
res
.
constData
(),
root_
))
{
result_
=
HttpDlgErrorCode
::
ParseJsonError
;
if
(
result_
!=
HttpDlgErrorCode
::
HttpStatusNeq200
)
{
result_
=
HttpDlgErrorCode
::
ParseJsonError
;
}
break
;
}
...
...
This diff is collapsed.
Click to expand it.
jlib/qt/View/HttpDlg.h
View file @
c9a1ab5a
...
...
@@ -25,7 +25,15 @@ public:
};
HttpDlg
(
QWidget
*
parent
=
nullptr
,
int
timeOut
=
10
,
HttpDlgGif
gif
=
HttpDlgGif
::
Spinner1s_200px_gray
);
enum
class
Method
{
Get
,
Post
,
Put
,
Patch
,
Delete
,
};
HttpDlg
(
QWidget
*
parent
=
nullptr
,
int
timeOut
=
10
,
int
retries
=
0
,
HttpDlgGif
gif
=
HttpDlgGif
::
Spinner1s_200px_gray
);
~
HttpDlg
();
void
get
(
const
QUrl
&
url
);
...
...
@@ -98,10 +106,16 @@ private:
int
httpStatusCode_
=
0
;
QString
httpReason_
=
{};
Json
::
Value
root_
=
{};
const
int
timeout
;
int
time_out_sec_
=
10
;
int
timer_id_
=
0
;
QNetworkAccessManager
*
mgr
=
{};
QNetworkReply
*
reply_
=
{};
const
int
retrys
;
int
retry_counter
=
0
;
QNetworkAccessManager
*
mgr
{};
QNetworkRequest
lastRequest_
{};
Method
lastMethod_
=
Method
::
Post
;
QByteArray
lastData_
{};
QNetworkReply
*
reply_
{};
QMetaObject
::
Connection
connection_
=
{};
QLabel
*
label_
=
{};
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment