|
| 1 | +## Rest API SDK Core |
| 2 | + |
| 3 | +[](https://maven-badges.herokuapp.com/maven-central/com.taboola/rest-api-sdk-core) |
| 4 | +[](https://travis-ci.org/taboola/rest-api-sdk-core) |
| 5 | + |
| 6 | +### Table of Contents |
| 7 | +1. [Getting Started](#1-getting-started) |
| 8 | +2. [Exceptions](#2-exceptions) |
| 9 | +3. [Usage](#3-usage) |
| 10 | + |
| 11 | +### Intro |
| 12 | +Easily create Rest API clients by defining endpoint mappings and models that is going to be filled by JSON over HTTP |
| 13 | + |
| 14 | +## 1. Getting Started |
| 15 | + |
| 16 | + |
| 17 | +### 1.1 Create Rest Client |
| 18 | +First you will need _RestAPIClient.java_ object |
| 19 | +``` |
| 20 | +RestAPIClient restClient = RestAPIClient.builder() |
| 21 | + .setBaseUrl('your_base_url') |
| 22 | + .build(); |
| 23 | +``` |
| 24 | + |
| 25 | +### 1.2 Create Model And Mapping |
| 26 | +To know what is possible when creating endpoints interface please refer to [Retrofit2 documentation](https://square.github.io/retrofit/) |
| 27 | +``` |
| 28 | +public class EntityModelExample { |
| 29 | + private String id; |
| 30 | + private String name; |
| 31 | +
|
| 32 | + public String getId() { return id; } |
| 33 | + public String setId(String id) { this.id = id; } |
| 34 | + public String getName() { return name; } |
| 35 | + public String setName(String name) { this.name = name; } |
| 36 | +} |
| 37 | +
|
| 38 | +public interface EntityModelEndpoint { |
| 39 | +
|
| 40 | + @POST("/{account_id}") |
| 41 | + @Headers("Content-Type: application/json") |
| 42 | + EntityModelExample update(@Header("Authorization") String accessToken, |
| 43 | + @Path("account_id") String accountId, |
| 44 | + @Body EntityModelExample entity) throws RestAPIException; |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +### 1.3 Create Rest Client Endpoint |
| 49 | +``` |
| 50 | +EntityModelEndpoint endpoint = restClient.createRetrofitEndpoint(EntityModelEndpoint.class); |
| 51 | +// use endpoint... |
| 52 | +``` |
| 53 | + |
| 54 | +### 2. Exceptions |
| 55 | + |
| 56 | +- **RestAPIUnauthorizedException** - Token is expired or bad credentials were supplied (HTTP status 401) |
| 57 | + - Can be resolved by re-authentication or making sure that supplied credentials are correct |
| 58 | +- **RestAPIRequestException** - Bad request (HTTP status 4xx) |
| 59 | + - Can be resolved by fixing the request to a valid one |
| 60 | +- **RestAPIConnectivityException** - Connectivity issues (HTTP status 5xx) |
| 61 | + - Can be resolved by retrying or fixing networking issues |
| 62 | + |
| 63 | +### 3. Usage |
| 64 | + |
| 65 | +If your project is built with Maven add following to your pom file: |
| 66 | + |
| 67 | +``` |
| 68 | +<dependency> |
| 69 | + <groupId>com.taboola</groupId> |
| 70 | + <artifactId>rest-api-sdk-core</artifactId> |
| 71 | + <version>x.y.z</version> |
| 72 | +</dependency> |
| 73 | +``` |
| 74 | + |
| 75 | +If your project is built with Gradle add following to your gradle setting file: |
| 76 | + |
| 77 | +``` |
| 78 | +// https://mvnrepository.com/artifact/com.taboola/rest-api-sdk-core |
| 79 | +compile group: 'com.taboola', name: 'rest-api-sdk-core', version: 'x.y.z' |
| 80 | +``` |
| 81 | + |
| 82 | +Replace 'x.y.z' with the latest available version from [Maven Central](https://mvnrepository.com/artifact/com.taboola/rest-api-sdk-core) |
0 commit comments