api.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { getBackendSrv } from '@grafana/runtime';
  2. import { notifyApp } from '../../core/actions';
  3. import { createErrorNotification, createSuccessNotification } from '../../core/copy/appNotification';
  4. import { dispatch } from '../../store/store';
  5. import { Playlist, PlaylistDTO } from './types';
  6. export async function createPlaylist(playlist: Playlist) {
  7. await withErrorHandling(() => getBackendSrv().post('/api/playlists', playlist));
  8. }
  9. export async function updatePlaylist(uid: string, playlist: Playlist) {
  10. await withErrorHandling(() => getBackendSrv().put(`/api/playlists/${uid}`, playlist));
  11. }
  12. export async function deletePlaylist(uid: string) {
  13. await withErrorHandling(() => getBackendSrv().delete(`/api/playlists/${uid}`), 'Playlist deleted');
  14. }
  15. export async function getPlaylist(uid: string): Promise<Playlist> {
  16. const result: Playlist = await getBackendSrv().get(`/api/playlists/${uid}`);
  17. return result;
  18. }
  19. export async function getAllPlaylist(query: string): Promise<PlaylistDTO[]> {
  20. const result: PlaylistDTO[] = await getBackendSrv().get('/api/playlists/', { query });
  21. return result;
  22. }
  23. async function withErrorHandling(apiCall: () => Promise<void>, message = 'Playlist saved') {
  24. try {
  25. await apiCall();
  26. dispatch(notifyApp(createSuccessNotification(message)));
  27. } catch (e) {
  28. dispatch(notifyApp(createErrorNotification('Unable to save playlist', e)));
  29. }
  30. }