aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/server/redundancy.ts
blob: 3aca4ebfdc55b5b1ffd13da039fb1c35a83a0158 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { makeDeleteRequest, makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests'
import { VideoRedundanciesTarget } from '@shared/models'
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'

function updateRedundancy (url: string, accessToken: string, host: string, redundancyAllowed: boolean, expectedStatus = 204) {
  const path = '/api/v1/server/redundancy/' + host

  return makePutBodyRequest({
    url,
    path,
    token: accessToken,
    fields: { redundancyAllowed },
    statusCodeExpected: expectedStatus
  })
}

function listVideoRedundancies (options: {
  url: string
  accessToken: string
  target: VideoRedundanciesTarget
  start?: number
  count?: number
  sort?: string
  statusCodeExpected?: HttpStatusCode
}) {
  const path = '/api/v1/server/redundancy/videos'

  const { url, accessToken, target, statusCodeExpected, start, count, sort } = options

  return makeGetRequest({
    url,
    token: accessToken,
    path,
    query: {
      start: start ?? 0,
      count: count ?? 5,
      sort: sort ?? 'name',
      target
    },
    statusCodeExpected: statusCodeExpected || HttpStatusCode.OK_200
  })
}

function addVideoRedundancy (options: {
  url: string
  accessToken: string
  videoId: number
}) {
  const path = '/api/v1/server/redundancy/videos'
  const { url, accessToken, videoId } = options

  return makePostBodyRequest({
    url,
    token: accessToken,
    path,
    fields: { videoId },
    statusCodeExpected: HttpStatusCode.NO_CONTENT_204
  })
}

function removeVideoRedundancy (options: {
  url: string
  accessToken: string
  redundancyId: number
}) {
  const { url, accessToken, redundancyId } = options
  const path = '/api/v1/server/redundancy/videos/' + redundancyId

  return makeDeleteRequest({
    url,
    token: accessToken,
    path,
    statusCodeExpected: HttpStatusCode.NO_CONTENT_204
  })
}

export {
  updateRedundancy,
  listVideoRedundancies,
  addVideoRedundancy,
  removeVideoRedundancy
}