Created
October 25, 2013 06:14
-
-
Save KevinMoolana/7150130 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.pagerduty.functionApi; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
public class ApiClass { | |
/* | |
* getResponse will return lists the full JSON response for the 10 most | |
* recently created incidents, in descending date order. | |
*/ | |
public String getHTML() { | |
URL url; | |
HttpURLConnection conn; | |
BufferedReader rd; | |
String subDomain = "webdemo"; | |
String token = "VxuRAAxQoTgTjbo7wmmG"; | |
String query = ""; | |
String line; | |
String result = ""; | |
try { | |
query = "limit=10&offset=0&sort_by=created_on:desc"; | |
String urlString = "https://" + subDomain | |
+ ".pagerduty.com/api/v1/incidents" + "?" + query; | |
url = new URL(urlString); | |
conn = (HttpURLConnection) url.openConnection(); | |
conn.setRequestMethod("GET"); | |
conn.setRequestProperty("Content-type", "application/json"); | |
conn.setRequestProperty("Authorization", "Token token=" + token); | |
conn.setRequestProperty("Content-type", "application/json"); | |
rd = new BufferedReader( | |
new InputStreamReader(conn.getInputStream())); | |
while ((line = rd.readLine()) != null) { | |
result += line; | |
} | |
rd.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return result; | |
} | |
/* | |
* getResponse will return lists the created date, assigned user, and status | |
* for each of the 10 most recently created incidents, in descending date order. | |
*/ | |
public Response getResponse(String urlToRead) { | |
URL url; | |
HttpURLConnection conn; | |
String subDomain = "webdemo"; | |
String token = "VxuRAAxQoTgTjbo7wmmG"; | |
String query = ""; | |
Response response = null; | |
try { | |
query = "limit=10&offset=0&sort_by=created_on:desc"; | |
String urlString = "https://" + subDomain | |
+ ".pagerduty.com/api/v1/incidents" + "?" + query; | |
url = new URL(urlString); | |
conn = (HttpURLConnection) url.openConnection(); | |
conn.setRequestMethod("GET"); | |
conn.setRequestProperty("Content-type", "application/json"); | |
conn.setRequestProperty("Authorization", "Token token=" + token); | |
conn.setRequestProperty("Content-type", "application/json"); | |
InputStreamReader in = new InputStreamReader(conn.getInputStream()); | |
response = new Response(in); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return response; | |
} | |
public static void main(String args[]) { | |
ApiClass c = new ApiClass(); | |
// System.out.println(c.getResponse("")); | |
System.out.println(c.getHTML()); | |
} | |
} | |
/* | |
* separate class but under the same package | |
*/ | |
package org.pagerduty.functionApi; | |
import java.io.IOException; | |
import java.io.Reader; | |
import org.json.simple.JSONObject; | |
import org.json.simple.parser.JSONParser; | |
import org.json.simple.parser.ParseException; | |
public class Response { | |
private final String assignedUser; | |
private final String status; | |
private final String createdDate; | |
Response(Reader in) throws IOException, ParseException { | |
JSONObject msg = (JSONObject) new JSONParser().parse(in); | |
assignedUser = String.valueOf(msg.get("assigned_to_user")); | |
status = String.valueOf(msg.get("status")); | |
createdDate = (String) msg.get("created_on"); | |
} | |
public String getAssignedUser() { | |
return assignedUser; | |
} | |
public String getStatus() { | |
return status; | |
} | |
public String getCreatedDate() { | |
return createdDate; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment