This repository has been archived by the owner on May 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Phone2ActionGETRequest.java
54 lines (46 loc) · 3.16 KB
/
Phone2ActionGETRequest.java
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
// We import some standard Java classes to make our HTTP Request
import java.net.*;
import java.io.*;
import java.util.Scanner;
// This is the Minimal JSON package, which we've added as a dependency
// It converts the JSON we get from the HTTP Request to a useable format
import com.eclipsesource.json.*;
// Documentation for this package here: https://github.com/ralfstx/minimal-json
import com.eclipsesource.json.JsonObject.Member;
public class Phone2ActionGETRequest
{
public static void main(String[] args) throws IOException
{
// Our Phone2Action API Key goes here
// Super important, we need this to show that we're authorized to make our request
String xAPIKey = ""; // Should not be an empty String, see the README for info on how to get an API Key
// First, we construct the URL we'll make our HTTP GET Request to:
String endpoint = "https://fmrrixuk32.execute-api.us-east-1.amazonaws.com/hacktj/legislators";
String parameters = "?level=NATIONAL_LOWER&address=";
String address = "6560 Braddock Rd"; // Change this to match your address!
String addressFormatted = URLEncoder.encode(address, "UTF-8"); // This formats the article title for our URL
URL phone2ActionURL = new URL(endpoint + parameters + addressFormatted); // This is the Java URL class, necessary here
// Then, we set up the HTTP GET Request itself
HttpURLConnection connection = (HttpURLConnection) phone2ActionURL.openConnection();
connection.setRequestMethod("GET"); // We need to set the HTTP Request type!
connection.setRequestProperty("X-API-Key", xAPIKey); // We pass in the API Key to access the Phone2Action API
// Now, we're able to fire the configured HTTP GET Request with the getInputStream() method
InputStream response = connection.getInputStream(); // The HttpURLConnection's response is an InputStream
// Next, we convert the response to a JsonObject with Minimal JSON
Scanner scanner = new Scanner(response);
String responseBody = scanner.useDelimiter("\\A").next(); // The response is a String, which isn't useful
JsonObject responseObject = Json.parse(responseBody).asObject(); // This converts it to a (useful) JsonObject
scanner.close();
// Finally, we pick out the data we want and print it to the console
// Use Postman to figure out what format your data is in and access it accordingly
// A sample JSON is also available at https://github.com/phone2action/hacktj-examples#legislators-api
JsonArray responseRepresentatives = responseObject.get("officials").asArray();
// We don't know what each of the keys of the response's pages will be, so we loop through all of them
for (JsonValue responseRepresentative : responseRepresentatives) // Each key is a Member object, provided by Minimal JSON
{
String firstName = responseRepresentative.asObject().get("first_name").asString();
String lastName = responseRepresentative.asObject().get("last_name").asString();
System.out.println("Your Representative in national Congress is " + firstName + " " + lastName); // We convert the key's value to a JsonObject
}
}
}