A music player that connects to your cloud/distributed storage.
at main 2.7 kB view raw
1module User.Layer.Methods.Dropbox exposing (..) 2 3import Common 4import Http 5import Http.Ext as Http 6import Http.Extras as Http 7import Json.Decode as Json 8import Task exposing (Task) 9import Url exposing (Url) 10 11 12 13-- 🌳 14 15 16type alias Tokens = 17 { accessToken : String 18 , expiresIn : Int -- Time in seconds the access token expires in 19 , refreshToken : Maybe String 20 } 21 22 23 24-- 🏔 25 26 27clientId : String 28clientId = 29 "te0c9pbeii8f8bw" 30 31 32clientSecret : String 33clientSecret = 34 "kxmlfdsw8k9e0ot" 35 36 37redirectUri : Url -> String 38redirectUri url = 39 Common.urlOrigin url ++ "?action=authenticate/dropbox" 40 41 42 43-- ENCODING 44 45 46tokensDecoder : Json.Decoder Tokens 47tokensDecoder = 48 Json.map3 49 (\a e r -> 50 { accessToken = a 51 , expiresIn = e 52 , refreshToken = r 53 } 54 ) 55 (Json.field "access_token" Json.string) 56 (Json.field "expires_in" Json.int) 57 (Json.string 58 |> Json.field "refresh_token" 59 |> Json.maybe 60 ) 61 62 63 64-- 🛠 65 66 67exchangeAuthCode : (Result Http.Error Tokens -> msg) -> Url -> String -> Cmd msg 68exchangeAuthCode msg url code = 69 [ ( "client_id", clientId ) 70 , ( "client_secret", clientSecret ) 71 , ( "code", code ) 72 , ( "grant_type", "authorization_code" ) 73 , ( "redirect_uri", redirectUri url ) 74 ] 75 |> Common.queryString 76 |> String.append "https://api.dropboxapi.com/oauth2/token" 77 |> (\u -> 78 { url = u 79 , body = Http.emptyBody 80 , expect = Http.expectJson msg tokensDecoder 81 } 82 ) 83 |> Http.post 84 85 86refreshAccessToken : String -> Task String Tokens 87refreshAccessToken refreshToken = 88 [ ( "client_id", clientId ) 89 , ( "client_secret", clientSecret ) 90 , ( "refresh_token", refreshToken ) 91 , ( "grant_type", "refresh_token" ) 92 ] 93 |> Common.queryString 94 |> String.append "https://api.dropboxapi.com/oauth2/token" 95 |> (\u -> 96 { method = "POST" 97 , headers = [] 98 , url = u 99 , body = Http.emptyBody 100 , resolver = 101 Http.stringResolver 102 (\resp -> 103 resp 104 |> Http.responseToString 105 |> Result.mapError Http.errorToString 106 |> Result.andThen 107 (Json.decodeString tokensDecoder 108 >> Result.mapError Json.errorToString 109 ) 110 ) 111 , timeout = Nothing 112 } 113 ) 114 |> Http.task