CNetWork.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "stdafx.h"
  2. #include "CNetWork.h"
  3. #include <nlohmann/json.hpp>
  4. #include <fmt/format.h>
  5. CNetWork::CNetWork() : m_http_ret(http_f)
  6. {
  7. }
  8. CNetWork::~CNetWork(void)
  9. {
  10. }
  11. HTTPRET CNetWork::GetServerNode(std::string& data) {
  12. std::vector<cpr::Parameter> p;
  13. std::string text = GetUrl("/api/client/v3/nodes",p,CApp::getSingletonPtr()->GetUserinfo()->access_token);
  14. if (text.empty()) {
  15. return http_f;
  16. }
  17. data = text.c_str();
  18. return http_yes;
  19. }
  20. HTTPRET CNetWork::PostLogin(LPCSTR username, LPCSTR password, std::string& data)
  21. {
  22. std::vector<cpr::Pair> p;
  23. p.push_back({ "email",username });
  24. p.push_back({ "password",password });
  25. std::string text = PostUrl("/api/client/v3/login", p);
  26. if (text.empty()) {
  27. return http_f;
  28. }
  29. data = text.c_str();
  30. return http_yes;
  31. }
  32. void CNetWork::SetUrl(LPCSTR url)
  33. {
  34. m_url = url;
  35. }
  36. SStringA CNetWork::GetLastErrorA()
  37. {
  38. return SStringA().Format("%s", m_error_msg.c_str()).GetBuffer(0);
  39. }
  40. SStringW CNetWork::GetLastErrorW()
  41. {
  42. return S_CA2W(m_error_msg.c_str(),CP_UTF8).GetBuffer(0);
  43. }
  44. std::string CNetWork::GetUrl(std::string path, std::vector<cpr::Parameter> parame,std::string token)
  45. {
  46. cpr::Header hander;
  47. if (!token.empty())
  48. {
  49. hander = cpr::Header{ {"accept", "application/json"} , {"Authorization", "bearer " + token} };
  50. }
  51. else {
  52. hander = cpr::Header{ {"accept", "application/json"} };
  53. }
  54. auto s = fmt::format("{0}{1}", m_url, path.c_str());
  55. cpr::Response r;
  56. if (parame.empty())
  57. {
  58. r = cpr::Get(cpr::Url{ s.c_str() }, hander, cpr::Timeout{ 30 * 100});
  59. }
  60. else {
  61. r = cpr::Get(cpr::Url{ s.c_str() }, hander, cpr::Timeout{ 30 * 100 });
  62. }
  63. if (r.status_code == 200 || r.status_code == 201)
  64. {
  65. return std::move(r.text);
  66. }
  67. else {
  68. m_http_status = r.status_code;
  69. if (r.error.message.empty())
  70. {
  71. m_error_msg = r.status_line;
  72. }
  73. else {
  74. m_error_msg = r.error.message;
  75. }
  76. return "";
  77. }
  78. return std::string("");
  79. }
  80. std::string CNetWork::PostUrl(std::string path, std::vector<cpr::Pair> parame, std::string token)
  81. {
  82. auto s = fmt::format("{0}{1}", m_url, path.c_str());
  83. cpr::Header hander;
  84. if (!token.empty())
  85. {
  86. hander = cpr::Header{ {"accept", "application/json"} , {"Authorization", "bearer " + token} };
  87. }
  88. else {
  89. hander = cpr::Header{ {"accept", "application/json"} };
  90. }
  91. cpr::Response r;
  92. if (parame.empty())
  93. {
  94. r = cpr::Get(cpr::Url{ s.c_str() },hander , cpr::Timeout{ 30 * 100 });
  95. }
  96. else {
  97. r = cpr::Post(cpr::Url{ s.c_str() }, cpr::Payload{ parame.begin(),parame.end() }, hander, cpr::Timeout{ 30 * 100 });
  98. }
  99. if (r.status_code == 200 || r.status_code == 201)
  100. {
  101. return std::move(r.text);
  102. }
  103. else {
  104. m_http_status = r.status_code;
  105. if (r.error.message.empty())
  106. {
  107. m_error_msg = r.status_line;
  108. }
  109. else {
  110. m_error_msg = r.error.message;
  111. }
  112. return "";
  113. }
  114. return std::string("");
  115. }
  116. bool write_data(std::string /*data*/, intptr_t /*userdata*/)
  117. {
  118. return true;
  119. }
  120. bool CNetWork::Download(std::string path)
  121. {
  122. return false;
  123. /*std::string configdir = std::filesystem::current_path().string() + "\\" + CLASHCONFIGDIR;
  124. std::filesystem::path p(configdir);
  125. if (!std::filesystem::exists(p)) {
  126. std::filesystem::create_directory(p);
  127. }
  128. std::string configname = configdir + "\\" CLASHCONFIGNAME;
  129. std::ofstream ofs(configname);
  130. cpr::Url url{ path };
  131. cpr::Session session;
  132. session.SetUrl(url);
  133. cpr::Response response = session.Download(ofs);
  134. return response.status_code == 200;*/
  135. }
  136. void CNetWork::Init()
  137. {
  138. }
  139. void CNetWork::UnInit()
  140. {
  141. }