Skip to content
This repository was archived by the owner on Jul 1, 2021. It is now read-only.

Commit 52d7050

Browse files
Added Licenses
1 parent dab39a4 commit 52d7050

File tree

11 files changed

+180
-193
lines changed

11 files changed

+180
-193
lines changed

src/io/github/TheBusyBiscuit/GitHubWebAPI4Java/objects/GitHubObject.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,26 @@ public Map<String, String> getParameters() {
183183
return null;
184184
}
185185

186+
187+
protected boolean getBoolean(String attribute, boolean full) throws IllegalAccessException {
188+
JsonElement element = getResponse(full);
189+
190+
if (element == null) {
191+
throw new IllegalAccessException("Could not connect to '" + getURL() + "'");
192+
}
193+
JsonObject response = element.getAsJsonObject();
194+
195+
return isInvalid(response, attribute) ? false: response.get(attribute).getAsBoolean();
196+
}
197+
198+
protected String getString(String attribute, boolean full) throws IllegalAccessException {
199+
JsonElement element = getResponse(full);
200+
201+
if (element == null) {
202+
throw new IllegalAccessException("Could not connect to '" + getURL() + "'");
203+
}
204+
JsonObject response = element.getAsJsonObject();
205+
206+
return isInvalid(response, attribute) ? null: response.get(attribute).getAsString();
207+
}
186208
}

src/io/github/TheBusyBiscuit/GitHubWebAPI4Java/objects/repositories/GitHubBlob.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,11 @@ public int getSize() throws IllegalAccessException {
4141

4242
@GitHubAccessPoint(path = "@content", type = String.class, requiresAccessToken = false)
4343
public String getFileContent() throws IllegalAccessException {
44-
JsonElement element = getResponse(true);
45-
46-
if (element == null) {
47-
throw new IllegalAccessException("Could not connect to '" + getURL() + "'");
48-
}
49-
JsonObject response = element.getAsJsonObject();
50-
51-
return isInvalid(response, "content") ? null: response.get("content").getAsString();
44+
return getString("content", true);
5245
}
5346

5447
@GitHubAccessPoint(path = "@encoding", type = String.class, requiresAccessToken = false)
5548
public String getEncoding() throws IllegalAccessException {
56-
JsonElement element = getResponse(true);
57-
58-
if (element == null) {
59-
throw new IllegalAccessException("Could not connect to '" + getURL() + "'");
60-
}
61-
JsonObject response = element.getAsJsonObject();
62-
63-
return isInvalid(response, "encoding") ? null: response.get("encoding").getAsString();
49+
return getString("encoding", true);
6450
}
6551
}

