d17a448e31c1e075a562d75570f0667f939a3196
[aai/rest-client.git] / README.md
1 # Common REST Client Library
2
3 This library provides a single client implementation to be used by micro services for communicating via its REST API.
4
5 ---
6
7 ## Usage
8 In order to make the _REST Client_ library available to your microservice, include the following dependency in your service's pom.xml:
9
10     <!-- Common REST Client Library -->
11     <dependency>
12         <groupId>org.openecomp.aai</groupId>
13         <artifactId>rest-client</artifactId>
14         <version>1.0.0-SNAPSHOT</version>
15     </dependency>
16     
17 ## Code Examples
18
19 ### Creating and Configuring a Client Instance
20 In order to start talking to a service, you need to create a client instance and configure it.  The _RestClient_ uses a fluent interface which allows it to be both instantiated and configured as in the following example:
21
22     // Create an instance of the Rest Client and configure it.
23     RestClient myClient = new RestClient()
24         .validateServerHostname(false)
25         .validateServerCertChain(true)
26         .clientCertFile("certificate_filename")
27         .trustStroe("trust_store_filename")
28         .connectTimeoutMs(1000)
29         .readTimeoutMs(1000)
30         
31 Note, that all of the above configuration parameters are optional and will be set to default values if they are not specified.
32
33 ### Querying The A&AI
34 Once your service has a client instance, it can query the _Active & Available Inventory_ by specifying an HTTP endpoint, headers, and the expected response format:
35
36         MultivaluedMap<String, String> headers = new MultivaluedMapImpl();
37         headers.put("Accept", Arrays.asList(new String[]{"application/json"}));
38         headers.put("X-FromAppId", Arrays.asList(new String[]{"APP-ID"}));
39         headers.put("X-TransactionId", Arrays.asList(new String[]{UUID.randomUUID().toString()}));
40
41     OperationResult result = myClient.queryActiveInventory("http://some/endpoint", headers, RestClient.RESPONSE_MIME_TYPE.JSON);
42     
43     // You can also specify number of re-tries:
44     int retries = 3
45     OperationResult result = myClient.queryActiveInventory("http://some/endpoint", headers, RestClient.RESPONSE_MIME_TYPE.JSON, retries);
46
47          
48 The result of the query is returned as an _OperationResult_ object, which can be unpacked in the following manner:
49
50 The standard HTTP result code received back from the _A&AI_ is accessible as follows:
51
52     int resultCode = getResultCode()
53
54 The actual result payload is accessible in the following manner:
55
56     String resultPayload = result.getResult()
57
58 Finally, in the event of a failure, a failure cause message will be populated and can be accessed as follows:
59
60     String failureCause = result.getFailureCause()