-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSifterJava.java
126 lines (106 loc) · 4.32 KB
/
SifterJava.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
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
//package com.SifterJava;
/*
* SifterJava.java
*
* Copyright 2012 Mark Mikofski <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
import java.net.*;
import java.io.*;
import org.json.*;
/** SifterAPI Reader using org.json library **/
public class SifterJava {
public static void main (String[] args) throws Exception {
/** Enter your sifter account and access key in the
** string fields below */
// create URL object to SifterAPI
String yourAccount = "your-account"; // enter your account name
String yourDomain = yourAccount + ".sifterapp.com";
URL sifter = new URL("https",yourDomain,"/api/projects");
// open connectino to SifterAPI
URLConnection sifterConnection = sifter.openConnection();
// add Access Key to header request
String yourAccessKey = "your-32char-sifterapi-access-key"; // enter your access key
sifterConnection.setRequestProperty("X-Sifter-Token",
yourAccessKey);
// add Accept: application/json to header - also not necessary
sifterConnection.addRequestProperty("Accept","application/json");
// URLconnection.connect() not necessary
// getInputStream will connect
// don't make a content handler, org.json reads a stream!
// create buffer and open input stream
BufferedReader in = new BufferedReader(
new InputStreamReader(
sifterConnection.getInputStream()));
// don't readline, create stringbuilder, append, create string
// construct json tokener from input stream or buffered reader
JSONTokener x = new JSONTokener(in);
// initialize "projects" JSONObject from string
JSONObject projects = new JSONObject(x);
// prettyprint "projects"
System.out.println("************ projects ************");
System.out.println(projects.toString(2));
// array of projects
JSONArray array = projects.getJSONArray("projects");
int arrayLength = array.length();
JSONObject[] p = new JSONObject[arrayLength];
// projects
for (int i=0;i<arrayLength;i++) {
p[i] = array.getJSONObject(i);
System.out.println("************ project: " + (i+1) + " ************");
System.out.println(p[i].toString(2));
}
// check field names
String[] checkNames = {
"api_url",
"archived",
"api_issues_url",
"milestones_url",
"api_milestones_url",
"api_categories_url",
"issues_url",
"name",
"url",
"api_people_url",
"primary_company_name"};
// project field names
String[] fieldNames = JSONObject.getNames(p[0]);
int numKeys = p[0].length();
SifterProj[] proj = new SifterProj[arrayLength];
for (int j=0;j<arrayLength;j++) {
proj[j] = new SifterProj(p[j].get("api_url").toString(),
p[j].get("archived").toString(),
p[j].get("api_issues_url").toString(),
p[j].get("milestones_url").toString(),
p[j].get("api_milestones_url").toString(),
p[j].get("api_categories_url").toString(),
p[j].get("issues_url").toString(),
p[j].get("name").toString(),
p[j].get("url").toString(),
p[j].get("api_people_url").toString(),
p[j].get("primary_company_name").toString());
System.out.println("************ project: " + (j+1) + " ************");
for (int i=0;i<numKeys;i++) {
System.out.print(fieldNames[i]);
System.out.print(" : ");
System.out.println(p[j].get(fieldNames[i]));
}
}
}
}