管理 Lambda 函数

创建 lambda 后,可以使用管理 API 查看它、更新支持的字段、列出账户中的所有 lambdas,或删除它。

函数结构

JavaScript lambdas 导出默认处理器。处理器接收请求对象,并返回希望 HolaCloud 发回的响应 body。

 1export default (req) => {
 2  return {
 3    body: {
 4      method: req.method,
 5      path: req.path,
 6      headers: req.headers,
 7      data: req.body
 8    }
 9  };
10};

静态 lambdas 使用一种静态语言模式:static-htmlstatic-cssstatic-js。在这些模式中,code 是对应 lambda 提供的内容。

更新 Lambda

使用 PATCH /api/v0/lambdas/{lambda_id} 更新 namelanguagecodemethodpath

curl -X PATCH "https://api.hola.cloud/api/v0/lambdas/YOUR_LAMBDA_ID" \
  -H "X-Glue-Authentication: YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "hello-updated",
    "method": "POST",
    "path": "/hello-updated",
    "code": "export default (req) => ({ body: { message: \"Updated lambda\", data: req.body } })"
  }'
PATCH /api/v0/lambdas/YOUR_LAMBDA_ID HTTP/1.1
Host: api.hola.cloud
X-Glue-Authentication: YOUR_TOKEN
Content-Type: application/json

{
    "name": "hello-updated",
    "method": "POST",
    "path": "/hello-updated",
    "code": "export default (req) => ({ body: { message: \"Updated lambda\", data: req.body } })"
  }
package main

import (
	"fmt"
	"io"
	"net/http"
	"encoding/json"
	"strings"
)

func main() {
	payload := map[string]any{"code": "export default (req) => ({ body: { message: \"Updated lambda\", data: req.body } })", "method": "POST", "name": "hello-updated", "path": "/hello-updated"}
	bodyBytes, err := json.Marshal(payload)
	if err != nil {
		panic(err)
	}
	body := string(bodyBytes)

	req, err := http.NewRequest("PATCH", "https://api.hola.cloud/api/v0/lambdas/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 = ['code' => 'export default (req) => ({ body: { message: "Updated lambda", data: req.body } })', 'method' => 'POST', 'name' => 'hello-updated', 'path' => '/hello-updated'];
$body = json_encode($payload);

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://api.hola.cloud/api/v0/lambdas/YOUR_LAMBDA_ID',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'PATCH',
    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 = {"code": "export default (req) => ({ body: { message: \"Updated lambda\", data: req.body } })", "method": "POST", "name": "hello-updated", "path": "/hello-updated"}
body = json.dumps(payload)

response = requests.request(
    "PATCH",
    "https://api.hola.cloud/api/v0/lambdas/YOUR_LAMBDA_ID",
    headers=headers,
    data=body
)

print(response.text)
const payload = {"code": "export default (req) => ({ body: { message: \"Updated lambda\", data: req.body } })", "method": "POST", "name": "hello-updated", "path": "/hello-updated"};

const response = await fetch("https://api.hola.cloud/api/v0/lambdas/YOUR_LAMBDA_ID", {
  method: "PATCH",
  headers: {
    "X-Glue-Authentication": "YOUR_TOKEN",
    "Content-Type": "application/json"
  },
  body: JSON.stringify(payload)
});

console.log(await response.text());
const payload = {"code": "export default (req) => ({ body: { message: \"Updated lambda\", data: req.body } })", "method": "POST", "name": "hello-updated", "path": "/hello-updated"};

const response = await fetch("https://api.hola.cloud/api/v0/lambdas/YOUR_LAMBDA_ID", {
  method: "PATCH",
  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("code", "export default (req) => ({ body: { message: \"Updated lambda\", data: req.body } })", "method", "POST", "name", "hello-updated", "path", "/hello-updated");
        var body = new ObjectMapper().writeValueAsString(payload);

        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://api.hola.cloud/api/v0/lambdas/YOUR_LAMBDA_ID"))
            .method("PATCH", 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());
    }
}

预期响应:

 1{
 2  "id": "f3b2c1a0-1234-5678-9abc-def012345678",
 3  "created_timestamp": 1750507200,
 4  "owner": "user_123",
 5  "project_id": "project_456",
 6  "name": "hello-updated",
 7  "language": "javascript",
 8  "code": "export default (req) => ({ body: { message: \"Updated lambda\", data: req.body } })",
 9  "method": "POST",
10  "path": "/hello-updated"
11}

查看 Lambda 详情

按 ID 获取一个 lambda:

curl "https://api.hola.cloud/api/v0/lambdas/YOUR_LAMBDA_ID" \
  -H "X-Glue-Authentication: YOUR_TOKEN"
GET /api/v0/lambdas/YOUR_LAMBDA_ID 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/api/v0/lambdas/YOUR_LAMBDA_ID", 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/api/v0/lambdas/YOUR_LAMBDA_ID',
    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/api/v0/lambdas/YOUR_LAMBDA_ID",
    headers=headers
)

print(response.text)
const response = await fetch("https://api.hola.cloud/api/v0/lambdas/YOUR_LAMBDA_ID", {
  method: "GET",
  headers: {
    "X-Glue-Authentication": "YOUR_TOKEN"
  }
});

console.log(await response.text());
const response = await fetch("https://api.hola.cloud/api/v0/lambdas/YOUR_LAMBDA_ID", {
  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/api/v0/lambdas/YOUR_LAMBDA_ID"))
            .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());
    }
}

列出所有 Lambdas

curl "https://api.hola.cloud/api/v0/lambdas" \
  -H "X-Glue-Authentication: YOUR_TOKEN"
GET /api/v0/lambdas 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/api/v0/lambdas", 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/api/v0/lambdas',
    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/api/v0/lambdas",
    headers=headers
)

print(response.text)
const response = await fetch("https://api.hola.cloud/api/v0/lambdas", {
  method: "GET",
  headers: {
    "X-Glue-Authentication": "YOUR_TOKEN"
  }
});

console.log(await response.text());
const response = await fetch("https://api.hola.cloud/api/v0/lambdas", {
  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/api/v0/lambdas"))
            .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());
    }
}

删除 Lambda

永久删除一个 lambda:

curl -X DELETE "https://api.hola.cloud/api/v0/lambdas/YOUR_LAMBDA_ID" \
  -H "X-Glue-Authentication: YOUR_TOKEN"
DELETE /api/v0/lambdas/YOUR_LAMBDA_ID 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("DELETE", "https://api.hola.cloud/api/v0/lambdas/YOUR_LAMBDA_ID", 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/api/v0/lambdas/YOUR_LAMBDA_ID',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'DELETE',
    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(
    "DELETE",
    "https://api.hola.cloud/api/v0/lambdas/YOUR_LAMBDA_ID",
    headers=headers
)

print(response.text)
const response = await fetch("https://api.hola.cloud/api/v0/lambdas/YOUR_LAMBDA_ID", {
  method: "DELETE",
  headers: {
    "X-Glue-Authentication": "YOUR_TOKEN"
  }
});

console.log(await response.text());
const response = await fetch("https://api.hola.cloud/api/v0/lambdas/YOUR_LAMBDA_ID", {
  method: "DELETE",
  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/api/v0/lambdas/YOUR_LAMBDA_ID"))
            .method("DELETE", HttpRequest.BodyPublishers.noBody())
            .header("X-Glue-Authentication", "YOUR_TOKEN")
            .build();

        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}

删除成功后会返回已删除 lambda 的 API 响应或确认 payload,具体取决于已部署的 API 版本。

评论

发表评论