| 68 | } |
| 69 | |
| 70 | int AppNet::request(lua_State *L) |
| 71 | { |
| 72 | if (lua_gettop(L) != 1 || lua_type(L, -1) != LUA_TTABLE) |
| 73 | { |
| 74 | lua_pushnil(L); |
| 75 | lua_pushstring(L, "request: param error"); |
| 76 | return 2; |
| 77 | } |
| 78 | QVariantMap reqInfo = getValue(L, false, 2).toMap(); |
| 79 | if (!reqInfo.contains("url") || !reqInfo.contains("method")) |
| 80 | { |
| 81 | lua_pushnil(L); |
| 82 | lua_pushstring(L, "request: must have url,method"); |
| 83 | return 2; |
| 84 | } |
| 85 | QNetworkRequest req; |
| 86 | buildRequest(reqInfo, req); |
| 87 | static const QHash<QString, RequestMethod> methodHash = { |
| 88 | {"get", RequestMethod::GET}, |
| 89 | {"post", RequestMethod::POST}, |
| 90 | {"head", RequestMethod::HEAD}, |
| 91 | {"put", RequestMethod::PUT}, |
| 92 | {"delete", RequestMethod::DELETE}, |
| 93 | }; |
| 94 | RequestMethod method = methodHash.value(reqInfo["method"].toString().toLower(), RequestMethod::UNKNOWN); |
| 95 | QNetworkAccessManager *manager = Network::getManager(); |
| 96 | manager->setCookieJar(nullptr); |
| 97 | QNetworkReply *reply = nullptr; |
| 98 | switch (method) |
| 99 | { |
| 100 | case RequestMethod::GET: |
| 101 | reply = manager->get(req); |
| 102 | break; |
| 103 | case RequestMethod::POST: |
| 104 | reply = manager->post(req, reqInfo.value("data").toByteArray()); |
| 105 | break; |
| 106 | case RequestMethod::HEAD: |
| 107 | reply = manager->head(req); |
| 108 | break; |
| 109 | case RequestMethod::PUT: |
| 110 | reply = manager->put(req, reqInfo.value("data").toByteArray()); |
| 111 | break; |
| 112 | case RequestMethod::DELETE: |
| 113 | reply = manager->deleteResource(req); |
| 114 | break; |
| 115 | default: |
| 116 | reply = manager->sendCustomRequest(req, reqInfo["method"].toByteArray(), reqInfo.value("data").toByteArray()); |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | lua_pushstring(L, RequestData::selfKey); // table key |
| 121 | reply->setParent(nullptr); |
| 122 | RequestData *dt = new RequestData(reply, 0); |
| 123 | dt->push(L); |
| 124 | lua_rawset(L, -3); //table |
| 125 | dt->reqInfoRef = luaL_ref(L, LUA_REGISTRYINDEX); |
| 126 | |
| 127 | QObject::connect(reply, &QNetworkReply::finished, [dt, L](){ |
no test coverage detected