Skip to content

Commit 2b18f34

Browse files
committed
Ver 4.1.1 - GitHubClient - HttpClient
1 parent e08e1cb commit 2b18f34

File tree

3 files changed

+61
-18
lines changed

3 files changed

+61
-18
lines changed

src/main/java/org/telosys/tools/commons/bundles/BundlesManager.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ public BundlesManager(TelosysToolsCfg cfg) {
5353
this.telosysToolsCfg = cfg;
5454
}
5555

56+
private GitHubClient getGitHubClient() {
57+
return new GitHubClient( telosysToolsCfg.getCfgFileAbsolutePath() ) ;
58+
}
59+
5660
//--------------------------------------------------------------------------------------------------
5761
/**
5862
* Returns the DOWNLOADS folder's full path in the file system <br>
@@ -162,7 +166,8 @@ public BundleStatus downloadAndInstallBundle( String userName, String bundleName
162166
*/
163167
public BundleStatus downloadBundle( String userName, String bundleName, String downloadFolderInProject ) {
164168
BundleStatus status = new BundleStatus();
165-
GitHubClient gitHubClient = new GitHubClient( telosysToolsCfg.getProperties() ) ;
169+
// GitHubClient gitHubClient = new GitHubClient( telosysToolsCfg.getProperties() ) ;
170+
GitHubClient gitHubClient = getGitHubClient(); // v 4.1.1
166171
String destinationFile = buildDestinationFileName(bundleName, downloadFolderInProject) ;
167172
status.log("-> Download bundle '" + bundleName + "' ");
168173
status.log(" in '" + destinationFile + "' ");
@@ -189,7 +194,8 @@ public BundleStatus downloadBundle( String userName, String bundleName, String d
189194
public BundlesFromGitHub getGitHubBundlesList( String githubUserName ) throws Exception {
190195

191196
// HTTP request to GitHub
192-
GitHubClient gitHubClient = new GitHubClient( telosysToolsCfg.getProperties() ) ;
197+
// GitHubClient gitHubClient = new GitHubClient( telosysToolsCfg.getProperties() ) ;
198+
GitHubClient gitHubClient = getGitHubClient(); // v 4.1.1
193199
GitHubRepositoriesResponse githubResponse = gitHubClient.getRepositories(githubUserName);
194200

195201
// Build list of bundles names (can be void)
@@ -210,7 +216,8 @@ public BundlesFromGitHub getGitHubBundlesList( String githubUserName ) throws Ex
210216
public GitHubRateLimitResponse getGitHubRateLimit() throws Exception {
211217

212218
// HTTP request to GitHub
213-
GitHubClient gitHubClient = new GitHubClient( telosysToolsCfg.getProperties() ) ;
219+
// GitHubClient gitHubClient = new GitHubClient( telosysToolsCfg.getProperties() ) ;
220+
GitHubClient gitHubClient = getGitHubClient(); // v 4.1.1
214221
return gitHubClient.getRateLimit();
215222
}
216223

src/main/java/org/telosys/tools/commons/github/GitHubClient.java

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.json.simple.JSONObject;
2626
import org.json.simple.parser.JSONParser;
2727
import org.json.simple.parser.ParseException;
28+
import org.telosys.tools.commons.PropertiesManager;
29+
import org.telosys.tools.commons.cfg.TelosysToolsCfg;
2830
import org.telosys.tools.commons.http.Base64;
2931
import org.telosys.tools.commons.http.HttpClient;
3032
import org.telosys.tools.commons.http.HttpResponse;
@@ -37,22 +39,45 @@
3739
*/
3840
public class GitHubClient {
3941

40-
public static final String VERSION = "2.1" ; // GitHub Client Version (for checking in CLI and Download)
42+
// public static final String VERSION = "2.1" ; // GitHub Client Version (for checking in CLI and Download)
43+
public static final String VERSION = "3 (2024-01-05)" ; // GitHub Client Version (for checking in CLI and Download)
4144

4245
public static final String GIT_HUB_HOST_URL = "https://api.github.com" ;
4346

4447
public static final String GIT_HUB_REPO_URL_PATTERN = "https://github.com/${USER}/${REPO}/archive/master.zip" ;
4548

4649

47-
private final Properties proxyProperties ;
50+
// private final Properties proxyProperties ;
51+
private final String propertiesFileAbsolutePath ;
4852

49-
/**
50-
* Constructor
51-
* @param proxyProperties
52-
*/
53-
public GitHubClient(Properties proxyProperties) {
53+
// /**
54+
// * Constructor
55+
// * @param proxyProperties
56+
// */
57+
// public GitHubClient(Properties proxyProperties) {
58+
// super();
59+
// this.proxyProperties = proxyProperties;
60+
// }
61+
public GitHubClient(String propertiesFileAbsolutePath) {
5462
super();
55-
this.proxyProperties = proxyProperties;
63+
if ( propertiesFileAbsolutePath == null ) {
64+
throw new IllegalArgumentException("File path argument is mandatory");
65+
}
66+
this.propertiesFileAbsolutePath = propertiesFileAbsolutePath;
67+
}
68+
69+
private HttpClient buildHttpClient() {
70+
// TODO : load properties
71+
PropertiesManager propertiesManager = new PropertiesManager(propertiesFileAbsolutePath) ;
72+
Properties properties = propertiesManager.load(); // Ret NULL if file not found
73+
if ( properties != null ) {
74+
return new HttpClient(properties);
75+
}
76+
else {
77+
// Properties file not found, no properties loaded : use default values
78+
return new HttpClient();
79+
}
80+
5681
}
5782

5883
private Map<String, String> buildRequestHeaders() {
@@ -71,7 +96,8 @@ private Map<String, String> buildRequestHeaders() {
7196
}
7297

7398
private HttpResponse httpGet( String url ) throws Exception {
74-
HttpClient httpClient = new HttpClient(proxyProperties);
99+
// HttpClient httpClient = new HttpClient(proxyProperties);
100+
HttpClient httpClient = buildHttpClient();
75101
HttpResponse response;
76102
try {
77103
// Sometimes GitHub return a 403 status code
@@ -206,7 +232,9 @@ public final long downloadRepository(String userName, String repoName, String de
206232
String url = GitHubUtil.buildGitHubURL(userName, repoName, GIT_HUB_REPO_URL_PATTERN);
207233

208234
long bytesCount = 0 ;
209-
HttpClient httpClient = new HttpClient(proxyProperties);
235+
// HttpClient httpClient = new HttpClient(proxyProperties);
236+
HttpClient httpClient = buildHttpClient();
237+
210238
bytesCount = httpClient.downloadFile(url, destinationFile);
211239
return bytesCount ;
212240
}

src/test/java/org/telosys/tools/commons/github/GitHubClientIT.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,16 @@ public void testGetRepositoriesWithoutProperties() throws Exception {
5353
getRepositories( null );
5454
}
5555

56+
private GitHubClient buildGitHubClient() {
57+
return new GitHubClient( TestsEnv.getTestFile("cfg/telosys-tools.cfg").getAbsolutePath() ); // v 4.1.1
58+
}
59+
5660
private void getRepositories( Properties properties ) throws Exception {
5761
System.out.println("Getting repositories... ");
5862
printJavaVersion() ;
5963

60-
GitHubClient gitHubClient = new GitHubClient( properties );
64+
// GitHubClient gitHubClient = new GitHubClient( properties );
65+
GitHubClient gitHubClient = buildGitHubClient(); // v 4.1.1
6166

6267
GitHubRepositoriesResponse githubResponse = gitHubClient.getRepositories(GITHUB_USER);
6368

@@ -76,8 +81,9 @@ public void testDownloadRepository() throws Exception {
7681

7782
printJavaVersion() ;
7883

79-
Properties properties = TestsEnv.loadSpecificProxyProperties() ;
80-
GitHubClient gitHubClient = new GitHubClient( properties);
84+
// Properties properties = TestsEnv.loadSpecificProxyProperties() ;
85+
// GitHubClient gitHubClient = new GitHubClient( properties);
86+
GitHubClient gitHubClient = buildGitHubClient(); // v 4.1.1
8187

8288
String repoName = "plantuml" ; // "basic-templates-TT210" ;
8389
String destinationFile = TestsEnv.getTmpDownloadFolderFullPath() + "/" + repoName + ".zip" ;
@@ -92,7 +98,8 @@ public void testGetRateLimit() throws Exception {
9298

9399
GitHubUser.clear();
94100

95-
GitHubClient gitHubClient = new GitHubClient(null);
101+
// GitHubClient gitHubClient = new GitHubClient(null);
102+
GitHubClient gitHubClient = buildGitHubClient(); // v 4.1.1
96103
GitHubRateLimitResponse rateLimit = gitHubClient.getRateLimit();
97104

98105
System.out.println(rateLimit);
@@ -106,7 +113,8 @@ public void testGetRateLimitWithBadUser() throws Exception {
106113
// User + password
107114
GitHubUser.set("fake-user-azer7766-OuPMK", "xxxxx");
108115

109-
GitHubClient gitHubClient = new GitHubClient(null);
116+
// GitHubClient gitHubClient = new GitHubClient(null);
117+
GitHubClient gitHubClient = buildGitHubClient(); // v 4.1.1
110118
GitHubRateLimitResponse rateLimit = gitHubClient.getRateLimit();
111119

112120
System.out.println(rateLimit); // limit='60', remaining='60'

0 commit comments

Comments
 (0)