Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added java wrapper examples #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions source/includes/_auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<INSERT_API_KEY>$expiry" | openssl dgst -sha1 -binary -hmac "<INSERT_API_KEY>" | base64)
Expand Down
115 changes: 115 additions & 0 deletions source/includes/_batch.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<INSERT_API_KEY>' \
Expand Down Expand Up @@ -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=<INSERT_API_KEY>' \
Expand Down Expand Up @@ -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=<INSERT_API_KEY>&batch-id=<INSERT_BATCH_ID>'
```
Expand Down Expand Up @@ -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=<INSERT_API_KEY>&batch-id=<INSERT_BATCH_ID>'
```
Expand Down Expand Up @@ -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=<INSERT_API_KEY>' \
Expand Down
116 changes: 116 additions & 0 deletions source/includes/_offsite-seo.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading