empiredan commented on code in PR #1583:
URL: 
https://github.com/apache/incubator-pegasus/pull/1583#discussion_r1335410953


##########
src/http/http_client.h:
##########
@@ -0,0 +1,144 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#pragma once
+
+#include <curl/curl.h>
+#include <stddef.h>
+#include <functional>
+#include <string>
+#include <unordered_map>
+
+#include "http/http_method.h"
+#include "utils/errors.h"
+#include "utils/ports.h"
+#include "utils/string_view.h"
+
+namespace dsn {
+
+// A library for http client that provides convenient APIs to access http 
services, implemented
+// based on libcurl (https://curl.se/libcurl/c/).
+//
+// This class is not thread-safe. Thus maintain one instance for each thread.
+//
+// Example of submitting GET request to remote http service
+// --------------------------------------------------------
+// Create an instance of http_client:
+// http_client client;
+//
+// It's necessary to initialize the new instance before coming into use:
+// auto err = client.init();
+//
+// Specify the target url that you would request for:
+// err = client.set_url(method);
+//
+// If you would use GET method, call `with_get_method`:
+// err = client.with_get_method();
+//
+// If you would use POST method, call `with_post_method` with post data:
+// err = client.with_post_method(post_data);
+//
+// Submit the request to remote http service:
+// err = client.do_method();
+//
+// If response data should be processed, use callback function:
+// auto callback = [...](const void *data, size_t length) {
+//     ......
+//     return true;
+// };
+// err = client.do_method(callback);
+//
+// Or just provide a string pointer:
+// std::string response;
+// err = client.do_method(&response);
+//
+// Get the http status code after requesting:
+// long http_status;
+// err = client.get_http_status(http_status);
+class http_client
+{
+public:
+    using http_callback = std::function<bool(const void *data, size_t length)>;
+
+    http_client();
+    ~http_client();
+
+    // Before coming into use, init() must be called to initialize http 
client. It could also be
+    // called to reset the http clients that have been initialized previously.
+    dsn::error_s init();
+
+    // Specify the target url that the request would be sent for.
+    dsn::error_s set_url(const std::string &url);
+
+    // Using post method, with `data` as the payload for post body.
+    dsn::error_s with_post_method(const std::string &data);
+
+    // Using get method.
+    dsn::error_s with_get_method();
+
+    // Specify the maximum time in milliseconds that a request is allowed to 
complete.
+    dsn::error_s set_timeout(long timeout_ms);
+
+    // Operations for the header fields.
+    void clear_header_fields();
+    void set_accept(dsn::string_view val);
+    void set_content_type(dsn::string_view val);
+
+    // Submit request to remote http service, with response processed by 
callback function.
+    dsn::error_s do_method(const http_callback &callback = {});

Review Comment:
   OK, I added test cases for non-empty callback.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to