1

Schedule a TaskProcessing AI task in Nextcloud

by
Published Sep 5, 2024

This must be added as an approval script

Script· approval nextcloud Verified

The script

Submitted by nextcloud Bun
Verified 511 days ago
1
// This must be an approval script!
2

3
import * as wmill from "windmill-client";
4
import createClient, { type Middleware } from "openapi-fetch";
5

6
type Nextcloud = {
7
  baseUrl: string,
8
  password: string,
9
  username: string
10
};
11

12
export async function main(
13
  ncResource: Nextcloud,
14
  userId: string | null = null,
15
  taskType: string,
16
  input: object,
17
  useAppApiAuth: boolean = false,
18
) {
19

20
  const client = createClient<paths>({ baseUrl: ncResource.baseUrl });
21
  const authMiddleware: Middleware = {
22
    async onRequest({ request, options }) {
23
      // fetch token, if it doesn’t exist
24
      // add Authorization header to every request
25
      request.headers.set("Authorization", `Basic ${btoa((userId || ncResource.username) + ':' + ncResource.password)}`);
26
      if (useAppApiAuth) {
27
        request.headers.set("AA-VERSION", "2.3.0",);
28
        request.headers.set("EX-APP-ID", "flow",);
29
        request.headers.set("EX-APP-VERSION", "1.0.1",);
30
        request.headers.set("AUTHORIZATION-APP-API", btoa(
31
          `${userId || ncResource.username}:${ncResource.password}`,
32
        ));
33
      }
34
      return request;
35
    },
36
  };
37
  client.use(authMiddleware);
38

39
  const resumeUrls = await wmill.getResumeUrls()
40

41
  const res = await client.POST("/ocs/v2.php/taskprocessing/schedule", {
42
    params: {
43
      header: {
44
        "OCS-APIRequest": true,
45
      },
46
      query: {
47
        format: "json",
48
      },
49

50
    },
51
    body: {
52
      type: taskType,
53
      input: input,
54
      appId: 'windmill',
55
      webhookUri: resumeUrls.resume,
56
      webhookMethod: 'HTTP:POST',
57
    }
58
  });
59
  return {
60
    urls: resumeUrls,
61
    task: await res.data,
62
  }
63
}
Other submissions
  • Submitted by marcel klehr12 Deno
    Created 643 days ago
    1
    import * as wmill from "npm:windmill-client@1";
    2
    import * as nc from "https://raw.githubusercontent.com/marcelklehr/nextcloud-client-deno/c57692db56e9292b252c9b8aca7516753881409c/core/index.ts";
    3
    
    
    4
    // fill the type, or use the +Resource type to get a type-safe reference to a resource
    5
    // type Postgresql = object
    6
    
    
    7
    export async function main(
    8
      nextcloudResource: string,
    9
      userId: string | null = null,
    10
      taskType: string,
    11
      input: object,
    12
      useAppApiAuth: boolean = false,
    13
    ) {
    14
      const ncResource = await wmill.getResource(
    15
        nextcloudResource,
    16
      );
    17
      const config = new nc.Configuration({
    18
        username: userId || ncResource.username,
    19
        password: ncResource.password,
    20
        basePath: ncResource.baseUrl,
    21
        middleware: [{
    22
          async pre(context) {
    23
            if (!context.url.includes("?")) {
    24
              context.url += "?";
    25
            } else {
    26
              context.url += "&";
    27
            }
    28
            context.url += "format=json";
    29
            return context;
    30
          },
    31
        }],
    32
        ...(useAppApiAuth && ({
    33
          headers: {
    34
            "AA-VERSION": "2.3.0",
    35
            "EX-APP-ID": "windmill_app",
    36
            "EX-APP-VERSION": "1.0.0",
    37
            "AUTHORIZATION-APP-API": btoa(
    38
              `${userId || ncResource.username}:${ncResource.password}`,
    39
            ),
    40
          },
    41
        })),
    42
      });
    43
      const api = new nc.TaskProcessingApiApi(config);
    44
    
    
    45
      const resumeUrls = await wmill.getResumeUrls()
    46
    
    
    47
      const res = await api.taskProcessingApiScheduleRaw({
    48
        taskProcessingApiScheduleRequest: {
    49
          type: taskType,
    50
          input,
    51
          appId: 'windmill',
    52
          webhookUri: resumeUrls.resume,
    53
          webhookMethod: 'HTTP:POST',
    54
        },
    55
        oCSAPIRequest: true,
    56
      });
    57
      return {
    58
        urls: resumeUrls,
    59
        task: await res.raw.json()
    60
      }
    61
    }
    62
    
    
    63
    
    
  • Submitted by nextcloud Bun
    Created 103 days ago
    This script is a newer edit of the script that had been approved, but it needs to be re-approved
    1
    // This must be an approval script!
    2
    
    
    3
    import * as wmill from "windmill-client";
    4
    import createClient, { type Middleware } from "openapi-fetch";
    5
    
    
    6
    export async function main(
    7
      nextcloud: RT.Nextcloud,
    8
      taskType: string,
    9
      input: object,
    10
    ) {
    11
    
    
    12
      const client = createClient<paths>({ baseUrl: nextcloud.baseUrl });
    13
      const authMiddleware: Middleware = {
    14
        async onRequest({ request, options }) {
    15
          // fetch token, if it doesn’t exist
    16
          // add Authorization header to every request
    17
          request.headers.set("Authorization", `Basic ${btoa((nextcloud.userId) + ':' + nextcloud.token)}`);
    18
          return request;
    19
        },
    20
      };
    21
      client.use(authMiddleware);
    22
    
    
    23
      const resumeUrls = await wmill.getResumeUrls()
    24
    
    
    25
      const res = await client.POST("/ocs/v2.php/taskprocessing/schedule", {
    26
        params: {
    27
          header: {
    28
            "OCS-APIRequest": true,
    29
          },
    30
          query: {
    31
            format: "json",
    32
          },
    33
    
    
    34
        },
    35
        body: {
    36
          type: taskType,
    37
          input: input,
    38
          appId: 'windmill',
    39
          webhookUri: resumeUrls.resume,
    40
          webhookMethod: 'HTTP:POST',
    41
        }
    42
      });
    43
      return {
    44
        urls: resumeUrls,
    45
        task: await res.data,
    46
      }
    47
    }