<commit_before>#golatch [![Build Status](https://travis-ci.org/millenc/golatch.svg?branch=master)](https://travis-ci.org/millenc/golatch)

**golatch** is a package that easily lets you integrate [Latch](https://latch.elevenpaths.com/) in your [Go](https://golang.org/) applications. If you don't know what Latch is or how to use it please visit their [official site](https://latch.elevenpaths.com/).

##Installation


You can download the package manually and put it in your `/src` folder or use `go get`:

``` bash
$ go get github.com/millenc/golatch 
```
You can also use `go get -u` to update the package. 

##Usage

First you need to create the Latch struct that you will use to call all the operations of the API:

``` go
import "golatch"

// ...
latch := golatch.NewLatch("MyAppID", "MySecretKey")
``` 
where "MyAppID" and "MySecretKey" are your application's ID and secret key respectively. You can find this information in the developer's area of the official website once you create your application.

###Pairing

You can use the `Pair()` method providing the pairing token supplied by the user:

``` go
response, err := latch.Pair("MyToken")
```
If everything goes well, this call will return a response containing the Account ID that has been paired:

``` go
if err == nil {
	account_id := response.AccountId()
	//Store the account id
}
```
You must store this account ID together with the user information to use it in future API calls.

###Unpairing

Call the `Unpair()` method and pass the Account ID you want to unpair as argument:

``` go
if err := latch.Unpair("AccountID"); err != nil {
	//Handle error
}
```

If no error is returned, the account has been unpaired successfully.

###Locking/unlocking

*NOTE*: These methods require a GOLD or PLATINUM subscription in order to work. If you don't have any of these types of subscriptions you will get an error.

You can change the status of an account (lock/unlock) using the `Lock()`/`Unlock()` methods:

``` go
err := latch.Lock("AccountID")
err := latch.Unlock("AccountID")
```
If you have created operations for an application you can lock and unlock them using the `LockOperation()` and `UnlockOperation()` methods:

``` go
err := latch.LockOperation("AccountID", "MyOperationID")
err := latch.UnlockOperation("AccountID", "MyOperationID")
```

###Status

To get the current status of an account, you can use the `Status()` method:

``` go
if response, err := latch.Status("AccountID", false); err == nil {
	//Handle response
}
```
Upon success, this method will return a `response` struct of type `LatchStatusResponse`. If the second parameter is `true`, then no One-time password information will be included in the response. You can easily access the information for the root application/operation:

**`Status()`**: Gets the account status:

``` go
status := response.Status()
if status == golatch.LATCH_STATUS_ON {
	//Latch is ON for this account, don't let the user log in (for example)
}
```
As you can see in the previous snippet, to determine the status of an account it's recommended that you use the `golatch.LATCH_STATUS_ON` (ON status) and `golatch.LATCH_STATUS_OFF` (OFF status) constants.

**`TwoFactor()`**: Gets the two factor authentication information:

``` go
two_factor LatchTwoFactor = reponse.TwoFactor()
if two_factor.Token != "" {
	token     := two_factor.Token
	generated := two_factor.Generated
	//Handle two factor authentication
}
```
Please note that this method will always return a `LatchTwoFactor` struct, even if the response didn't include this information. Hence you should always test for zero values before proceeding (`two_factor.Token != ""`).

**`Operations()`**: Gets the child operations of the application:

``` go
operations := reponse.Operations()
```
An application (or operation) can have child operations. This method will return the status for these nested operations as a map of `LatchOperationStatus` indexed by operation ID. Each of these `LatchOperationStatus` structs have the following information:

``` go
status     := operation_status.Status
two_factor := operation_status.TwoFactor
operations := operation_status.Operations
```
The information contained in these fields is the same that has been previously discussed.

###Operation Status

**IMPORTANT**: If you have created operations for your application you should not query the status of the application, but rather the status of individual operations. To do so, you can use the `OperationStatus()` method:

``` go
if response, err := latch.OperationStatus("AccountID", "MyOperationID", false); err == nil {
    //Handle response
}
```
If the third parameter is `true`, then no One-time password information will be included in the response. The response is of the same type and contains the same information as the one returned by the `Status()` method.

###Managing operations

You can create/edit/delete operations directly from your application:

**Create operation**:

``` go
if response, err := latch.AddOperation(parentId, name, twoFactor, lockOnRequest); err == nil {
	//Get the ID of the newly created operation
	operationId := response.OperationId()
}
```
where:

* `parentId`: Id of this operation's parent (application or another operation).
* `name`: Name of the operation.
* `twoFactor`: Use of two factor authentication. One of these values:
	* OPT_IN (optional): `golatch.OPT_IN`
	* MANDATORY (mandatory): `golatch.MANDATORY`
	* DISABLED (disabled): `golatch.DISABLED`
* `lockOnRequest`: Takes the same values as twoFactor. 

**Modify operation**:<commit_msg>Added the silent parameter to the Status() and OperationStatus() usage instructions.<commit_after>#golatch [![Build Status](https://travis-ci.org/millenc/golatch.svg?branch=master)](https://travis-ci.org/millenc/golatch)

**golatch** is a package that easily lets you integrate [Latch](https://latch.elevenpaths.com/) in your [Go](https://golang.org/) applications. If you don't know what Latch is or how to use it please visit their [official site](https://latch.elevenpaths.com/).

##Installation


You can download the package manually and put it in your `/src` folder or use `go get`:

``` bash
$ go get github.com/millenc/golatch 
```
You can also use `go get -u` to update the package. 

##Usage

First you need to create the Latch struct that you will use to call all the operations of the API:

``` go
import "golatch"

// ...
latch := golatch.NewLatch("MyAppID", "MySecretKey")
``` 
where "MyAppID" and "MySecretKey" are your application's ID and secret key respectively. You can find this information in the developer's area of the official website once you create your application.

###Pairing

You can use the `Pair()` method providing the pairing token supplied by the user:

``` go
response, err := latch.Pair("MyToken")
```
If everything goes well, this call will return a response containing the Account ID that has been paired:

``` go
if err == nil {
	account_id := response.AccountId()
	//Store the account id
}
```
You must store this account ID together with the user information to use it in future API calls.

###Unpairing

Call the `Unpair()` method and pass the Account ID you want to unpair as argument:

``` go
if err := latch.Unpair("AccountID"); err != nil {
	//Handle error
}
```

If no error is returned, the account has been unpaired successfully.

###Locking/unlocking

*NOTE*: These methods require a GOLD or PLATINUM subscription in order to work. If you don't have any of these types of subscriptions you will get an error.

You can change the status of an account (lock/unlock) using the `Lock()`/`Unlock()` methods:

``` go
err := latch.Lock("AccountID")
err := latch.Unlock("AccountID")
```
If you have created operations for an application you can lock and unlock them using the `LockOperation()` and `UnlockOperation()` methods:

``` go
err := latch.LockOperation("AccountID", "MyOperationID")
err := latch.UnlockOperation("AccountID", "MyOperationID")
```

###Status

To get the current status of an account, you can use the `Status()` method:

``` go
if response, err := latch.Status("AccountID", false, false); err == nil {
	//Handle response
}
```
Upon success, this method will return a `response` struct of type `LatchStatusResponse`. If the second parameter is `true` (nootp), then the One-time password information will not be included in the response. If the third parameter is `true` (silent) Latch will not send a push notification to the user alerting of the access if the account's latch is on (this requires a SILVER, GOLD or PLATINUM subscription). You can easily access the information for the root application/operation:

**`Status()`**: Gets the account status:

``` go
status := response.Status()
if status == golatch.LATCH_STATUS_ON {
	//Latch is ON for this account, don't let the user log in (for example)
}
```
As you can see in the previous snippet, to determine the status of an account it's recommended that you use the `golatch.LATCH_STATUS_ON` (ON status) and `golatch.LATCH_STATUS_OFF` (OFF status) constants.

**`TwoFactor()`**: Gets the two factor authentication information:

``` go
two_factor LatchTwoFactor = reponse.TwoFactor()
if two_factor.Token != "" {
	token     := two_factor.Token
	generated := two_factor.Generated
	//Handle two factor authentication
}
```
Please note that this method will always return a `LatchTwoFactor` struct, even if the response didn't include this information. Hence you should always test for zero values before proceeding (`two_factor.Token != ""`).

**`Operations()`**: Gets the child operations of the application:

``` go
operations := reponse.Operations()
```
An application (or operation) can have child operations. This method will return the status for these nested operations as a map of `LatchOperationStatus` indexed by operation ID. Each of these `LatchOperationStatus` structs have the following information:

``` go
status     := operation_status.Status
two_factor := operation_status.TwoFactor
operations := operation_status.Operations
```
The information contained in these fields is the same that has been previously discussed.

###Operation Status

**IMPORTANT**: If you have created operations for your application you should not query the status of the application, but rather the status of individual operations. To do so, you can use the `OperationStatus()` method:

``` go
if response, err := latch.OperationStatus("AccountID", "MyOperationID", false, false); err == nil {
    //Handle response
}
```
If the third parameter is `true` (nootp), then no One-time password information will be included in the response. If the fourth parameter is `true` (silent) Latch will not send a push notification to the user alerting of the access if the operation's latch is on (this requires a SILVER, GOLD or PLATINUM subscription). The response is of the same type and contains the same information as the one returned by the `Status()` method.

###Managing operations

You can create/edit/delete operations directly from your application:

**Create operation**:

``` go
if response, err := latch.AddOperation(parentId, name, twoFactor, lockOnRequest); err == nil {
	//Get the ID of the newly created operation
	operationId := response.OperationId()
}
```
where:

* `parentId`: Id of this operation's parent (application or another operation).
* `name`: Name of the operation.
* `twoFactor`: Use of two factor authentication. One of these values:
	* OPT_IN (optional): `golatch.OPT_IN`
	* MANDATORY (mandatory): `golatch.MANDATORY`
	* DISABLED (disabled): `golatch.DISABLED`
* `lockOnRequest`: Takes the same values as twoFactor. 

**Modify operation**:
{'fraction_non_alphanumeric': 0.10513195220493424, 'fraction_numerical': 0.0, 'mean_word_length': 4.80828756864703, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '390322', 'n_tokens_mistral': 3285, 'n_tokens_neox': 3110, 'n_words': 1474}