extension-http-outcalls

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

HTTP Outcalls

HTTP Outcalls

HTTP outcalls extension for Caffeine AI.

Overview

概述

This skill adds the ability to make HTTP GET and POST requests from the backend canister. Useful for integrating with external APIs and services.
该技能为后端canister添加了发起HTTP GET和POST请求的能力,适用于与外部API和服务集成。

Backend

后端

For HTTP outcalls that must be performed in the backend:
There is the prefabricated module
mo:caffeineai-http-outcalls/outcall.mo
that that cannot be modified. It provides fundamental functionality for making HTTP GET or PUT requests in the backend.
mo
module {
  public type TransformationInput = {
    context : Blob;
    response : IC.http_request_result;
  };
  public type TransformationOutput = IC.http_request_result;
  public type Transform = query TransformationInput -> async TransformationOutput;
  public type Header = {
    name: Text;
    value: Text;
  };

  // Helper function for the transform callback used by the IC on HTTP outcalls.
  public func transform(input : TransformationInput) : TransformationOutput;

  // HTTP GET request with a transform callback function.
  public func httpGetRequest(url : Text, extraHeaders: [Header], transform : Transform) : async Text;

  // HTTP POST request, specifying a transform callback.
  public func httpPostRequest(url : Text, extraHeaders: [Header], body : Text, transform : Transform) : async Text;
};
Usage for GET:
motoko
import OutCall "mo:caffeineai-http-outcalls/outcall";

actor {
  public query func transform(input: OutCall.TransformationInput) : async OutCall.TransformationOutput {
    OutCall.transform(input);
  };

  func makeGetOutcall(url: Text) : async Text {
    await OutCall.httpGetRequest(url, [], transform);
  };
};
Hint: JSON parsing is not directly supported in Motoko. Better tunnel JSON to frontend for parsing.
POST usage is analogous.
对于必须在后端执行的HTTP outcalls:
有一个预制模块
mo:caffeineai-http-outcalls/outcall.mo
,不可修改。它提供了在后端发起HTTP GET或PUT请求的基础功能。
mo
module {
  public type TransformationInput = {
    context : Blob;
    response : IC.http_request_result;
  };
  public type TransformationOutput = IC.http_request_result;
  public type Transform = query TransformationInput -> async TransformationOutput;
  public type Header = {
    name: Text;
    value: Text;
  };

  // Helper function for the transform callback used by the IC on HTTP outcalls.
  public func transform(input : TransformationInput) : TransformationOutput;

  // HTTP GET request with a transform callback function.
  public func httpGetRequest(url : Text, extraHeaders: [Header], transform : Transform) : async Text;

  // HTTP POST request, specifying a transform callback.
  public func httpPostRequest(url : Text, extraHeaders: [Header], body : Text, transform : Transform) : async Text;
};
GET请求用法:
motoko
import OutCall "mo:caffeineai-http-outcalls/outcall";

actor {
  public query func transform(input: OutCall.TransformationInput) : async OutCall.TransformationOutput {
    OutCall.transform(input);
  };

  func makeGetOutcall(url: Text) : async Text {
    await OutCall.httpGetRequest(url, [], transform);
  };
};
提示:Motoko中不直接支持JSON解析,建议将JSON传递到前端进行解析。
POST请求用法类似。