|
1 | 1 | import java.io.IOException; |
2 | 2 | import java.net.URI; |
| 3 | +import java.net.URLEncoder; |
3 | 4 | import java.net.http.HttpClient; |
4 | 5 | import java.net.http.HttpRequest; |
| 6 | +import java.nio.charset.StandardCharsets; |
5 | 7 | import java.net.http.HttpResponse; |
6 | 8 | import java.util.ArrayList; |
7 | 9 | import java.util.List; |
@@ -69,6 +71,31 @@ private JSONObject getAPIRequest(String path, HashMap<String, Object> parameters |
69 | 71 | return null; |
70 | 72 | } |
71 | 73 |
|
| 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 | + |
72 | 99 | public JSONObject getRequestArchive(String requestId) { |
73 | 100 | return getAPIRequest("/requests/" + requestId, new HashMap<String, Object>() {{}}); |
74 | 101 | } |
@@ -335,4 +362,101 @@ public JSONArray yellowpagesSearch(HashMap<String, Object> parameters) { |
335 | 362 |
|
336 | 363 | return getData(response); |
337 | 364 | } |
| 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 | + } |
338 | 462 | } |
0 commit comments