Converts markdown to HTML using the GitHub API markdown endpoint.
116 {
117
118 CURL* curl = curl_easy_init();
119 CURLcode resultCode;
120 string htmlText;
121 struct curl_slist* headers = NULL;
122 headers = curl_slist_append(headers, ((const string)"Accept: application/vnd.github+json").c_str());
123 headers = curl_slist_append(headers, ("Authorization: token " + githubToken).c_str());
124
125 if (curl) {
128 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &htmlText);
129 curl_easy_setopt(curl, CURLOPT_USERAGENT, "Ahmed-Khaled-dev");
130 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
131
132 json postData;
133 postData["text"] = markdownText;
134 string postDataString = postData.dump();
135 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postDataString.c_str());
136
137 resultCode = curl_easy_perform(curl);
138
139 if (resultCode == CURLE_OK) {
140
141 long httpCode;
142 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
143
144 if (httpCode == 200) {
145 return htmlText;
146 }
147 else if (httpCode == 404) {
148 throw runtime_error("Markdown API url not found");
149 }
150 else {
152 }
153 }
154 else {
156 }
157
158 curl_easy_cleanup(curl);
159 curl_slist_free_all(headers);
160 }
161 else {
163 }
164
165 return htmlText;
166}
void handleGithubApiErrorCodes(long errorCode, string apiResponse)
Throws runtime exceptions with appropriate messages that describe the given GitHub API error code All...
Definition Utils.cpp:78
size_t handleApiCallBack(char *data, size_t size, size_t numOfBytes, string *buffer)
Callback function that is required to handle API response in libcurl.
Definition Utils.cpp:66
string githubMarkdownApiUrl
Definition Config.h:22
string githubApiUnableToMakeRequestError
Definition Config.h:65
string githubApiLibcurlError
Definition Config.h:66