Manejando Funciones Lambda

Después de crear una lambda, usa la API de administración para inspeccionarla, actualizar sus campos soportados, listar todas las lambdas de la cuenta o eliminarla.

Estructura de la Función

Las lambdas JavaScript exportan un manejador por defecto. El manejador recibe un objeto de solicitud y devuelve el cuerpo de respuesta que HolaCloud debe enviar.

 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};

Las lambdas estáticas usan uno de los modos de lenguaje estático: static-html, static-css o static-js. En esos modos, code es el contenido servido para la lambda correspondiente.

Actualizar una Lambda

Usa PATCH /api/v0/lambdas/{lambda_id} para actualizar name, language, code, method o path.

curl -X PATCH "https://api.hola.cloud/api/v0/lambdas/TU_LAMBDA_ID" \
  -H "X-Glue-Authentication: TU_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/TU_LAMBDA_ID HTTP/1.1
Host: api.hola.cloud
X-Glue-Authentication: TU_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/TU_LAMBDA_ID", strings.NewReader(body))
	if err != nil {
		panic(err)
	}
	req.Header.Set("X-Glue-Authentication", "TU_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/TU_LAMBDA_ID',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'PATCH',
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_HTTPHEADER => [
        'X-Glue-Authentication: TU_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": "TU_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/TU_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/TU_LAMBDA_ID", {
  method: "PATCH",
  headers: {
    "X-Glue-Authentication": "TU_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/TU_LAMBDA_ID", {
  method: "PATCH",
  headers: {
    "X-Glue-Authentication": "TU_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/TU_LAMBDA_ID"))
            .method("PATCH", HttpRequest.BodyPublishers.ofString(body))
            .header("X-Glue-Authentication", "TU_TOKEN")
            .header("Content-Type", "application/json")
            .build();

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

Respuesta esperada:

 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}

Ver Detalles de una Lambda

Obtén una lambda por ID:

curl "https://api.hola.cloud/api/v0/lambdas/TU_LAMBDA_ID" \
  -H "X-Glue-Authentication: TU_TOKEN"
GET /api/v0/lambdas/TU_LAMBDA_ID HTTP/1.1
Host: api.hola.cloud
X-Glue-Authentication: TU_TOKEN
package main

import (
	"fmt"
	"io"
	"net/http"
)

func main() {
	req, err := http.NewRequest("GET", "https://api.hola.cloud/api/v0/lambdas/TU_LAMBDA_ID", nil)
	if err != nil {
		panic(err)
	}
	req.Header.Set("X-Glue-Authentication", "TU_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/TU_LAMBDA_ID',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_HTTPHEADER => [
        'X-Glue-Authentication: TU_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": "TU_TOKEN",
}

response = requests.request(
    "GET",
    "https://api.hola.cloud/api/v0/lambdas/TU_LAMBDA_ID",
    headers=headers
)

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

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

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

Listar Todas las Lambdas

curl "https://api.hola.cloud/api/v0/lambdas" \
  -H "X-Glue-Authentication: TU_TOKEN"
GET /api/v0/lambdas HTTP/1.1
Host: api.hola.cloud
X-Glue-Authentication: TU_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", "TU_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: TU_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": "TU_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": "TU_TOKEN"
  }
});

console.log(await response.text());
const response = await fetch("https://api.hola.cloud/api/v0/lambdas", {
  method: "GET",
  headers: {
    "X-Glue-Authentication": "TU_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", "TU_TOKEN")
            .build();

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

Eliminar una Lambda

Elimina una lambda permanentemente:

curl -X DELETE "https://api.hola.cloud/api/v0/lambdas/TU_LAMBDA_ID" \
  -H "X-Glue-Authentication: TU_TOKEN"
DELETE /api/v0/lambdas/TU_LAMBDA_ID HTTP/1.1
Host: api.hola.cloud
X-Glue-Authentication: TU_TOKEN
package main

import (
	"fmt"
	"io"
	"net/http"
)

func main() {
	req, err := http.NewRequest("DELETE", "https://api.hola.cloud/api/v0/lambdas/TU_LAMBDA_ID", nil)
	if err != nil {
		panic(err)
	}
	req.Header.Set("X-Glue-Authentication", "TU_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/TU_LAMBDA_ID',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'DELETE',
    CURLOPT_HTTPHEADER => [
        'X-Glue-Authentication: TU_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": "TU_TOKEN",
}

response = requests.request(
    "DELETE",
    "https://api.hola.cloud/api/v0/lambdas/TU_LAMBDA_ID",
    headers=headers
)

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

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

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

Una eliminación correcta devuelve la respuesta de la API para la lambda eliminada o una carga de confirmación, según la versión desplegada de la API.

Comentarios

Deja un comentario