-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
297 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>RestClient</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.6 | ||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.rest.api; | ||
|
||
import java.net.URL; | ||
import java.net.HttpURLConnection; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.BufferedReader; | ||
import java.io.OutputStream; | ||
|
||
public class Client | ||
{ | ||
private String _authToken; | ||
private String _baseUrl; | ||
|
||
public Client(String user, String password, String url) | ||
{ | ||
_baseUrl = url; | ||
|
||
String authString = user + ":" + password; | ||
_authToken = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(authString.getBytes()); | ||
} | ||
|
||
public Response get(String uri) | ||
{ | ||
return this.execute(uri, Method.GET, null); | ||
} | ||
|
||
public Response post(String uri, String body) | ||
{ | ||
return this.execute(uri, Method.POST, body); | ||
} | ||
|
||
public Response put(String uri, String body) | ||
{ | ||
return this.execute(uri, Method.PUT, body); | ||
} | ||
|
||
public void delete(String uri) | ||
{ | ||
this.execute(uri, Method.DELETE, null); | ||
} | ||
|
||
public Response execute(String uri, Method method, String body) | ||
{ | ||
Response response = new Response(); | ||
|
||
try | ||
{ | ||
URL url = new URL(_baseUrl + uri); | ||
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | ||
|
||
conn.setInstanceFollowRedirects(false); | ||
conn.setRequestMethod(method.toString()); | ||
conn.setRequestProperty("Content-Type", "application/json"); | ||
conn.setRequestProperty("Authorization", _authToken); | ||
|
||
if (method == Method.POST || method == Method.PUT) | ||
{ | ||
conn.setDoOutput(true); | ||
final OutputStream os = conn.getOutputStream(); | ||
os.write(body.getBytes()); | ||
os.flush(); | ||
os.close(); | ||
} | ||
|
||
InputStream is = conn.getInputStream(); | ||
BufferedReader rd = new BufferedReader(new InputStreamReader( is)); | ||
|
||
String line; | ||
while ((line = rd.readLine()) != null) | ||
{ | ||
response.body += line; | ||
} | ||
rd.close(); | ||
|
||
response.statusCode = conn.getResponseCode(); | ||
conn.disconnect(); | ||
} | ||
catch (Exception e) | ||
{ | ||
response.exception = e.getMessage(); | ||
} | ||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.rest.api; | ||
|
||
public enum Method | ||
{ | ||
GET, | ||
POST, | ||
PUT, | ||
DELETE | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.rest.api; | ||
|
||
public class Response | ||
{ | ||
public int statusCode; | ||
public String body = ""; | ||
public String exception; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> | ||
<classpathentry kind="lib" path="lib/gson-2.2.2.jar"/> | ||
<classpathentry kind="src" path="/RestClient"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>ContactSample</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
11 changes: 11 additions & 0 deletions
11
Samples/ContactSample/.settings/org.eclipse.jdt.core.prefs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.6 | ||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.6 |
Binary file not shown.
48 changes: 48 additions & 0 deletions
48
Samples/ContactSample/src/com/rest/api/samples/ContactHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.rest.api.samples; | ||
|
||
import com.rest.api.*; | ||
import com.rest.api.samples.models.*; | ||
import com.google.gson.Gson; | ||
|
||
public class ContactHelper | ||
{ | ||
private Client _client; | ||
|
||
public ContactHelper(String site, String user, String password, String baseUrl) | ||
{ | ||
_client = new Client(site + "\\" + user, password, baseUrl); | ||
} | ||
|
||
public Contact GetContact(int id) | ||
{ | ||
Contact contact = new Contact(); | ||
|
||
try | ||
{ | ||
Response response = _client.get("/data/contact/" + String.valueOf(id)); | ||
Gson gson = new Gson(); | ||
contact = gson.fromJson(response.body, Contact.class); | ||
} | ||
catch (Exception e) | ||
{ | ||
e.printStackTrace(); | ||
} | ||
return contact; | ||
} | ||
|
||
public SearchResponse<Contact> GetContacts(String search, int page, int count) | ||
{ | ||
SearchResponse<Contact> contacts = null; | ||
try | ||
{ | ||
Response response = _client.get("/data/contacts?search=" + search + "&page=" + page + "&count=" + count); | ||
Gson gson = new Gson(); | ||
contacts = gson.fromJson(response.body, SearchResponse.class); | ||
} | ||
catch (Exception e) | ||
{ | ||
e.printStackTrace(); | ||
} | ||
return contacts; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
Samples/ContactSample/src/com/rest/api/samples/models/Contact.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.rest.api.samples.models; | ||
|
||
import java.util.List; | ||
|
||
public class Contact | ||
{ | ||
public String accountName; | ||
public String address1; | ||
public String address2; | ||
public String address3; | ||
public String businessPhone; | ||
public String city; | ||
public String country; | ||
public String firstName; | ||
public String emailAddress; | ||
public int id; | ||
public boolean isBounceback; | ||
public boolean isSubscribed; | ||
public String lastName; | ||
public String salesPerson; | ||
public String title; | ||
public List<FieldValue> fieldValues; | ||
} |
7 changes: 7 additions & 0 deletions
7
Samples/ContactSample/src/com/rest/api/samples/models/FieldValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.rest.api.samples.models; | ||
|
||
public class FieldValue | ||
{ | ||
public int id; | ||
public String value; | ||
} |
11 changes: 11 additions & 0 deletions
11
Samples/ContactSample/src/com/rest/api/samples/models/SearchResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.rest.api.samples.models; | ||
|
||
import java.util.List; | ||
|
||
public class SearchResponse<T> | ||
{ | ||
public List<T> elements; | ||
public int total; | ||
public int page; | ||
public int pageSize; | ||
} |
35 changes: 35 additions & 0 deletions
35
Samples/ContactSample/src/com/rest/api/samples/tests/ContactHelperTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.rest.api.samples.tests; | ||
|
||
import junit.framework.Assert; | ||
import com.rest.api.samples.models.*; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.rest.api.samples.ContactHelper; | ||
|
||
|
||
public class ContactHelperTests | ||
{ | ||
private ContactHelper _helper; | ||
|
||
@Before | ||
public void Setup() | ||
{ | ||
_helper = new ContactHelper("site", "user", "password", "https://secure.eloqua.com/API/REST/1.0"); | ||
} | ||
|
||
@Test | ||
public void GetContactTest() | ||
{ | ||
int id = 152365; | ||
Contact contact = _helper.GetContact(id); | ||
Assert.assertEquals(id, contact.id); | ||
} | ||
|
||
@Test | ||
public void GetContactsTest() | ||
{ | ||
SearchResponse<Contact> contacts = _helper.GetContacts("*", 1, 10); | ||
Assert.assertTrue(contacts.total > 0); | ||
} | ||
} |