summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/api/alert_management_alerts_api.js
blob: fa66ca5b3dd7533aa0a41f78b82eafb489649b66 (plain)
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
import axios from '~/lib/utils/axios_utils';
import { buildApiUrl } from '~/api/api_utils';
import { ContentTypeMultipartFormData } from '~/lib/utils/headers';

const ALERT_METRIC_IMAGES_PATH =
  '/api/:version/projects/:id/alert_management_alerts/:alert_iid/metric_images';
const ALERT_SINGLE_METRIC_IMAGE_PATH =
  '/api/:version/projects/:id/alert_management_alerts/:alert_iid/metric_images/:image_id';

export function fetchAlertMetricImages({ alertIid, id }) {
  const metricImagesUrl = buildApiUrl(ALERT_METRIC_IMAGES_PATH)
    .replace(':id', encodeURIComponent(id))
    .replace(':alert_iid', encodeURIComponent(alertIid));

  return axios.get(metricImagesUrl);
}

export function uploadAlertMetricImage({ alertIid, id, file, url = null, urlText = null }) {
  const options = { headers: { ...ContentTypeMultipartFormData } };
  const metricImagesUrl = buildApiUrl(ALERT_METRIC_IMAGES_PATH)
    .replace(':id', encodeURIComponent(id))
    .replace(':alert_iid', encodeURIComponent(alertIid));

  // Construct multipart form data
  const formData = new FormData();
  formData.append('file', file);
  if (url) {
    formData.append('url', url);
  }
  if (urlText) {
    formData.append('url_text', urlText);
  }

  return axios.post(metricImagesUrl, formData, options);
}

export function updateAlertMetricImage({ alertIid, id, imageId, url = null, urlText = null }) {
  const metricImagesUrl = buildApiUrl(ALERT_SINGLE_METRIC_IMAGE_PATH)
    .replace(':id', encodeURIComponent(id))
    .replace(':alert_iid', encodeURIComponent(alertIid))
    .replace(':image_id', encodeURIComponent(imageId));

  // Construct multipart form data
  const formData = new FormData();
  if (url != null) {
    formData.append('url', url);
  }
  if (urlText != null) {
    formData.append('url_text', urlText);
  }

  return axios.put(metricImagesUrl, formData);
}

export function deleteAlertMetricImage({ alertIid, id, imageId }) {
  const individualMetricImageUrl = buildApiUrl(ALERT_SINGLE_METRIC_IMAGE_PATH)
    .replace(':id', encodeURIComponent(id))
    .replace(':alert_iid', encodeURIComponent(alertIid))
    .replace(':image_id', encodeURIComponent(imageId));

  return axios.delete(individualMetricImageUrl);
}