-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAPITest.java
More file actions
64 lines (54 loc) · 2.38 KB
/
Copy pathAPITest.java
File metadata and controls
64 lines (54 loc) · 2.38 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
import okhttp3.*;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import java.io.IOException;
public class APITest {
int remainingCards;
boolean shuffled;
private String deckID;
// Constructor method
public APITest(String deckID, int remainingCards, Boolean shuffled) {
this.deckID = deckID;
this.remainingCards = remainingCards;
this.shuffled = shuffled;
}
public static void main(String[] args) throws IOException {
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = RequestBody.create(mediaType, "");
Request request = new Request.Builder()
.url("https://www.deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1")
.build();
try {
Response response = client.newCall(request).execute();
ResponseBody responseBody = response.body();
// Converting responseBody into a string
if (responseBody != null) {
String responseBodyString = responseBody.string();
String[] newString = responseBodyString.split(",");
// Storing values from responseBody and adding and creating an example class as a test
String deckID = newString[1].split(":")[1].trim();
int remainingCards = Integer.parseInt(newString[2].split(":")[1].trim());
String tempShuffled = newString[3].split(":")[1].trim();
Boolean shuffled = Boolean.parseBoolean(tempShuffled.replace("}", ""));
APITest temp = new APITest(deckID, remainingCards, shuffled);
// Printing out values to check if properly added
System.out.println(temp.deckID);
System.out.println(temp.remainingCards);
System.out.println(temp.shuffled);
}
// Check if everything was successful
int statusCode = response.code();
if (statusCode == 200) {
System.out.println("Success!");
} else {
System.out.print("Failed");
}
} catch (IOException e) {
throw new IOException(e);
}
}
}