调用 Lambda 函数
HolaCloud Lambda 支持直接管理调用、公开调用、按所有者划分的 mux 路由,以及少量服务检查端点。
管理调用
当客户端可以发送 X-Glue-Authentication 时,使用 /api/v0/run/{lambda_id}。该端点接受任何 HTTP 方法。
curl -X POST "https://api.hola.cloud/api/v0/run/YOUR_LAMBDA_ID" \
-H "X-Glue-Authentication: YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"key": "value"
}'POST /api/v0/run/YOUR_LAMBDA_ID HTTP/1.1
Host: api.hola.cloud
X-Glue-Authentication: YOUR_TOKEN
Content-Type: application/json
{
"key": "value"
}package main
import (
"fmt"
"io"
"net/http"
"encoding/json"
"strings"
)
func main() {
payload := map[string]any{"key": "value"}
bodyBytes, err := json.Marshal(payload)
if err != nil {
panic(err)
}
body := string(bodyBytes)
req, err := http.NewRequest("POST", "https://api.hola.cloud/api/v0/run/YOUR_LAMBDA_ID", strings.NewReader(body))
if err != nil {
panic(err)
}
req.Header.Set("X-Glue-Authentication", "YOUR_TOKEN")
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(responseBody))
}
<?php
$payload = ['key' => 'value'];
$body = json_encode($payload);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.hola.cloud/api/v0/run/YOUR_LAMBDA_ID',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => [
'X-Glue-Authentication: YOUR_TOKEN',
'Content-Type: application/json',
],
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
echo $response;
import requests
import json
headers = {
"X-Glue-Authentication": "YOUR_TOKEN",
"Content-Type": "application/json",
}
payload = {"key": "value"}
body = json.dumps(payload)
response = requests.request(
"POST",
"https://api.hola.cloud/api/v0/run/YOUR_LAMBDA_ID",
headers=headers,
data=body
)
print(response.text)
const payload = {"key": "value"};
const response = await fetch("https://api.hola.cloud/api/v0/run/YOUR_LAMBDA_ID", {
method: "POST",
headers: {
"X-Glue-Authentication": "YOUR_TOKEN",
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
console.log(await response.text());
const payload = {"key": "value"};
const response = await fetch("https://api.hola.cloud/api/v0/run/YOUR_LAMBDA_ID", {
method: "POST",
headers: {
"X-Glue-Authentication": "YOUR_TOKEN",
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception {
var payload = Map.of("key", "value");
var body = new ObjectMapper().writeValueAsString(payload);
var request = HttpRequest.newBuilder()
.uri(URI.create("https://api.hola.cloud/api/v0/run/YOUR_LAMBDA_ID"))
.method("POST", HttpRequest.BodyPublishers.ofString(body))
.header("X-Glue-Authentication", "YOUR_TOKEN")
.header("Content-Type", "application/json")
.build();
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}公开调用
对 webhook、浏览器调用和不应发送管理凭据的其他客户端,使用 /run/{lambda_id}。该端点接受任何 HTTP 方法。
curl -X POST "https://api.hola.cloud/run/YOUR_LAMBDA_ID" \
-H "Content-Type: application/json" \
-d '{
"key": "value"
}'POST /run/YOUR_LAMBDA_ID HTTP/1.1
Host: api.hola.cloud
Content-Type: application/json
{
"key": "value"
}package main
import (
"fmt"
"io"
"net/http"
"encoding/json"
"strings"
)
func main() {
payload := map[string]any{"key": "value"}
bodyBytes, err := json.Marshal(payload)
if err != nil {
panic(err)
}
body := string(bodyBytes)
req, err := http.NewRequest("POST", "https://api.hola.cloud/run/YOUR_LAMBDA_ID", strings.NewReader(body))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(responseBody))
}
<?php
$payload = ['key' => 'value'];
$body = json_encode($payload);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.hola.cloud/run/YOUR_LAMBDA_ID',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
],
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
echo $response;
import requests
import json
headers = {
"Content-Type": "application/json",
}
payload = {"key": "value"}
body = json.dumps(payload)
response = requests.request(
"POST",
"https://api.hola.cloud/run/YOUR_LAMBDA_ID",
headers=headers,
data=body
)
print(response.text)
const payload = {"key": "value"};
const response = await fetch("https://api.hola.cloud/run/YOUR_LAMBDA_ID", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
console.log(await response.text());
const payload = {"key": "value"};
const response = await fetch("https://api.hola.cloud/run/YOUR_LAMBDA_ID", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception {
var payload = Map.of("key", "value");
var body = new ObjectMapper().writeValueAsString(payload);
var request = HttpRequest.newBuilder()
.uri(URI.create("https://api.hola.cloud/run/YOUR_LAMBDA_ID"))
.method("POST", HttpRequest.BodyPublishers.ofString(body))
.header("Content-Type", "application/json")
.build();
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}lambda 通过 req 接收请求数据,包括方法、path、headers、query 值和 body。
Webhook 用法
将外部服务配置为向以下地址发送事件:
1https://api.hola.cloud/run/YOUR_LAMBDA_ID
Webhook 处理器示例:
1export default (req) => {
2 return {
3 body: {
4 received: true,
5 event: req.headers["x-github-event"],
6 payload: req.body
7 }
8 };
9};
Mux Router
Mux router 通过 /mux/{owner_id}/* 转发按所有者划分的路由。它接受任何 HTTP 方法。
curl -X GET "https://api.hola.cloud/mux/OWNER_ID/any/path/here"GET /mux/OWNER_ID/any/path/here HTTP/1.1
Host: api.hola.cloud
package main
import (
"fmt"
"io"
"net/http"
)
func main() {
req, err := http.NewRequest("GET", "https://api.hola.cloud/mux/OWNER_ID/any/path/here", nil)
if err != nil {
panic(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(responseBody))
}
<?php
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.hola.cloud/mux/OWNER_ID/any/path/here',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'GET',
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
echo $response;
import requests
response = requests.request(
"GET",
"https://api.hola.cloud/mux/OWNER_ID/any/path/here"
)
print(response.text)
const response = await fetch("https://api.hola.cloud/mux/OWNER_ID/any/path/here", {
method: "GET"
});
console.log(await response.text());
const response = await fetch("https://api.hola.cloud/mux/OWNER_ID/any/path/here", {
method: "GET"
});
const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
var request = HttpRequest.newBuilder()
.uri(URI.create("https://api.hola.cloud/mux/OWNER_ID/any/path/here"))
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}owner_id 标识 HolaCloud 所有者。剩余 path 会转发给 lambda 路由逻辑。
进行中的调用
查看当前正在运行的调用:
curl "https://api.hola.cloud/ongoing"GET /ongoing HTTP/1.1
Host: api.hola.cloud
package main
import (
"fmt"
"io"
"net/http"
)
func main() {
req, err := http.NewRequest("GET", "https://api.hola.cloud/ongoing", nil)
if err != nil {
panic(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(responseBody))
}
<?php
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.hola.cloud/ongoing',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'GET',
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
echo $response;
import requests
response = requests.request(
"GET",
"https://api.hola.cloud/ongoing"
)
print(response.text)
const response = await fetch("https://api.hola.cloud/ongoing", {
method: "GET"
});
console.log(await response.text());
const response = await fetch("https://api.hola.cloud/ongoing", {
method: "GET"
});
const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
var request = HttpRequest.newBuilder()
.uri(URI.create("https://api.hola.cloud/ongoing"))
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}当前用户
/me 端点返回已认证用户的信息:
curl "https://api.hola.cloud/me" \
-H "X-Glue-Authentication: YOUR_TOKEN"GET /me HTTP/1.1
Host: api.hola.cloud
X-Glue-Authentication: YOUR_TOKEN
package main
import (
"fmt"
"io"
"net/http"
)
func main() {
req, err := http.NewRequest("GET", "https://api.hola.cloud/me", nil)
if err != nil {
panic(err)
}
req.Header.Set("X-Glue-Authentication", "YOUR_TOKEN")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(responseBody))
}
<?php
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.hola.cloud/me',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => [
'X-Glue-Authentication: YOUR_TOKEN',
],
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
echo $response;
import requests
headers = {
"X-Glue-Authentication": "YOUR_TOKEN",
}
response = requests.request(
"GET",
"https://api.hola.cloud/me",
headers=headers
)
print(response.text)
const response = await fetch("https://api.hola.cloud/me", {
method: "GET",
headers: {
"X-Glue-Authentication": "YOUR_TOKEN"
}
});
console.log(await response.text());
const response = await fetch("https://api.hola.cloud/me", {
method: "GET",
headers: {
"X-Glue-Authentication": "YOUR_TOKEN"
}
});
const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
var request = HttpRequest.newBuilder()
.uri(URI.create("https://api.hola.cloud/me"))
.method("GET", HttpRequest.BodyPublishers.noBody())
.header("X-Glue-Authentication", "YOUR_TOKEN")
.build();
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}OpenAPI 规范
HolaCloud 在以下地址公开 Lambda OpenAPI 文档:
curl -X GET "https://api.hola.cloud/openapi"GET /openapi HTTP/1.1
Host: api.hola.cloud
package main
import (
"fmt"
"io"
"net/http"
)
func main() {
req, err := http.NewRequest("GET", "https://api.hola.cloud/openapi", nil)
if err != nil {
panic(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(responseBody))
}
<?php
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.hola.cloud/openapi',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'GET',
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
echo $response;
import requests
response = requests.request(
"GET",
"https://api.hola.cloud/openapi"
)
print(response.text)
const response = await fetch("https://api.hola.cloud/openapi", {
method: "GET"
});
console.log(await response.text());
const response = await fetch("https://api.hola.cloud/openapi", {
method: "GET"
});
const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
var request = HttpRequest.newBuilder()
.uri(URI.create("https://api.hola.cloud/openapi"))
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}
评论