Skip to content

Commit dbc490e

Browse files
authored
miner client submit to v2 api (#613)
1 parent 5767ce0 commit dbc490e

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

src/cmd/test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,8 +980,8 @@ bool Test::checkEvaluator(const Settings& settings, size_t id, std::string path,
980980
void Test::apiClient() {
981981
Log::get().info("Testing API client");
982982
ApiClient client;
983-
client.postProgram(std::string("tests") + FILE_SEP + "programs" + FILE_SEP +
984-
"oeis" + FILE_SEP + "000" + FILE_SEP + "A000005.asm");
983+
// client.postProgram(std::string("tests") + FILE_SEP + "programs" + FILE_SEP
984+
// + "oeis" + FILE_SEP + "000" + FILE_SEP + "A000005.asm");
985985
auto submission = client.getNextSubmission();
986986
if (submission.mode == Submission::Mode::REMOVE) return;
987987
auto program = submission.toProgram();

src/mine/api_client.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
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+
4360
void 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) {

src/mine/api_client.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ class ApiClient {
1212

1313
static ApiClient& getDefaultInstance();
1414

15+
std::string toJson(const Program& program);
16+
1517
void postProgram(const Program& program, size_t max_buffer = 0);
1618

17-
bool postProgram(const std::string& path, bool fail_on_error = true);
19+
//void postSubmission(const Submission& submission, size_t max_buffer = 0);
20+
21+
bool postSubmission(const std::string& path, bool fail_on_error = true);
1822

1923
void postCPUHour();
2024

0 commit comments

Comments
 (0)