-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paths3cr3txJava_protect_OpenAI_API_demo.java
More file actions
109 lines (106 loc) · 4.56 KB
/
s3cr3txJava_protect_OpenAI_API_demo.java
File metadata and controls
109 lines (106 loc) · 4.56 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
import java.io.*;
import java.net.*;
import javax.net.ssl.HttpsURLConnection;
public class s3cr3txJava_protect_OpenAI_API_demo {
/**
* @param strInput
* @return
*/
public static String getS3cr3tx(String strInput){
URL s3cr3tx_url;
try {
s3cr3tx_url = new URL(System.getenv("s3cr3tx_API_URL"));
HttpsURLConnection conn = (HttpsURLConnection)s3cr3tx_url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "text/plain");
conn.setRequestProperty("Email", System.getenv("s3cr3tx_Email"));
conn.setRequestProperty("APIToken", System.getenv("s3cr3tx_APIToken"));
conn.setRequestProperty("AuthCode", System.getenv("s3cr3tx_AuthCode"));
conn.setRequestProperty("Input", strInput);
conn.setRequestProperty("EorD", "d");
int responseCode = conn.getResponseCode();
System.out.println("\nResponse code: " + responseCode);
String strResult = new String();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
while ((line = br.readLine())!= null) {
strResult += line;
}
return strResult;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
}
public static String setS3cr3tx(String strInput){
try {
URL s3cr3tx_url = new URL(System.getenv("s3cr3tx_API_URL"));
HttpsURLConnection conn = (HttpsURLConnection) s3cr3tx_url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "text/plain");
conn.setRequestProperty("Email", System.getenv("s3cr3tx_Email"));
conn.setRequestProperty("APIToken", System.getenv("s3cr3tx_APIToken"));
conn.setRequestProperty("AuthCode", System.getenv("s3cr3tx_AuthCode"));
conn.setRequestProperty("Input", strInput);
conn.setRequestProperty("EorD", "e");
int responseCode = conn.getResponseCode();
System.out.println("\nResponse code: " + responseCode);
String strResult = new String();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
while ((line = br.readLine())!= null) {
strResult += line;
}
return strResult;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
}
public static void main(String[] args) throws Exception {
try {
URL openai_url = new URL("https://api.openai.com/v1/models");
HttpsURLConnection conn = (HttpsURLConnection) openai_url.openConnection();
conn.setRequestMethod("GET");
String strHeader = "Bearer " + System.getenv("OPENAI_API_KEY");
conn.setRequestProperty("Authorization",strHeader);
int responseCode = conn.getResponseCode();
System.out.println("\nSending 'GET' request to OpenAI: ");
System.out.println("\nResponse code: " + responseCode);
String strResult = new String();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
while ((line = br.readLine())!= null) {
strResult += line;
}
System.out.println("\n" + strResult);
System.setProperty("s3cr3tx_OPENAI_API_KEY", setS3cr3tx(System.getenv("OPENAI_API_KEY")));
URL openai_url_s3cr3tx = new URL("https://api.openai.com/v1/models");
HttpsURLConnection conn2 = (HttpsURLConnection) openai_url_s3cr3tx.openConnection();
conn2.setRequestMethod("GET");
conn2.setRequestProperty("Authorization", "Bearer " + getS3cr3tx(System.getProperty("s3cr3tx_OPENAI_API_KEY")));
int responseCode2 = conn.getResponseCode();
System.out.println("\nResponse code: " + responseCode2);
String strResult2 = new String();
BufferedReader br2 = new BufferedReader(new InputStreamReader(conn2.getInputStream()));
String lines = "";
while ((lines = br2.readLine())!= null) {
strResult2 += lines;
}
System.out.println("\n" + strResult2);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}