aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/torui/coflsky/proxy/APIKeyManager.java
blob: e726552ede7f909bbcfb2d8e856846d5c7e9d199 (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
package de.torui.coflsky.proxy;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import net.minecraftforge.fml.common.Loader;

import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Collectors;

public class APIKeyManager {
    private final Gson gson = new Gson();
    private APIInfo apiInfo = new APIInfo();

    public APIInfo getApiInfo(){
        return this.apiInfo;
    }


    public class APIInfo{
        @SerializedName("api-key")
        public String key;
    }

    public void loadIfExists() throws Exception {
        Path dataPath = Paths.get(Loader.instance().getConfigDir().getPath(), "CoflSky", "api-key.json");
        File file = dataPath.toFile();
        if(file.exists()) {
            BufferedReader reader = new BufferedReader( new InputStreamReader(new FileInputStream(file)));
            String raw = reader.lines().collect(Collectors.joining("\n"));
            this.apiInfo = gson.fromJson(raw,APIInfo.class);
            reader.close();
        }
    }


    public void saveKey() throws Exception {
        Path dataPath = Paths.get(Loader.instance().getConfigDir().getPath(), "CoflSky", "api-key.json");
        File file = dataPath.toFile();
        if(file.exists()) {
            file.delete();
        }
        file.createNewFile();

        String data = gson.toJson(apiInfo);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
        bw.append(data);
        bw.flush();
        bw.close();
    }


}