25. External service part 3: Invoking 3rd-party system

25.1. iFun Engine’s HTTP client

iFun Engine provides an HttpClient and HttpClientPool to call RESTful API when platforms that can’t handle iFun Engine’s authentication or billing must be supported or external systems must be connected through the REST method for some other purpose. HttpClientPool is recommended for highly frequent requests and you can see HttpClientPool for configuration.

Tip

For more, please see API documentation.

Note

Since iFun Engine’s HTTP client is based on libcurl, please visit libcurl error codes for the HTTP client’s error codes.

25.1.1. E.g. GET method

25.1.1.1. Synchronous method

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void Example() {
  Ptr<HttpClient> http_client(new HttpClient);

  if (http_client->Get("http://example.com") != CURLE_OK) {
    LOG(ERROR) << "http request failure: "
               << http_client->error_message();
    return;
  }

  const http::Response &response = http_client->response();

  // You can use HttpClientPool like this.
  //
  // http::Response response;
  // HttpClientPool::Get("http://example.com", &response);

  if (response.status_code != http::kOk) {
    LOG(WARNING) << "status code: " << response.status_code;
  }

  LOG(INFO) << "body: " << response.body;

  // Below code shows how to parse json from the response.
  // Json r;
  // r.FromString(response.body);
}

C# provides HttpWebResponse class for basic HTTP communication.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public static void Example()
{
    string strUri = "http://www.example.com";
    System.Net.HttpWebRequest request =
        (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUri);
    request.Method = "GET";

    System.Net.HttpWebResponse response =
        (System.Net.HttpWebResponse)request.GetResponse();

    if (response.StatusCode != HttpStatusCode.OK) {
        Log.Error("http request failure");
        return;
    }

    System.IO.Stream respStream = response.GetResponseStream();
    System.IO.StreamReader streamReader =
        new System.IO.StreamReader(
            respStream, System.Text.Encoding.Default);
    string strResult = streamReader.ReadToEnd();

    Log.Info ("Response: {0}", strResult);

    // Below code shows how to parse json from the response.
    //  JToken token = JToken.Parse (strResult);
    //  if (token is JObject) {
    //    JObject jsonResponse = token.ToObject<JObject>();
    //    Log.Info ("Response: {0}", jsonResponse.ToString());
    //  } else if (token is JArray) {
    //    JArray jsonResponse = token.ToJArray ();
    //    foreach(JObject obj in jsonResponse)
    //    {
    //      Log.Info ("Response: {0}", obj.ToString());
    //    }
    //  }
}

25.1.1.2. Asynchronous method

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
void OnResponseReceived1(const CURLcode code, const http::Response &response) {
  if (code != CURLE_OK) {
    LOG(ERROR) << "http request failure";
    return;
  }

  if (response.status_code != http::kOk) {
    LOG(WARNING) << "status code: " << response.status_code;
  }

  LOG(INFO) << "body: " << response.body;

  // Below code shows how to parse json from the response.
  // Json r;
  // r.FromString(response.body);
}

void OnResponseReceived2(const CURLcode code, const string &error_message,
                         const http::Response &response) {
  if (code != CURLE_OK) {
    LOG(ERROR) << "http request failure: " << error_message;
    return;
  }

  if (response.status_code != http::kOk) {
    LOG(WARNING) << "status code: " << response.status_code;
  }

  LOG(INFO) << "body: " << response.body;

  // Below code shows how to parse json from the response.
  // Json r;
  // r.FromString(response.body);
}

void Example() {
  Ptr<HttpClient> http_client(new HttpClient);

  // You can send a HTTP GET request easily.
  http_client->GetAsync("http://example.com", OnResponseReceived1);

  // GetAsync2() gives an error messages additionally.
  http_client->GetAsync("http://example.com", OnResponseReceived2);

  // You can use HttpClientPool like this.
  //
  // HttpClientPool::GetAsync("http://example.com", OnResponseReceived);
}

C# provides HttpWebResponse and AsyncCallback class for asynchronous HTTP communication.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public static void OnResponseReceived(IAsyncResult result)
{
    System.Net.HttpWebResponse response =
        (result.AsyncState as HttpWebRequest).EndGetResponse (result)
            as HttpWebResponse;

    if (response.StatusCode != HttpStatusCode.OK) {
        Log.Error("http request failure");
        return;
    }

    System.IO.Stream respStream = response.GetResponseStream();
    System.IO.StreamReader streamReader =
        new System.IO.StreamReader(
            respStream, System.Text.Encoding.Default);
    string strResult = streamReader.ReadToEnd();

    Log.Info ("Response: {0}", strResult);

    // Below code shows how to parse json from the response.
    //  JToken token = JToken.Parse (strResult);
    //  if (token is JObject) {
    //    JObject jsonResponse = token.ToObject<JObject>();
    //    Log.Info ("Response: {0}", jsonResponse.ToString());
    //  } else if (token is JArray) {
    //    JArray jsonResponse = token.ToJArray ();
    //    foreach(JObject obj in jsonResponse)
    //    {
    //      Log.Info ("Response: {0}", obj.ToString());
    //    }
    //  }
}

public static void Example()
{
    string strUri = "http://www.example.com";
    System.Net.HttpWebRequest request =
        (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUri);
    request.Method = "GET";

    request.BeginGetResponse(new AsyncCallback(OnResponseReceived), request);
}

25.1.2. E.g. POST method

