# Box
The Unofficial [Box SDK](https://developer.box.com/docs/overview) for Elixir.
[![CircleCI](https://circleci.com/gh/RobotsAndPencils/box-elixir.svg?style=svg&circle-token=599bc6c8ef826c6138c4dd17f374f08ca6bd526a)](https://circleci.com/gh/RobotsAndPencils/box-elixir)
## Installation
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `box` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:box, "~> 0.2.3"}
]
end
```
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/box](https://hexdocs.pm/box).
## Configuration
Box takes configuration from the application environment.
```
config :box,
client_id: "abc12",
enterprise_id: "123",
public_key_id: "abc123",
client_secret: "secret",
private_key_path: "config/key.pem",
private_key_password: "super secret"
```
## Usage
### Login
```
{:ok, auth} = BoxOAuth2.new()
```
Call login to authenticate with Box:
```
{:ok, auth} = BoxOAuth2.login(auth)
```
The access token will expire, so it is necessary to call login frequently. If the access token is still valid, login will return it as-is without reauthenticating.
Use the `auth.client` to perform actions against the API.
### Uploading a file
Upload a local file to the root folder with a given name on Box.
```
{:ok, folder} = Box.folder_from_path(auth.client, "/")
{:ok, _response} = Box.upload(auth.client, "path/to/file.ext", folder.id, "file.ext")
```
Uploading may fail with an `item_name_in_use` error.
### List files in a folder
```
{:ok, folder} = Box.folder_from_path(auth.client, "/")
{:ok, entries} = Box.folder_items(auth.client, folder.id, [:id, :name])
IO.inspect(entries)
```
### Create a shared link
To share a file:
```
{:ok, file} = Box.file_from_path(auth.client, "file.ext")
{:ok, response} = Box.create_shared_link(auth.client, file.id)
IO.puts(response.shared_link.download_url)
```
To see an existing shared link (shared_link is nil if not created):
```
{:ok, file} = Box.file_from_path(auth.client, "file.ext")
{:ok, response} = Box.file_info(auth.client, file.id)
IO.puts(response.shared_link.download_url)
```