src/io/github/TheBusyBiscuit/GitHubWebAPI4Java/objects/repositories/GitHubBranch.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,7 @@ public String getRawURL() {
4646

4747
@GitHubAccessPoint(path = "@name", type = String.class, requiresAccessToken = false)
4848
public String getName() throws IllegalAccessException {
49-
JsonElement element = getResponse(false);
50-
51-
if (element == null) {
52-
throw new IllegalAccessException("Could not connect to '" + getURL() + "'");
53-
}
54-
JsonObject response = element.getAsJsonObject();
55-
56-
return isInvalid(response, "name") ? null: response.get("name").getAsString();
49+
return getString("name", false);
5750
}
5851

5952
@GitHubAccessPoint(path = "@commit", type = GitHubCommit.class, requiresAccessToken = false)

src/io/github/TheBusyBiscuit/GitHubWebAPI4Java/objects/repositories/GitHubComment.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,6 @@ public GitHubUser getUser() throws IllegalAccessException {
4444

4545
@GitHubAccessPoint(path = "@body", type = String.class, requiresAccessToken = false)
4646
public String getMessageBody() throws IllegalAccessException {
47-
JsonElement element = getResponse(false);
48-
49-
if (element == null) {
50-
throw new IllegalAccessException("Could not connect to '" + getURL() + "'");
51-
}
52-
JsonObject response = element.getAsJsonObject();
53-
54-
return isInvalid(response, "body") ? null: response.get("body").getAsString();
47+
return getString("body", false);
5548
}
5649
}

src/io/github/TheBusyBiscuit/GitHubWebAPI4Java/objects/repositories/GitHubIssue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public List<GitHubComment> getComments() throws IllegalAccessException {
156156
throw new IllegalAccessException("Could not connect to '" + getURL() + "'");
157157
}
158158

159-
List<GitHubComment> list = new ArrayList<GitHubComment>();
159+
List<GitHubComment> list = new ArrayList<>();
160160
JsonArray array = response.getAsJsonArray();
161161

162162
for (int i = 0; i < array.size(); i++) {

src/io/github/TheBusyBiscuit/GitHubWebAPI4Java/objects/repositories/GitHubLabel.java

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,38 +53,17 @@ public int getID() throws IllegalAccessException {
5353

5454
@GitHubAccessPoint(path = "@name", type = String.class, requiresAccessToken = false)
5555
public String getName() throws IllegalAccessException {
56-
JsonElement element = getResponse(false);
57-
58-
if (element == null) {
59-
throw new IllegalAccessException("Could not connect to '" + getURL() + "'");
60-
}
61-
JsonObject response = element.getAsJsonObject();
62-
63-
return isInvalid(response, "name") ? null: response.get("name").getAsString();
56+
return getString("name", false);
6457
}
6558

6659
@GitHubAccessPoint(path = "@color", type = String.class, requiresAccessToken = false)
6760
public String getColor() throws IllegalAccessException {
68-
JsonElement element = getResponse(false);
69-
70-
if (element == null) {
71-
throw new IllegalAccessException("Could not connect to '" + getURL() + "'");
72-
}
73-
JsonObject response = element.getAsJsonObject();
74-
75-
return isInvalid(response, "color") ? null: response.get("color").getAsString();
61+
return getString("color", false);
7662
}
7763

7864
@GitHubAccessPoint(path = "@default", type = Boolean.class, requiresAccessToken = false)
7965
public boolean isDefaultLabel() throws IllegalAccessException {
80-
JsonElement element = getResponse(false);
81-
82-
if (element == null) {
83-
throw new IllegalAccessException("Could not connect to '" + getURL() + "'");
84-
}
85-
JsonObject response = element.getAsJsonObject();
86-
87-
return isInvalid(response, "default") ? false: response.get("default").getAsBoolean();
66+
return getBoolean("default", false);
8867
}
8968

9069
public String getURLEncodedParameter() {
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package io.github.TheBusyBiscuit.GitHubWebAPI4Java.objects.repositories;
2+
3+
import java.io.UnsupportedEncodingException;
4+
import java.net.URLEncoder;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import com.google.gson.JsonArray;
9+
import com.google.gson.JsonElement;
10+
import com.google.gson.JsonObject;
11+
12+
import io.github.TheBusyBiscuit.GitHubWebAPI4Java.GitHubWebAPI;
13+
import io.github.TheBusyBiscuit.GitHubWebAPI4Java.annotations.GitHubAccessPoint;
14+
import io.github.TheBusyBiscuit.GitHubWebAPI4Java.objects.GitHubObject;
15+
16+
public class GitHubLicense extends GitHubObject {
17+
18+
private String param;
19+
20+
public GitHubLicense(GitHubWebAPI api, String key) throws UnsupportedEncodingException {
21+
super(api, null, "/licenses/" + URLEncoder.encode(key, "utf-8"));
22+
23+
this.param = URLEncoder.encode(key, "utf-8");
24+
}
25+
26+
public GitHubLicense(GitHubObject obj) {
27+
super(obj);
28+
}
29+
30+
@Override
31+
public String getRawURL() {
32+
return ".*licenses/.*";
33+
}
34+
35+
@GitHubAccessPoint(path = "@key", type = String.class, requiresAccessToken = false)
36+
public String getKey() throws IllegalAccessException {
37+
return getString("key", false);
38+
}
39+
40+
@GitHubAccessPoint(path = "@name", type = String.class, requiresAccessToken = false)
41+
public String getName() throws IllegalAccessException {
42+
return getString("name", false);
43+
}
44+
45+
@GitHubAccessPoint(path = "@spdx_id", type = String.class, requiresAccessToken = false)
46+
public String getSpdxID() throws IllegalAccessException {
47+
return getString("spdx_id", false);
48+
}
49+
50+
@GitHubAccessPoint(path = "@description", type = String.class, requiresAccessToken = false)
51+
public String getDescription() throws IllegalAccessException {
52+
return getString("description", true);
53+
}
54+
55+
@GitHubAccessPoint(path = "@implementation", type = String.class, requiresAccessToken = false)
56+
public String getImplementation() throws IllegalAccessException {
57+
return getString("implementation", true);
58+
}
59+
60+
@GitHubAccessPoint(path = "@body", type = String.class, requiresAccessToken = false)
61+
public String getFullBody() throws IllegalAccessException {
62+
return getString("body", true);
63+
}
64+
65+
@GitHubAccessPoint(path = "@featured", type= Boolean.class, requiresAccessToken = false)
66+
public boolean isFeatured() throws IllegalAccessException {
67+
return getBoolean("featured", true);
68+
}
69+
70+
public String getURLEncodedParameter() {
71+
return this.param;
72+
}
73+
74+
private List<String> getList(String path) throws IllegalAccessException {
75+
JsonElement response = getResponse(true);
76+
77+
if (response == null) {
78+
throw new IllegalAccessException("Could not connect to '" + getURL() + "'");
79+
}
80+
81+
List<String> list = new ArrayList<>();
82+
JsonArray array = response.getAsJsonArray();
83+
84+
for (int i = 0; i < array.size(); i++) {
85+
list.add(array.get(i).getAsString());
86+
}
87+
88+
return list;
89+
}
90+
91+
@GitHubAccessPoint(path= "@permissions", type = String.class, requiresAccessToken = false)
92+
public List<String> getPermissions() throws IllegalAccessException {
93+
return getList("permissions");
94+
}
95+
96+
@GitHubAccessPoint(path= "@conditions", type = String.class, requiresAccessToken = false)
97+
public List<String> getConditions() throws IllegalAccessException {
98+
return getList("conditions");
99+
}
100+
101+
@GitHubAccessPoint(path= "@limitations", type = String.class, requiresAccessToken = false)
102+
public List<String> getLimitations() throws IllegalAccessException {
103+
return getList("limitations");
104+
}
105+
106+
}

src/io/github/TheBusyBiscuit/GitHubWebAPI4Java/objects/repositories/GitHubMilestone.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,7 @@ public GitHubUser getCreator() throws IllegalAccessException {
6060

6161
@GitHubAccessPoint(path = "@description", type = String.class, requiresAccessToken = false)
6262
public String getDescription() throws IllegalAccessException {
63-
JsonElement element = getResponse(false);
64-
65-
if (element == null) {
66-
throw new IllegalAccessException("Could not connect to '" + getURL() + "'");
67-
}
68-
JsonObject response = element.getAsJsonObject();
69-
70-
return isInvalid(response, "description") ? null: response.get("description").getAsString();
63+
return getString("description", false);
7164
}
7265

7366
@GitHubAccessPoint(path = "/labels", type = GitHubLabel.class, requiresAccessToken = false)
@@ -79,7 +72,7 @@ public List<GitHubLabel> getLabels() throws IllegalAccessException, UnsupportedE
7972
throw new IllegalAccessException("Could not connect to '" + getURL() + "'");
8073
}
8174

82-
List<GitHubLabel> list = new ArrayList<GitHubLabel>();
75+
List<GitHubLabel> list = new ArrayList<>();
8376
JsonArray array = response.getAsJsonArray();
8477

8578
for (int i = 0; i < array.size(); i++) {

src/io/github/TheBusyBiscuit/GitHubWebAPI4Java/objects/repositories/GitHubPullRequest.java

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,7 @@ public GitHubUser getUser() throws IllegalAccessException {
6363

6464
@GitHubAccessPoint(path = "@locked", type = Boolean.class, requiresAccessToken = false)
6565
public boolean isLocked() throws IllegalAccessException {
66-
JsonElement element = getResponse(false);
67-
68-
if (element == null) {
69-
throw new IllegalAccessException("Could not connect to '" + getURL() + "'");
70-
}
71-
JsonObject response = element.getAsJsonObject();
72-
73-
return isInvalid(response, "locked") ? false: response.get("locked").getAsBoolean();
66+
return getBoolean("locked", false);
7467
}
7568

7669
@GitHubAccessPoint(path = "@base/label", type = String.class, requiresAccessToken = false)
@@ -322,38 +315,17 @@ public GitHubMilestone getMilestone() throws IllegalAccessException {
322315

323316
@GitHubAccessPoint(path = "@merged", type = Boolean.class, requiresAccessToken = false)
324317
public boolean isMerged() throws IllegalAccessException {
325-
JsonElement element = getResponse(true);
326-
327-
if (element == null) {
328-
throw new IllegalAccessException("Could not connect to '" + getURL() + "'");
329-
}
330-
JsonObject response = element.getAsJsonObject();
331-
332-
return isInvalid(response, "merged") ? false: response.get("merged").getAsBoolean();
318+
return getBoolean("merged", true);
333319
}
334320

335321
@GitHubAccessPoint(path = "@mergeable", type = Boolean.class, requiresAccessToken = false)
336322
public boolean isMergeable() throws IllegalAccessException {
337-
JsonElement element = getResponse(true);
338-
339-
if (element == null) {
340-
throw new IllegalAccessException("Could not connect to '" + getURL() + "'");
341-
}
342-
JsonObject response = element.getAsJsonObject();
343-
344-
return isInvalid(response, "mergeable") ? false: response.get("mergeable").getAsBoolean();
323+
return getBoolean("mergeable", true);
345324
}
346325

347326
@GitHubAccessPoint(path = "@body", type = String.class, requiresAccessToken = false)
348327
public String getMessageBody() throws IllegalAccessException {
349-
JsonElement element = getResponse(false);
350-
351-
if (element == null) {
352-
throw new IllegalAccessException("Could not connect to '" + getURL() + "'");
353-
}
354-
JsonObject response = element.getAsJsonObject();
355-
356-
return isInvalid(response, "body") ? null: response.get("body").getAsString();
328+
return getString("body", false);
357329
}
358330

359331
@GitHubAccessPoint(path = "@merged_at", type = Date.class, requiresAccessToken = false)
@@ -382,7 +354,7 @@ public List<GitHubUser> getAssignees() throws IllegalAccessException, Unsupporte
382354
}
383355
JsonObject response = element.getAsJsonObject();
384356

385-
List<GitHubUser> users = new ArrayList<GitHubUser>();
357+
List<GitHubUser> users = new ArrayList<>();
386358

387359
JsonArray array = response.get("assignees").getAsJsonArray();
388360

@@ -415,7 +387,7 @@ public List<GitHubComment> getComments() throws IllegalAccessException {
415387
throw new IllegalAccessException("Could not connect to '" + getURL() + "'");
416388
}
417389

418-
List<GitHubComment> list = new ArrayList<GitHubComment>();
390+
List<GitHubComment> list = new ArrayList<>();
419391
JsonArray array = response.getAsJsonArray();
420392

421393
for (int i = 0; i < array.size(); i++) {

0 commit comments

Comments
 (0)