44#include < sstream>
55#include < thread>
66
7+ #include " lang/comments.hpp"
78#include " lang/program_util.hpp"
89#include " sys/file.hpp"
910#include " sys/git.hpp"
@@ -40,18 +41,34 @@ ApiClient& ApiClient::getDefaultInstance() {
4041 return api_client;
4142}
4243
44+ std::string ApiClient::toJson (const Program& program){
45+ const std::string id = Comments::getSequenceIdFromProgram (program);
46+ const std::string submitter = Comments::getSubmitter (program);
47+ const std::string change_type = Comments::getCommentField (program, Comments::PREFIX_CHANGE_TYPE);
48+ const std::string mode = ((change_type == " " || change_type == " Found" ) ? " add" : " update" );
49+ const std::string type = " program" ;
50+ std::ostringstream oss;
51+ ProgramUtil::print (program, oss);
52+ const std::string content = oss.str ();
53+ return " {\" id\" :\" " + escapeJsonString (id) + " \" ,"
54+ " \" submitter\" :\" " + escapeJsonString (submitter) + " \" ,"
55+ " \" mode\" :\" " + escapeJsonString (mode) + " \" ,"
56+ " \" type\" :\" " + escapeJsonString (type) + " \" ,"
57+ " \" content\" :\" " + escapeJsonString (content) + " \" }" ;
58+ }
59+
4360void ApiClient::postProgram (const Program& program, size_t max_buffer) {
4461 // attention: curl sometimes has problems with absolute paths.
4562 // so we use a relative path here!
46- const std::string tmp = " post_program_" + std::to_string (client_id) + " .asm " ;
63+ const std::string tmp = " post_program_" + std::to_string (client_id) + " .json " ;
4764 out_queue.push_back (program);
4865 while (!out_queue.empty ()) {
4966 {
5067 std::ofstream out (tmp);
51- ProgramUtil::print (out_queue.back (), out );
68+ out << toJson (out_queue.back ());
5269 out.close ();
5370 }
54- if (postProgram (tmp, out_queue.size () > max_buffer)) {
71+ if (postSubmission (tmp, out_queue.size () > max_buffer)) {
5572 out_queue.pop_back ();
5673 } else {
5774 break ;
@@ -60,11 +77,11 @@ void ApiClient::postProgram(const Program& program, size_t max_buffer) {
6077 std::remove (tmp.c_str ());
6178}
6279
63- bool ApiClient::postProgram (const std::string& path, bool fail_on_error) {
80+ bool ApiClient::postSubmission (const std::string& path, bool fail_on_error) {
6481 if (!isFile (path)) {
6582 Log::get ().error (" File not found: " + path, true );
6683 }
67- const std::string url = base_url + " programs " ;
84+ const std::string url = base_url_v2 + " submissions " ;
6885 if (!WebClient::postFile (url, path)) {
6986 const std::string msg (" Cannot submit program to API server" );
7087 if (fail_on_error) {
0 commit comments