25.1.2.1. Synchronous method

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void Example() {
  Ptr<HttpClient> http_client(new HttpClient);

  Json data;
  data["example"] = "hello world!";

  // Send a POST request to http://example.com
  // Post function needs specific type of post data, types are as follows:
  //  - fun::Json, std::string, curl_httppost
  if (http_client->Post("http://example.com", data) != CURLE_OK) {
    LOG(ERROR) << "http request failure: "
               << http_client->error_message();
    return;
  }

  const http::Response &response = http_client->response();

  // You can use HttpClientPool like this.
  //
  // http::Response response;
  // HttpClientPool::Post("http://example.com", data, &response);

  if (response.status_code != http::kOk) {
    LOG(WARNING) << "status code: " << response.status_code;
  }

  LOG(INFO) << "body: " << response.body;

  // Below code shows how to parse json from the response.
  // Json r;
  // r.FromString(response.body);
}

C# provides HttpWebResponse class for basic HTTP communication.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public static void Example()
{
    string strUri = "http://example.com";
    System.Net.HttpWebRequest request =
      (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUri);
    request.Method = "POST";

    JObject data = new JObject ();
    data ["example"] = "hello world!";
    byte[] contentBytes =
        System.Text.UTF8Encoding.UTF8.GetBytes(data.ToString());

    request.ContentLength = contentBytes.Length;
    System.IO.Stream requestStream = request.GetRequestStream();
    requestStream.Write(contentBytes, 0, contentBytes.Length);
    requestStream.Close();

    System.Net.HttpWebResponse response =
      (System.Net.HttpWebResponse)request.GetResponse();

    if (response.StatusCode != HttpStatusCode.OK) {
      Log.Warning("http request failure");
      return;
    }

    System.IO.Stream respStream = response.GetResponseStream();
    System.IO.StreamReader streamReader =
      new System.IO.StreamReader(respStream, System.Text.Encoding.Default);
    string strResult = streamReader.ReadToEnd ();

    Log.Info ("Response: {0}", strResult);

    // Below code shows how to parse json from the response.
    //  JToken token = JToken.Parse (strResult);
    //  if (token is JObject) {
    //    JObject jsonResponse = token.ToObject<JObject>();
    //    Log.Info ("Response: {0}", jsonResponse.ToString());
    //  } else if (token is JArray) {
    //    JArray jsonResponse = token.ToJArray ();
    //    foreach(JObject obj in jsonResponse)
    //    {
    //      Log.Info ("Response: {0}", obj.ToString());
    //    }
    //  }
}

25.1.2.2. Asynchronous method

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void OnResponseReceived(const CURLcode code, const string &error_message,
                        const http::Response &response) {
  if (code != CURLE_OK) {
    LOG(ERROR) << "http request failure: " << error_message;
    return;
  }

  if (response.status_code != http::kOk) {
    LOG(WARNING) << "status code: " << response.status_code;
  }

  LOG(INFO) << "body: " << response.body;

  // Below code shows how to parse json from the response.
  // Json r;
  // r.FromString(response.body);
}

void Example() {
  Ptr<HttpClient> http_client(new HttpClient);

  Json data;
  data["example"] = "hello world!";

  // Send a POST request to http://example.com
  // Post function needs specific type of post data, types are as follows:
  //  - fun::Json, std::string, curl_httppost
  http_client->PostAsync2("http://example.com", data, OnResponseReceived);

  // You can use HttpClientPool like this.
  //
  // HttpClientPool::PostAsync("http://example.com", data,
  //                           OnResponseReceived);
}

C# provides HttpWebResponse and AsyncCallback class for asynchronous HTTP communication.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  public static void OnResponseReceived(IAsyncResult result)
  {
      System.Net.HttpWebResponse response =
          (result.AsyncState as HttpWebRequest).EndGetResponse (result)
              as HttpWebResponse;

      if (response.StatusCode != HttpStatusCode.OK) {
          Log.Error("http request failure");
          return;
      }

      System.IO.Stream respStream = response.GetResponseStream();
      System.IO.StreamReader streamReader =
          new System.IO.StreamReader(
              respStream, System.Text.Encoding.Default);
      string strResult = streamReader.ReadToEnd();

      Log.Info ("Response: {0}", strResult);

      // Below code shows how to parse json from the response.
      //  JToken token = JToken.Parse (strResult);
      //  if (token is JObject) {
      //    JObject jsonResponse = token.ToObject<JObject>();
      //    Log.Info ("Response: {0}", jsonResponse.ToString());
      //  } else if (token is JArray) {
      //    JArray jsonResponse = token.ToJArray ();
      //    foreach(JObject obj in jsonResponse)
      //    {
      //      Log.Info ("Response: {0}", obj.ToString());
      //    }
      //  }
  }

  public static void Example()
  {
      string strUri = "http://example.com";
      System.Net.HttpWebRequest request =
        (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUri);
      request.Method = "POST";

      JObject data = new JObject ();
      data ["example"] = "hello world!";
      byte[] contentBytes =
          System.Text.UTF8Encoding.UTF8.GetBytes(data.ToString());

      request.ContentLength = contentBytes.Length;
      System.IO.Stream requestStream = request.GetRequestStream();
      requestStream.Write(contentBytes, 0, contentBytes.Length);
      requestStream.Close();

      request.BeginGetResponse(new AsyncCallback(OnResponseReceived), request);
  }