-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTempMailSo.java
More file actions
165 lines (146 loc) · 5.4 KB
/
Copy pathTempMailSo.java
File metadata and controls
165 lines (146 loc) · 5.4 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* TempMailSo SDK
* A Java SDK for the TempMailSo API (https://tempmail.so)
*
* This SDK provides methods to:
* - List available domains
* - Create temporary inboxes
* - List all inboxes
* - Delete inboxes
* - List emails in an inbox
* - Retrieve email details
* - Delete emails
*
* @author TempMailSo SDK Team
* @version 1.0.0
*/
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.Map;
public class TempMailSo {
private final String apiKey;
private final String authToken;
private final String baseUrl = "https://tempmail-so.p.rapidapi.com";
private final HttpClient client;
/**
* Initialize TempMailSo client
* @param apiKey RapidAPI key
* @param authToken TempMailSo authorization token
*/
public TempMailSo(String apiKey, String authToken) {
this.apiKey = apiKey;
this.authToken = authToken;
this.client = HttpClient.newHttpClient();
}
/**
* List all available domains
* @return JSON response containing available domains
*/
public String listDomains() throws IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl + "/domains"))
.header("x-rapidapi-key", apiKey)
.header("Authorization", "Bearer " + authToken)
.GET()
.build();
return sendRequest(request);
}
/**
* Create a new temporary inbox
* @param address Custom email prefix
* @param domain Selected domain
* @param lifespan Lifespan in seconds (0, 300, 600, 900, 1200, 1800)
* @return JSON response containing inbox details
*/
public String createInbox(String address, String domain, int lifespan) throws IOException, InterruptedException {
String formData = String.format("name=%s&domain=%s&lifespan=%d", address, domain, lifespan);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl + "/inboxes"))
.header("x-rapidapi-key", apiKey)
.header("Authorization", "Bearer " + authToken)
.header("Content-Type", "application/x-www-form-urlencoded")
.POST(HttpRequest.BodyPublishers.ofString(formData))
.build();
return sendRequest(request);
}
/**
* List all inboxes
* @return JSON response containing all inboxes
*/
public String listInboxes() throws IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl + "/inboxes"))
.header("x-rapidapi-key", apiKey)
.header("Authorization", "Bearer " + authToken)
.GET()
.build();
return sendRequest(request);
}
/**
* Delete an inbox
* @param inboxId ID of the inbox to delete
* @return JSON response confirming deletion
*/
public String deleteInbox(String inboxId) throws IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl + "/inboxes/" + inboxId))
.header("x-rapidapi-key", apiKey)
.header("Authorization", "Bearer " + authToken)
.DELETE()
.build();
return sendRequest(request);
}
/**
* List all emails in an inbox
* @param inboxId ID of the inbox
* @return JSON response containing emails
*/
public String listEmails(String inboxId) throws IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl + "/inboxes/" + inboxId + "/mails"))
.header("x-rapidapi-key", apiKey)
.header("Authorization", "Bearer " + authToken)
.GET()
.build();
return sendRequest(request);
}
/**
* Get details of a specific email
* @param inboxId ID of the inbox
* @param emailId ID of the email
* @return JSON response containing email details
*/
public String getEmail(String inboxId, String emailId) throws IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl + "/inboxes/" + inboxId + "/mails/" + emailId))
.header("x-rapidapi-key", apiKey)
.header("Authorization", "Bearer " + authToken)
.GET()
.build();
return sendRequest(request);
}
/**
* Delete a specific email
* @param inboxId ID of the inbox
* @param emailId ID of the email to delete
* @return JSON response confirming deletion
*/
public String deleteEmail(String inboxId, String emailId) throws IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl + "/inboxes/" + inboxId + "/mails/" + emailId))
.header("x-rapidapi-key", apiKey)
.header("Authorization", "Bearer " + authToken)
.DELETE()
.build();
return sendRequest(request);
}
// Helper method to send HTTP requests
private String sendRequest(HttpRequest request) throws IOException, InterruptedException {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.body();
}
}