summaryrefslogtreecommitdiff
path: root/pse-dashboard/src/api/gpodder.js
blob: 057e5a7dec9edaf1645865361ce6a3f770f2f998 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import axios from 'axios';

// export default function useGpodder({
export default function useGPodder({
        baseURL, 
        throwHandler = (err) => err, 
        gPodderUser, 
        useCredentials
    }) { 
    
    const gpodder = axios.create({
        baseURL,
        credentials: useCredentials ? "include" : "omit",
        headers: {
            'Content-Type': 'application/json',
        }
    });

    let auth = {
        username: gPodderUser?.username || "",
        password: gPodderUser?.password || ""
    };

    gpodder.interceptors.response.use((response) => response, (error) => {
        // whatever you want to do with the error
        throwHandler(error);
        throw error;
    });

    return {

        /******************************************************************************/
        /* Authentication API                                                         */
        /******************************************************************************/

        async register(gPodderUser) {
            return gpodder.post(`/api/2/auth/register.json`, gPodderUser);
        },

        async login(gPodderUserData) {
            auth = gPodderUserData;
            gPodderUser = gPodderUserData

            return gpodder.post(`/api/2/auth/${gPodderUser.username}/login.json`, {}, {auth});
        },

        async logout() {
            return gpodder.post(`/api/2/auth/${gPodderUser.username}/logout.json`, {}, {auth});
        },

        async changePassword(passwordChange) {
            return gpodder.put(`/api/2/auth/${gPodderUser.username}/changepassword.json`, passwordChange, {auth});
        },

        async forgotPassword({email}) {
            return gpodder.post(`/api/2/auth/${email}/forgot.json`, {});
        },

        // no auth!
        async resetPassword({username, password, token}) {
            return gpodder.put(`/api/2/auth/${username}/resetpassword.json?token=${token}`, {password});
        },

        async deleteAccount(passwordData) {
            return gpodder.delete(`/api/2/auth/${gPodderUser.username}/delete.json`, {auth, data: passwordData});
        },

        /******************************************************************************/
        /* Subscription API                                                           */
        /******************************************************************************/

        async getTitles() {
            return gpodder.get(`/subscriptions/titles/${gPodderUser.username}.json`, {auth});
        },

        async putSubscriptions(subscriptions) {
            return gpodder.put(`/subscriptions/${gPodderUser.username}/device.json`, subscriptions, {auth});
        },

        async postSubscriptions(subscriptions) {
            return gpodder.post(`/api/2/subscriptions/${gPodderUser.username}/device.json`, subscriptions, {auth});
        },

        /******************************************************************************/
        /* EpisodeActions API                                                         */
        /******************************************************************************/

        async getEpisodeActions() {
            return gpodder.get(`/api/2/episodes/${gPodderUser.username}.json`, {auth});
        },

        async postEpisodeActions(episodeActions) {
            return gpodder.post(`/api/2/episodes/${gPodderUser.username}.json`, episodeActions, {auth});
        },
    }
}