1
Schedule a TaskProcessing AI task in Nextcloud Approval
One script reply has been approved by the moderators Verified

This must be added as an approval script

Created by marcel klehr12 140 days ago Viewed 731 times
0
Submitted by nextcloud Bun
Verified 13 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