From e06efa90185175c0128c98ed33751e563c1507a8 Mon Sep 17 00:00:00 2001 From: dimnorin Date: Mon, 28 Nov 2016 23:52:56 +0200 Subject: [PATCH] Added java wrapper examples --- source/includes/_auth.md | 4 + source/includes/_batch.md | 115 +++++++++++++++++++++++++++ source/includes/_offsite-seo.md | 116 +++++++++++++++++++++++++++ source/includes/_rankings.md | 137 ++++++++++++++++++++++++++++++++ source/includes/_reviews.md | 120 ++++++++++++++++++++++++++++ source/index.md | 1 + 6 files changed, 493 insertions(+) diff --git a/source/includes/_auth.md b/source/includes/_auth.md index 6f23435d1fa..5d2d67f04b7 100644 --- a/source/includes/_auth.md +++ b/source/includes/_auth.md @@ -14,6 +14,10 @@ echo urlencode($sig); // for get requests echo $sig; ``` +```java +ApiContext context = new ApiContext(API_KEY, API_SECRET); +``` + ```shell expiry=$((`date +%s`+1800)) # not more than 1800 seconds sig=$(echo -n "$expiry" | openssl dgst -sha1 -binary -hmac "" | base64) diff --git a/source/includes/_batch.md b/source/includes/_batch.md index 93d4770aa2c..2b2530a695c 100644 --- a/source/includes/_batch.md +++ b/source/includes/_batch.md @@ -26,6 +26,26 @@ if ($result['success']) { } ``` +```java +import com.brightlocal.sdk.ApiContext; +import com.brightlocal.sdk.api.ApiBatch; +import com.brightlocal.sdk.api.Batch; +import com.brightlocal.sdk.exception.ApiException; + +ApiContext context = new ApiContext(API_KEY, API_SECRET); +// Initialize sdk +ApiBatch apiBatch = new ApiBatch(context); +try { + // Create a new batch + Batch batch = apiBatch.create(); + if (batch.isSuccess()) { + int batchId = batch.getId(); + } +} catch (ApiException e) { + e.printStackTrace(); +} +``` + ```shell curl -X POST \ -d 'api-key=' \ @@ -100,6 +120,29 @@ if ($batchApi->commit($batchId)) { } ``` +```java +import com.brightlocal.sdk.ApiContext; +import com.brightlocal.sdk.api.ApiBatch; +import com.brightlocal.sdk.api.Batch; +import com.brightlocal.sdk.exception.ApiException; + +ApiContext context = new ApiContext(API_KEY, API_SECRET); +// Initialize sdk +ApiBatch apiBatch = new ApiBatch(context); +try { + // Create a new batch + Batch batch = apiBatch.create(); + if (batch.isSuccess()) { + batch = apiBatch.commit(batch); + if (batch.isSuccess()) { + context.log("Committed batch successfully."); + } + } +} catch (ApiException e) { + e.printStackTrace(); +} +``` + ```shell curl -X PUT \ -d 'api-key=' \ @@ -163,6 +206,32 @@ $batchApi = new BatchApi($api); print_r($batchApi->get_results($batchId)); ``` +```java +import com.brightlocal.sdk.ApiContext; +import com.brightlocal.sdk.api.ApiBatch; +import com.brightlocal.sdk.api.Batch; +import com.brightlocal.sdk.api.BatchResults; +import com.brightlocal.sdk.exception.ApiException; + +ApiContext context = new ApiContext(API_KEY, API_SECRET); +// Initialize sdk +ApiBatch apiBatch = new ApiBatch(context); + +try { + // Create a new batch + Batch batch = apiBatch.create(); + if (batch.isSuccess()) { + // If batch was successfully created, request batch results + BatchResults batchResults = apiBatch.getResults(batch); + if (batchResults.isSuccess()) { + // process batch results here + } + } +} catch (ApiException e) { + e.printStackTrace(); +} +``` + ```shell curl 'https://tools.brightlocal.com/seo-tools/api/v4/batch?api-key=&batch-id=' ``` @@ -252,6 +321,29 @@ if ($batchApi->delete($batchId)) { } ``` +```java +import com.brightlocal.sdk.ApiContext; +import com.brightlocal.sdk.api.ApiBatch; +import com.brightlocal.sdk.api.Batch; +import com.brightlocal.sdk.exception.ApiException; + +ApiContext context = new ApiContext(API_KEY, API_SECRET); +// Initialize sdk +ApiBatch apiBatch = new ApiBatch(context); +try { + // Create a new batch + Batch batch = apiBatch.create(); + if (batch.isSuccess()) { + batch = apiBatch.delete(batch); + if (batch.isSuccess()) { + context.log("Deleted batch successfully."); + } + } +} catch (ApiException e) { + e.printStackTrace(); +} +``` + ```shell curl -X DELETE 'https://tools.brightlocal.com/seo-tools/api/v4/batch?api-key=&batch-id=' ``` @@ -305,6 +397,29 @@ if ($batchApi->stop($batchId)) { } ``` +```java +import com.brightlocal.sdk.ApiContext; +import com.brightlocal.sdk.api.ApiBatch; +import com.brightlocal.sdk.api.Batch; +import com.brightlocal.sdk.exception.ApiException; + +ApiContext context = new ApiContext(API_KEY, API_SECRET); +// Initialize sdk +ApiBatch apiBatch = new ApiBatch(context); +try { + // Create a new batch + Batch batch = apiBatch.create(); + if (batch.isSuccess()) { + batch = apiBatch.stop(batch); + if (batch.isSuccess()) { + context.log("Stopped batch successfully."); + } + } +} catch (ApiException e) { + e.printStackTrace(); +} +``` + ```shell curl -X PUT -d 'api-key=' \ diff --git a/source/includes/_offsite-seo.md b/source/includes/_offsite-seo.md index 78c2fd4ccb1..5f9aa806d5c 100644 --- a/source/includes/_offsite-seo.md +++ b/source/includes/_offsite-seo.md @@ -39,6 +39,60 @@ if ($batchId) { } ``` +```java +import com.brightlocal.sdk.ApiContext; +import com.brightlocal.sdk.api.ApiBatch; +import com.brightlocal.sdk.api.ApiOffsiteSeo; +import com.brightlocal.sdk.api.Batch; +import com.brightlocal.sdk.api.BatchResults; +import com.brightlocal.sdk.api.Job; +import com.brightlocal.sdk.exception.ApiException; + +ApiContext context = new ApiContext(API_KEY, API_SECRET); +// Initialize sdk +ApiBatch apiBatch = new ApiBatch(context); +ApiOffsiteSeo apiOffsiteSeo = new ApiOffsiteSeo(context); + +try { + // Step 1: Create a new batch + Batch batch = apiBatch.create(); + if (batch.isSuccess()) { + // If batch was successfully created, ad jobs + context.log("Created batch ID " + batch.getId()); + + // Step 2: Add offsite job to batch + Job job = apiOffsiteSeo.offsite(batch, "http://www.gramercytavern.com"); + if (job.isSuccess()) { + context.log("Added job with ID " + job.getJobId()); + } + // Step 3: Commit batch (to signal all jobs added, processing + // starts) + batch = apiBatch.commit(batch); + if (batch.isSuccess()) { + context.log("Committed batch successfully."); + + // poll for results, in a real world example you might + // want to do this in a separate thread + BatchResults batchResults; + do { + batchResults = apiBatch.getResults(batch); + + try { + Thread.sleep(5000); // limit how often you poll + } catch (InterruptedException e) { + } + } while (!"Stopped".equals(batchResults.getStatus()) + && !"Finished".equals(batchResults.getStatus())); + + // Prints result + context.log(batchResults.getRawResponse()); + } + } +} catch (ApiException e) { + e.printStackTrace(); +} +``` + > Success (201 Created) ```json @@ -134,6 +188,68 @@ if ($batchId) { } ``` +```java +import com.brightlocal.sdk.ApiContext; +import com.brightlocal.sdk.api.ApiBatch; +import com.brightlocal.sdk.api.ApiOffsiteSeo; +import com.brightlocal.sdk.api.Batch; +import com.brightlocal.sdk.api.BatchResults; +import com.brightlocal.sdk.api.Job; +import com.brightlocal.sdk.exception.ApiException; + +ApiContext context = new ApiContext(API_KEY, API_SECRET); +// Initialize sdk +ApiBatch apiBatch = new ApiBatch(context); +ApiOffsiteSeo apiOffsiteSeo = new ApiOffsiteSeo(context); + +SocialProfile socialProfile = new SocialProfile("http://www.gramercytavern.com/", "Gramercy Tavern", "New York", "NY", "CAN"); +socialProfile.setFetchFacebook(SocialProfile.YES_NO.no); +// socialProfile.setFetchFoursquare(SocialProfile.YES_NO.yes); +// socialProfile.setFetchTwitter(SocialProfile.YES_NO.yes); +socialProfile.setStreetAddress("42 E 20th St"); +socialProfile.setTelephone("(212) 477-0777"); +socialProfile.setPostcode("10003"); + +try { + // Step 1: Create a new batch + Batch batch = apiBatch.create(); + if (batch.isSuccess()) { + // If batch was successfully created, ad jobs + context.log("Created batch ID " + batch.getId()); + + // Step 2: Add social profile job to batch + Job job = apiOffsiteSeo.socialProfiles(batch, socialProfile); + if (job.isSuccess()) { + context.log("Added job with ID " + job.getJobId()); + } + // Step 3: Commit batch (to signal all jobs added, processing + // starts) + batch = apiBatch.commit(batch); + if (batch.isSuccess()) { + context.log("Committed batch successfully."); + + // poll for results, in a real world example you might + // want to do this in a separate thread + BatchResults batchResults; + do { + batchResults = apiBatch.getResults(batch); + + try { + Thread.sleep(5000); // limit how often you poll + } catch (InterruptedException e) { + } + } while (!"Stopped".equals(batchResults.getStatus()) + && !"Finished".equals(batchResults.getStatus())); + + // Prints result + context.log(batchResults.getRawResponse()); + } + } +} catch (ApiException e) { + e.printStackTrace(); +} +``` + > Success (201 Created) ```json diff --git a/source/includes/_rankings.md b/source/includes/_rankings.md index eaed38ba160..9288e9363ff 100644 --- a/source/includes/_rankings.md +++ b/source/includes/_rankings.md @@ -67,6 +67,80 @@ if ($batchId) { } ``` +```java +import com.brightlocal.sdk.ApiContext; +import com.brightlocal.sdk.api.ApiBatch; +import com.brightlocal.sdk.api.ApiRanking; +import com.brightlocal.sdk.api.Batch; +import com.brightlocal.sdk.api.BatchResults; +import com.brightlocal.sdk.api.Job; +import com.brightlocal.sdk.exception.ApiException; + +ApiContext context = new ApiContext(API_KEY, API_SECRET); +// Initialize sdk +ApiBatch apiBatch = new ApiBatch(context); +ApiRanking apiRanking = new ApiRanking(context); + +// Prepare test searches +List searches = new ArrayList<>(); +com.brightlocal.sdk.pojo.Search search = new com.brightlocal.sdk.pojo.Search("google", "USA", "restaurant new york"); +search.setGoogleLocation("New York, NY"); +search.addUrl("le-bernardin.com"); +search.addBusinessNames("Le Bernardin"); +searches.add(search); + +search = new com.brightlocal.sdk.pojo.Search("google", "USA", "estaurant manhattan"); +search.setGoogleLocation("New York, NY"); +search.addUrl("le-bernardin.com"); +search.addBusinessNames("Le Bernardin"); +searches.add(search); + +search = new com.brightlocal.sdk.pojo.Search("google", "USA", "restaurant 10019"); +search.setGoogleLocation("New York, NY"); +search.addUrl("le-bernardin.com"); +search.addBusinessNames("Le Bernardin"); +searches.add(search); + +try { + // Create a new batch + Batch batch = apiBatch.create(); + if (batch.isSuccess()) { + // If batch was successfully created, ad jobs + context.log("Created batch ID " + batch.getId()); + + for (com.brightlocal.sdk.pojo.Search search1 : searches) { + Job job = apiRanking.addSearchJob(batch, search1); + if (job.isSuccess()) { + context.log("Added job with ID " + job.getJobId()); + } + } + + batch = apiBatch.commit(batch); + if (batch.isSuccess()) { + context.log("Committed batch successfully."); + + // poll for results, in a real world example you might + // want to do this in a separate thread + BatchResults batchResults; + do { + batchResults = apiBatch.getResults(batch); + + try { + Thread.sleep(5000); // limit how often you poll + } catch (InterruptedException e) { + } + } while (!"Stopped".equals(batchResults.getStatus()) + && !"Finished".equals(batchResults.getStatus())); + + // Prints result + context.log(batchResults.getRawResponse()); + } + } +} catch (ApiException e) { + e.printStackTrace(); +} +``` + ```shell curl -X POST \ -d 'api-key=' \ @@ -525,6 +599,69 @@ if ($batchId) { } ``` +```java +import com.brightlocal.sdk.ApiContext; +import com.brightlocal.sdk.api.ApiBatch; +import com.brightlocal.sdk.api.ApiRanking; +import com.brightlocal.sdk.api.Batch; +import com.brightlocal.sdk.api.BatchResults; +import com.brightlocal.sdk.api.BulkJob; +import com.brightlocal.sdk.exception.ApiException; + +ApiContext context = new ApiContext(API_KEY, API_SECRET); +// Initialize sdk +ApiBatch apiBatch = new ApiBatch(context); +ApiRanking apiRanking = new ApiRanking(context); + +// Prepare test search +List searches = new ArrayList<>(); +searches.add("restaurant new york"); +searches.add("restaurant manhattan"); +searches.add("restaurant 10019"); + +com.brightlocal.sdk.pojo.BulkSearch bulkSearch = new com.brightlocal.sdk.pojo.BulkSearch("google", "USA", + searches); +bulkSearch.setGoogleLocation("New York, NY"); +bulkSearch.addUrl("le-bernardin.com"); +bulkSearch.addBusinessNames("Le Bernardin"); + +try { + // Create a new batch + Batch batch = apiBatch.create(); + if (batch.isSuccess()) { + // If batch was successfully created, ad jobs + context.log("Created batch ID " + batch.getId()); + + BulkJob job = apiRanking.addBulkSearchJob(batch, bulkSearch); + if (job.isSuccess()) { + context.log("Added jobs with ID " + job.getJobIds()); + } + + batch = apiBatch.commit(batch); + if (batch.isSuccess()) { + context.log("Committed batch successfully."); + + // poll for results, in a real world example you might + // want to do this in a separate thread + BatchResults batchResults; + do { + batchResults = apiBatch.getResults(batch); + + try { + Thread.sleep(5000); // limit how often you poll + } catch (InterruptedException e) { + } + } while (!"Stopped".equals(batchResults.getStatus()) + && !"Finished".equals(batchResults.getStatus())); + // Prints result + context.log(batchResults.getRawResponse()); + } + } +} catch (ApiException e) { + e.printStackTrace(); +} +``` + ```shell curl -X POST \ -F 'api-key=' \ diff --git a/source/includes/_reviews.md b/source/includes/_reviews.md index ed3f19b0467..8f7e2dd4aee 100644 --- a/source/includes/_reviews.md +++ b/source/includes/_reviews.md @@ -45,6 +45,69 @@ if ($batchId) { } ``` +```java +import com.brightlocal.sdk.ApiContext; +import com.brightlocal.sdk.api.ApiBatch; +import com.brightlocal.sdk.api.ApiReview; +import com.brightlocal.sdk.api.Batch; +import com.brightlocal.sdk.api.BatchResults; +import com.brightlocal.sdk.api.Job; +import com.brightlocal.sdk.exception.ApiException; +import com.brightlocal.sdk.pojo.Review; + +ApiContext context = new ApiContext(API_KEY, API_SECRET); +// Initialize sdk +ApiBatch apiBatch = new ApiBatch(context); +ApiReview apiReview = new ApiReview(context); + +// Prepare test reviews +List reviews = new ArrayList<>(); +reviews.add(new Review("https://plus.google.com/114222978585544488148/about?hl=en", "USA")); +reviews.add(new Review("https://plus.google.com/117313296997732479889/about?hl=en", "USA")); +reviews.add(new Review("https://plus.google.com/111550668382222753542/about?hl=en", "USA")); + +try { + // Step 1: Create a new batch + Batch batch = apiBatch.create(); + if (batch.isSuccess()) { + // If batch was successfully created, ad jobs + context.log("Created batch ID " + batch.getId()); + + // Step 2: Add review lookup jobs to batch + for (Review review : reviews) { + Job job = apiReview.addFetchReviewsJob(batch, review); + if (job.isSuccess()) { + context.log("Added job with ID " + job.getJobId()); + } + } + // Step 3: Commit batch (to signal all jobs added, processing + // starts) + batch = apiBatch.commit(batch); + if (batch.isSuccess()) { + context.log("Committed batch successfully."); + + // poll for results, in a real world example you might + // want to do this in a separate thread + BatchResults batchResults; + do { + batchResults = apiBatch.getResults(batch); + + try { + Thread.sleep(5000); // limit how often you poll + } catch (InterruptedException e) { + } + } while (!"Stopped".equals(batchResults.getStatus()) + && !"Finished".equals(batchResults.getStatus())); + + // Prints result + context.log(batchResults.getRawResponse()); + } + } +} catch (ApiException e) { + e.printStackTrace(); +} +``` + > Success (201 Created) ```json @@ -142,6 +205,63 @@ if ($batchId) { } ``` +```java +import com.brightlocal.sdk.ApiContext; +import com.brightlocal.sdk.api.ApiBatch; +import com.brightlocal.sdk.api.ApiReview; +import com.brightlocal.sdk.api.Batch; +import com.brightlocal.sdk.api.BatchResults; +import com.brightlocal.sdk.api.Job; +import com.brightlocal.sdk.exception.ApiException; +import com.brightlocal.sdk.pojo.Review; + +ApiContext context = new ApiContext(API_KEY, API_SECRET); +// Initialize sdk +ApiBatch apiBatch = new ApiBatch(context); +ApiReview apiReview = new ApiReview(context); + +ReviewBusinessData reviewBusinessData = new ReviewBusinessData("Le Bernardin", "USA", "New York", "10019", "google", "(212) 554-1515"); + +try { + // Step 1: Create a new batch + Batch batch = apiBatch.create(); + if (batch.isSuccess()) { + // If batch was successfully created, ad jobs + context.log("Created batch ID " + batch.getId()); + + // Step 2: Add review lookup jobs to batch + Job job = apiReview.addFetchReviewsJob(batch, reviewBusinessData); + if (job.isSuccess()) { + context.log("Added job with ID " + job.getJobId()); + } + // Step 3: Commit batch (to signal all jobs added, processing + // starts) + batch = apiBatch.commit(batch); + if (batch.isSuccess()) { + context.log("Committed batch successfully."); + + // poll for results, in a real world example you might + // want to do this in a separate thread + BatchResults batchResults; + do { + batchResults = apiBatch.getResults(batch); + + try { + Thread.sleep(5000); // limit how often you poll + } catch (InterruptedException e) { + } + } while (!"Stopped".equals(batchResults.getStatus()) + && !"Finished".equals(batchResults.getStatus())); + + // Prints result + context.log(batchResults.getRawResponse()); + } + } +} catch (ApiException e) { + e.printStackTrace(); +} +``` + > Success (201 Created) ```json diff --git a/source/index.md b/source/index.md index 7d721e3d0ea..a0afbea6c64 100644 --- a/source/index.md +++ b/source/index.md @@ -3,6 +3,7 @@ title: API Reference language_tabs: - php + - java - shell toc_footers: