# Data Revealing

## Start session

VGS Show SDK requires `vault id` and [environment](https://docs.verygoodsecurity.com/vault/vault-management/going-live#sandbox-vs-live). To create a `VGSShow` for your application you need to get your `vault Id` and specify `Environment`. For example:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val id = "<VAULT_ID>"
val environment = VGSEnvironment.Sandbox()
val vgsShow = VGSShow(context, id, environment) 
```

{% endtab %}

{% tab title="Java" %}

```java
String id = "<VAULT_ID>";
VGSEnvironment environment = new VGSEnvironment.Sandbox();
VGSShow vgsShow = new VGSShow(context, id, environment); 
```

{% endtab %}
{% endtabs %}

### Multiple Data Regions

Please use the following configuration to set up a specific data region if needed.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val id = "<VAULT_ID>"
val environment = VGSEnvironment.Sandbox("eu-1")
val vgsShow = VGSShow(context, id, environment) 
```

{% endtab %}

{% tab title="Java" %}

```java
String id = "<VAULT_ID>";
VGSEnvironment environment = new VGSEnvironment.Sandbox("eu-1");
VGSShow vgsShow = new VGSShow(context, id, environment); 
```

{% endtab %}
{% endtabs %}

## Add a Custom Hostname

When integrated with VGS, by default, the traffic is passed via the VGS proxy, which has `tntxxxxx.sandbox.verygoodproxy.com` format where `tntxxxxx` is your Vault identifier. Show SDK allows you to set your own hostname and make requests to a non-VGS domain name or localhost on your local machine.\
Before adding a new Hostname, you should create a CNAME record for your existing domain in your DNS provider’s account that should point to `<VAULT_ID>.<ENVIRONMENT>.verygoodproxy.com`.

\*\* Learn more about [Custom Hostnames](https://docs.verygoodsecurity.com/vault/http-proxy/inbound-connection/custom-hostnames)\*\*.

To specify your hostname, you have to initializethe  Show SDK using `Builder` class:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val vgsForm: VGSShow = VGSShow.Builder(this, "<VAULT_ID>")
    .setEnvironment(VGSEnvironment.Sandbox())
    .setHostname("www.customdomain.com")
    .build()
      
```

{% endtab %}

{% tab title="Java" %}

```java
VGSShow vgsForm = new VGSShow.Builder(context, "<VAULT_ID>")
    .setEnvironment(new VGSEnvironment.Sandbox())
    .setHostname("www.customdomain.com")
    .build();
        
```

{% endtab %}
{% endtabs %}

> • When the hostname is not valid or not registered in VGS system, it will be ignored by SDK and all the traffic will be passed via VGS proxy directly.\
> • Only **https** scheme is supported.\\

## Data subscribing

It allows **VGSShow** to bind to the field and deliver and deliver response to user.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val infoField = findViewById<VGSTextView>(R.id.infoField)
vgsShow.subscribe(infoField)
```

{% endtab %}

{% tab title="Java" %}

```java
VGSTextView infoField = findViewById(R.id.infoField);
vgsShow.subscribe(infoField);
```

{% endtab %}
{% endtabs %}

## Reveal information

**Show SDK** provides several ways to reveal information. Reveal can be made in synchronous or asynchronous ways, and can also receive different parameters.

### Synchronous request

To make a request in synchronous way you should call `request()` function.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val request = VGSRequest.Builder("/post", VGSHttpMethod.POST)
    .build()
vgsShow.request(request)
```

{% endtab %}

{% tab title="Java" %}

```java
VGSRequest request = new VGSRequest.Builder("/post", VGSHttpMethod.POST)
        .build();
vgsShow.request(request);
```

{% endtab %}
{% endtabs %}

### Asynchronous request

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val request = VGSRequest.Builder("/post", VGSHttpMethod.POST)
    .build()
vgsShow.requestAsync(request)
```

{% endtab %}

{% tab title="Java" %}

```java
VGSRequest request = new VGSRequest.Builder("/post", VGSHttpMethod.POST)
        .build();
vgsShow.requestAsync(request);
```

{% endtab %}
{% endtabs %}

## Headers

SDK provides two options to define headers in request: **static** and **dynamic**.

### Static headers

**Static** headers could be set for all requests during session. Use `setCustomHeader()` to set one.

```kotlin
vgsShow.setCustomHeader("<HEADER-NAME>", "<HEADER_VALUE>")
```

### Dynamic headers

**Dynamic** headers are set for one specific request. You can set them using `VGSRequest.Builder`

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val request = VGSRequest.Builder("/post", VGSHttpMethod.POST)
    .headers(mapOf("<HEADER-NAME>" to "<HEADER_VALUE>"))
    .build()
vgsShow.request(request)
```

{% endtab %}

{% tab title="Java" %}

```java
Map<String, String> headers = new HashMap<>();
headers.put("<HEADER-NAME>", "<HEADER_VALUE>");\n
VGSRequest request = new VGSRequest.Builder("/post", VGSHttpMethod.POST)
        .headers(headers)
        .build();
vgsShow.request(request);
```

{% endtab %}
{% endtabs %}

## Payload

To get the revealed data, you should specify the payload in `json` with `<CONTENT_PATH>` and related `<alias>`. Example payload: `"sensitive_info : "ton_qwerty12345567890""`.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val keyValue = mapOf("sensitive_info" to "ton_qwerty12345567890")\n
val payload = JSONObject(keyValue)
val request = VGSRequest.Builder("/post", VGSHttpMethod.POST)
    .body(payload, VGSHttpBodyFormat.JSON)
    .build()
vgsShow.requestAsync(request)
```

{% endtab %}

{% tab title="Java" %}

```java
Map<String, Object> keyValue = new HashMap<>();
keyValue.put("sensitive_info", "ton_qwerty12345567890");
JSONObject payload = new JSONObject(keyValue);
VGSRequest request = new VGSRequest.Builder("/post", VGSHttpMethod.POST)
    .body(payload, VGSHttpBodyFormat.JSON)
    .build();
vgsShow.request(request);
```

{% endtab %}
{% endtabs %}

Also, you can add any additional/extra data to the payload:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val keyValue = mapOf(
    "sensitive_info" to "ton_qwerty12345567890",
    "extraData" to mapOf("someKey" to "someValue")
)
val payload = JSONObject(keyValue)
val request = VGSRequest.Builder("/post", VGSHttpMethod.POST)
    .body(payload, VGSHttpBodyFormat.JSON)
    .build()
vgsShow.requestAsync(request)
```

{% endtab %}

{% tab title="Java" %}

```java
Map<String, String> extraData = new HashMap<>();
extraData.put("someKey", "someValue");
Map<String, Object> keyValue = new HashMap<>();
keyValue.put("sensitive_info", "ton_qwerty12345567890");
keyValue.put("extraData", extraData);
JSONObject payload = new JSONObject(keyValue);
VGSRequest request = new VGSRequest.Builder("/post", VGSHttpMethod.POST)
    .body(payload, VGSHttpBodyFormat.JSON)
    .build();
vgsShow.request(request);
```

{% endtab %}
{% endtabs %}

## Handling responses from asynchronous requests

Interface definition for a callback to be invoked when **VGSShow** receives a response from the Server.

The developer can receive a local or HTTP response code in a callback. List of current response codes:

| **Code**  | **Description**                                             |
| --------- | ----------------------------------------------------------- |
| 100 - 600 | Http response status code                                   |
| 1401      | When response data format is not supported                  |
| 1404      | When payload is invalid                                     |
| 1480      | When VGS configuration URL is not valid                     |
| 1481      | When internet permission not added to `AndroidManifest.xml` |
| 1482      | When device is not connected to internet                    |
| 1483      | When network request is executing to long                   |

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
vgsShow.addResponseListener(object : VgsShowResponseListener {\n
    override fun onResponse(response: VGSResponse) {
        when (response) {
            is VGSResponse.Success -> {
                val successCode = response.code
            }
            is VGSResponse.Error -> {
                val errorCode = response.code
                val message = response.message
            }
        }
    }
})
```

{% endtab %}

{% tab title="Java" %}

```java
vgsShow.addResponseListener(new VgsShowResponseListener() {\n
    @Override
    public void onResponse(@NotNull VGSResponse response) {
        if(response instanceof VGSResponse.Success) {
            int successCode = ((VGSResponse.Success)response).getCode();
        } else {
            int errorCode = ((VGSResponse.Error)response).getCode();
            String message = ((VGSResponse.Error)response).getMessage();
        }
    }
});
```

{% endtab %}
{% endtabs %}

## Apply previous response

`VGSResponse.Success` can be applied again by calling `VGSShow.applyResponse` function with the previous response as a parameter.

## End session

Clear all information collected before by VGSCollect and cancel all requests. Call `onDestroy` inside the Android onDestroy callback.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
override fun onDestroy() {
    vgsShow.onDestroy()
    super.onDestroy()
}
```

{% endtab %}

{% tab title="Java" %}

```java
@Override
protected void onDestroy() {
    vgsShow.onDestroy();
    super.onDestroy();
}
```

{% endtab %}
{% endtabs %}
