Code Monkey home page Code Monkey logo

elm-dropbox's Introduction

Unofficial Dropbox API for Elm.

Demo

https://avh4.github.io/elm-dropbox/

Usage

The Dropbox API uses OAuth 2.0 for authentication. To authenticate the user, you will need to send them to the approrpiate authentication URL, which can be done with Dropbox.authorize. But to create the authorization URL, you will need to provide the Navigation.Location of the current page.

First, set up your Model to store the current page's location:

elm-package install elm-lang/navigation
import Navigation

type alias Model =
    { ...
    , location : Navigation.Location
    }

initialModel : Navigation.Location -> Model
initialModel location =
    { ...
    , location = location
    }

Dropbox authentication requires a "client id", also called the "app key". can be found on the Dropbox Developers page for your app. Links: your Dropbox apps, creat a new Dropbox app You will need to get the client id/app key of your existing app, or create a new app.

Now, you can create a Msg that will initiate Dropbox login:

elm-package install avh4/elm-dropbox
import Dropbox

type Msg
    = ...
    | LogInToDropbox

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
    case msg of
        ...
        LogInToDropbox ->
            ( model
            , Dropbox.authorize
                { clientId = "MY_CLIENT_ID"
                , state = Nothing
                , requireRole = Nothing
                , forceReapprove = False
                , disableSignup = False
                , locale = Nothing
                , forceReauthentication = False
                }
                model.location
            )

view : Model -> Html Msg
view model =
    ...
    Html.button
        [ Html.Events.onClick LogInToDropbox ]
        [ Html.text "Log in with Dropbox" ]

When authentication completes, Dropbox will redirect to the current page, providing the authentication details in the URL's fragment identifier . You can use Dropbox.program to automatically parse the redirect response (it also will help you get the Navigation.Location to store in your model).

import Dropbox

type Msg
    = ...
    | AuthResponse Dropbox.AuthorizeResult

main : Program Never Model (Dropbox.Msg Msg)
main =
    Dropbox.program
        { init = \location -> ( initialModel location, Cmd.none )
        , update = update
        , subscriptions = subscriptions
        , view = view
        , onAuth = AuthResponse
        }

You can now handle the auth response and take the resulting Dropbox.UserAuth and use it to make API requests. See the documentation for the full list of supported API calls. Typically you will take the Dropbox.UserAuth and store it in your model for later use. You may also want to persist it in local storage so that it can be used on future visits to your app.

import Dropbox

type alias Model =
    { ...
    , dropboxAuth : Maybe Dropbox.UserAuth
    }

type Msg
    = ...
    | FetchFileResponse (Result Dropbox.DownloadError Dropbox.DownloadResponse)

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
    case msg of
        ...
        AuthResponse (Dropbox.AuthorizeOk auth) ->
            ( { model | dropboxAuth = Just auth.userAuth }
            , Dropbox.download auth.userAuth
                { path : "/data_file_to_load.json" }
                |> Task.attempt FetchFileResponse
            )

elm-dropbox's People

Contributors

avh4 avatar

Watchers

James Cloos avatar Ilias Van Peer avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.