Skip to content

Commit 73ef0b3

Browse files
authored
endpoint /businesses (#7)
* added new features for new endpoint; bump version * renamed method * version increased
1 parent 7b961a7 commit 73ef0b3

File tree

2 files changed

+125
-1
lines changed

2 files changed

+125
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'java'
22
apply plugin: 'maven-publish'
33

44
group = 'com.github.outscraper'
5-
version = '2.1.1'
5+
version = '2.1.3'
66

77
repositories {
88
mavenCentral()

src/main/java/OutscraperClient.java

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import java.io.IOException;
22
import java.net.URI;
3+
import java.net.URLEncoder;
34
import java.net.http.HttpClient;
45
import java.net.http.HttpRequest;
6+
import java.nio.charset.StandardCharsets;
57
import java.net.http.HttpResponse;
68
import java.util.ArrayList;
79
import java.util.List;
@@ -69,6 +71,31 @@ private JSONObject getAPIRequest(String path, HashMap<String, Object> parameters
6971
return null;
7072
}
7173

74+
public JSONObject postAPIRequest(String path, HashMap<String, Object> parameters) {
75+
String url = privateApiURL + path;
76+
77+
JSONObject jsonPayload = new JSONObject(parameters != null ? parameters : new HashMap<>());
78+
79+
HttpRequest request = HttpRequest.newBuilder()
80+
.uri(URI.create(url))
81+
.POST(HttpRequest.BodyPublishers.ofString(jsonPayload.toString()))
82+
.header("accept", "application/json")
83+
.header("content-type", "application/json")
84+
.header("X-API-KEY", privateApiKey)
85+
.header("client", "Java SDK")
86+
.build();
87+
88+
try {
89+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
90+
String body = response.body();
91+
return new JSONObject(body);
92+
} catch (IOException | InterruptedException | JSONException e) {
93+
e.printStackTrace();
94+
}
95+
96+
return null;
97+
}
98+
7299
public JSONObject getRequestArchive(String requestId) {
73100
return getAPIRequest("/requests/" + requestId, new HashMap<String, Object>() {{}});
74101
}
@@ -335,4 +362,101 @@ public JSONArray yellowpagesSearch(HashMap<String, Object> parameters) {
335362

336363
return getData(response);
337364
}
365+
366+
public JSONObject businessesSearch(HashMap<String, Object> parameters) {
367+
if (parameters == null) parameters = new HashMap<>();
368+
parameters.putIfAbsent("async", false);
369+
370+
JSONObject response = postAPIRequest("/businesses", parameters);
371+
if (response == null) return null;
372+
373+
Object data = response.opt("data");
374+
if (data instanceof JSONObject) {
375+
return (JSONObject) data;
376+
}
377+
if (data instanceof JSONArray) {
378+
return new JSONObject().put("items", (JSONArray) data).put("has_more", false).put("next_cursor", JSONObject.NULL);
379+
}
380+
return response;
381+
}
382+
383+
public JSONArray businessesIterSearch(HashMap<String, Object> parameters) {
384+
if (parameters == null) parameters = new HashMap<>();
385+
386+
JSONArray all = new JSONArray();
387+
Object cursorObj = parameters.get("cursor");
388+
String cursor = cursorObj != null ? String.valueOf(cursorObj) : null;
389+
390+
while (true) {
391+
if (cursor != null) {
392+
parameters.put("cursor", cursor);
393+
} else {
394+
parameters.remove("cursor");
395+
}
396+
397+
JSONObject page = businessesSearch(parameters);
398+
if (page == null) break;
399+
400+
JSONArray items = page.optJSONArray("items");
401+
if (items == null) {
402+
Object data = page.opt("data");
403+
if (data instanceof JSONArray) items = (JSONArray) data;
404+
}
405+
406+
if (items != null) {
407+
for (int i = 0; i < items.length(); i++) {
408+
all.put(items.get(i));
409+
}
410+
}
411+
412+
boolean hasMore = page.optBoolean("has_more", false);
413+
414+
String nextCursor = null;
415+
Object nextCursorObj = page.opt("next_cursor");
416+
if (nextCursorObj != null && nextCursorObj != JSONObject.NULL) {
417+
nextCursor = String.valueOf(nextCursorObj);
418+
if (nextCursor.isBlank()) nextCursor = null;
419+
}
420+
421+
if (!hasMore || nextCursor == null || items == null || items.length() == 0) {
422+
break;
423+
}
424+
425+
cursor = nextCursor;
426+
}
427+
428+
return all;
429+
}
430+
431+
public JSONObject businessesGet(String businessId, HashMap<String, Object> parameters) {
432+
if (businessId == null || businessId.isBlank()) {
433+
throw new IllegalArgumentException("businessId is required");
434+
}
435+
if (parameters == null) parameters = new HashMap<>();
436+
437+
Object fieldsObj = parameters.get("fields");
438+
if (fieldsObj instanceof List) {
439+
@SuppressWarnings("unchecked")
440+
List<Object> fields = (List<Object>) fieldsObj;
441+
List<String> strFields = new ArrayList<>();
442+
for (Object f : fields) {
443+
if (f != null) strFields.add(String.valueOf(f));
444+
}
445+
parameters.put("fields", String.join(",", strFields));
446+
}
447+
448+
String encodedId = URLEncoder.encode(String.valueOf(businessId), StandardCharsets.UTF_8);
449+
JSONObject response = getAPIRequest("/businesses/" + encodedId, parameters);
450+
if (response == null) return null;
451+
452+
Object data = response.opt("data");
453+
if (data instanceof JSONObject) return (JSONObject) data;
454+
if (data instanceof JSONArray) {
455+
JSONArray arr = (JSONArray) data;
456+
if (arr.length() > 0 && arr.get(0) instanceof JSONObject) {
457+
return arr.getJSONObject(0);
458+
}
459+
}
460+
return response;
461+
}
338462
}

0 commit comments

Comments
 (0)