Skip to content

Commit

Permalink
Refactor COSClient to support POST requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Porter committed Jul 9, 2024
1 parent 17d1fa0 commit 08247bd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
52 changes: 52 additions & 0 deletions cosclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,30 @@ preResponse COSClient::invokeDeleteRequest(const QString& path, const preRequest
return response;
}

preResponse COSClient::invokePostRequest(const QString &path, const preRequest &request)
{
preResponse response;
// 构建请求
QNetworkRequest networkRequest = buildPostRequest(path, request.queryParams, request.data); // 使用提供的代码片段构建POST请求
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, request.contentType);
networkRequest.setRawHeader("Content-MD5",_getContentMD5(request.data).toUtf8());
// 发送请求并等待响应
QNetworkReply* reply = manager->post(networkRequest, request.data);
// 等待响应完成
QEventLoop loop;
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
// 处理响应
response.data = reply->readAll();
response.statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
const QList<QByteArray> rawHeaderList = reply->rawHeaderList();
for (const QByteArray& header : rawHeaderList) {
response.headers.insert(QString(header), QString(reply->rawHeader(header)));
}
reply->deleteLater();
return response;
}

QString COSClient::buildTagXmlFromMap(const QMap<QString, QString> &map) {
QDomDocument doc;
QDomElement root = doc.createElement("Tagging");
Expand Down Expand Up @@ -461,3 +485,31 @@ QNetworkRequest COSClient::buildDeleteRequest(const QString &path, const QMap<QS
return request;
}

QNetworkRequest COSClient::buildPostRequest(const QString &path, const QMap<QString, QString> queryParams, const QByteArray &data)
{
if (!preCheckSession())
{
return QNetworkRequest();
}
QUrl url;
QUrlQuery query;
url=QUrl(generalApiUrl+path);
for(auto it=queryParams.begin();it!=queryParams.end();it++)
{
query.addQueryItem(it.key(),it.value());
}
url.setQuery(query);
QNetworkRequest request(url);
request.setRawHeader("Host",endpoint.toUtf8());
//设置请求方法
request.setAttribute(QNetworkRequest::CustomVerbAttribute,"POST");
QString sig=this->signHelper->generateSignature(request,300);
request.setRawHeader("Authorization",sig.toUtf8());
if(token!="")
{
request.setRawHeader("x-cos-security-token",token.toUtf8());
}
return request;
}


11 changes: 11 additions & 0 deletions cosclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ struct preRequest{
struct preResponse{
QByteArray data;
QMap<QString,QString> headers;
QMap<QString,QString> getMetaDatas(){
// x-cos-meta-*
QMap<QString,QString> metaDatas;
for(auto it=headers.begin();it!=headers.end();it++){
if(it.key().startsWith("x-cos-meta-")){
metaDatas[it.key().mid(11)]=it.value();
}
}
return metaDatas;
};
int statusCode;
};
class COSClient : public QObject
Expand Down Expand Up @@ -73,10 +82,12 @@ class COSClient : public QObject
QNetworkRequest buildPutRequest(const QString& path,const QMap<QString, QString> queryParams, const QByteArray& data);
QNetworkRequest buildHeadRequest(const QString& path,const QMap<QString, QString> queryParams);
QNetworkRequest buildDeleteRequest(const QString& path,const QMap<QString, QString> queryParams);
QNetworkRequest buildPostRequest(const QString& path,const QMap<QString, QString> queryParams, const QByteArray& data);
preResponse invokeGetFileRequest(const QString& path, const preRequest& request);
preResponse invokePutRequest(const QString& path, const preRequest& request);
preResponse invokeHeadRequest(const QString& path, const preRequest& request);
preResponse invokeDeleteRequest(const QString& path, const preRequest& request);
preResponse invokePostRequest(const QString& path, const preRequest& request);
// 构建XML字符串
QString buildTagXmlFromMap(const QMap<QString, QString> &map);

Expand Down

0 comments on commit 08247bd

Please sign in to comment.