[elm-discuss] Re: Http 401 Interceptor? My 3rd Elm Project - authentication

2016-09-16 Thread Erik Lott
No, our pages make their own HTTP requests from within the page, and do not 
delegate the top layer to make requests. The majority of our api requests 
are done from within pages, and I think that's likely the case for most 
apps. Each of our "pages" manage their own http errors through the use of a 
reusable view module that displays errors in a popup modal. We COULD pass 
401 errors upwards to the top layer to initiate a re-route to login, but we 
don't - If a 401 hasn't been caught during the initial transition to a 
page, it's unlikely that one is going to occur during the lifetime of a 
single page. If a 401 does occur during a page api request, it's simple to 
display a "please login" message in the  popup error modal.

If you absolutely need to communicate all 401's upwards, just make that 
part of the API of your page modules - that will involve sending an 
'OutMsg' from your page's "update" function.

Happy to explain further if anything is unclear.

On Friday, September 16, 2016 at 6:17:24 PM UTC-4, Rupert Smith wrote:
>
> On Friday, September 16, 2016 at 6:07:42 PM UTC+1, Erik Lott wrote:
>>
>> Rupert, here is a high level overview of how we currently structure our 
>> elm SPAs:
>>
>>1. Top layer: This layer manages routing, page changes, and page 
>>resource loading, and current user state
>>2. Page layer: These are individual pages - typically one page for 
>>each url. There may be deeper levels of widgets/views within each 
>>page, but we don't have pages nested within pages.
>>
>>
>> When the top layer is notified of a page change (this mechanism can vary 
>> from app to app), it first communicates with the backend server to load any 
>> resources that are needed to display that page. If the resource load 
>> succeeds, the page is displayed by changing the 'currentRoute' to the new 
>> routing state, and providing the loaded resources to the new page. If the 
>> load fails, however, you can check the http error to determine why. If 
>> you're received a 401 failure, you can quickly change to your "login route" 
>> and ask the user to login.
>>
>> Keep in mind that method allows you to catch and mange http errors that 
>> occur during page transitions, and not from http requests made from within 
>> a page. We're fine with that tradeoff, because 99% of the time, you'll only 
>> need to catch 401 during page transitions anyways.
>>
>> Hopefully that helps.
>>
>
> Very enlightening thanks.
>
> What about POSTs from pages? For example, I have a page which shows user 
> accounts (to an admin), and will also allow a new user account to be 
> created. It would seem natural to POST the new account from within that 
> page. Or do you do that from the top-level too? 
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Re: Json.Decode list of objects without known fields

2016-09-16 Thread Oliver Searle-Barnes
You can do this with a Dict but it's a little fiddly because you need to 
use an ADT to represent the different types of the values

import Html
import Json.Decode as JD
import Dict
import String


json : String
json =
 """
 [{"a": 1, "foo": "bar", "bar": "baz"},
 {"a": 2, "foo": "baz", "bar": "foo"},
 {"a": 34, "foo": "big", "bar": "lebowski"}]
 """


type ValueType = StringType String | IntType Int


valueTypeDecoder =
  JD.oneOf
[ JD.map StringType JD.string
, JD.map IntType JD.int
]


gen_keys : String -> String
gen_keys json =
json
|> JD.decodeString (JD.list <| JD.dict valueTypeDecoder)
|> Result.toMaybe
|> flip Maybe.andThen List.head
|> Maybe.map Dict.keys
|> Maybe.withDefault []
|> String.join ","


main : Html.Html string
main =
Html.text (gen_keys json)


On Saturday, 17 September 2016 00:52:20 UTC+2, Gary Young wrote:
>
> In short.  Solve this:
>
> module Main exposing (..)
>
> import Html
>
>
> -- import Json.Decode exposing (Decoder, decodeString, keyValuePairs)
>
>
> json : String
> json =
> """[{a: 1, foo: "bar", bar: "baz"},
>  {a: 2, foo: "baz", bar: "foo"},
>  {a: 34, foo: "big", bar: "lebowski"}]"""
>
>
> gen_keys : String -> String
> gen_keys json =
> -- Decode the first JSON record and return a comma separated
> -- list of keys
> json
>
>
> main : Html.Html string
> main =
> Html.text (gen_keys json)
>
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] problem on composition and communication of modules

2016-09-16 Thread Matteo
Hello everybody, I have a problem and I can't find a solution.

The code consist in 3 modules. *Main*, *ItemList* and *Form*

The *Main* is the glue and message broker of the app. The other two are a 
list and a form to update the list.

The list has it's logic to fetch the results from an api, with all it's 
internal events that gets routed through the update function of *Main* with 
Cmd.map.
The form is exactly the same. With internal events to manage its state and 
an add button to trigger an HTTP POST request to update the database.

Everything is great and separated. But the problem is the "ADD" button. 
When I press it, I want to perform a post, and on the form event 
UpdateSucceed I want to update the list!

Here is the relevant code of Main.elm

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
FormMsg formMsg ->
let
( updatedItem, formCmd ) =
Form.update formMsg model.newItem
in
( { model | newItem = updatedItem }
, Cmd.map FormMsg formCmd
)


ItemListMsg itemListMsg ->
let
( updatedList, itemListCmd ) =
ItemList.update itemListMsg model.items
in
( { model | items = updatedList }
, Cmd.map ItemListMsg itemListCmd
)

here is the update function of Form.elm

update : Msg -> Item -> ( Item, Cmd Msg )
update msg model =
case msg of
...


Add ->
( model, doAddItem model )


UpdateSucceed newItem ->
( initialModel, Cmd.none )

I searched through the posts and everybody is suggesting to change the 
signature of the Form module update function, and return a third element, 
the outgoing message. I could go for it, but I really struggling to 
understand how to handle it in the Main update function, because one 
command is handled by the elm runtime on every message that arrives to 
update. I don't understand where I should manage this 2 different things to 
do when UpdateSucceed is triggered on the form. Re initialize the model in 
the form (clear the fields) and trigger an event that in some way gets 
routed through the Main module to the ItemList module.

Any help is appreciated, this is a personal project, so I'm eager to learn 
the most elegant way to handle this situation, and refactoring is not a 
problem.

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Re: Json.Decode list of objects without known fields

2016-09-16 Thread Gary Young
In short.  Solve this:

module Main exposing (..)

import Html


-- import Json.Decode exposing (Decoder, decodeString, keyValuePairs)


json : String
json =
"""[{a: 1, foo: "bar", bar: "baz"},
 {a: 2, foo: "baz", bar: "foo"},
 {a: 34, foo: "big", bar: "lebowski"}]"""


gen_keys : String -> String
gen_keys json =
-- Decode the first JSON record and return a comma separated
-- list of keys
json


main : Html.Html string
main =
Html.text (gen_keys json)

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Json.Decode list of objects without known fields

2016-09-16 Thread Gary Young
I love Elm and I strongly believe that it's the future, but I can't solve 
this one use-case that's stopping me from using it.  

I have a list of JSON objects like this:

[{a: 1, foo: "bar", bar: "baz"},
 {a: 2, foo: "baz", bar: "foo"},
 {a: 34, foo: "big", bar: "lebowski"}]

At runtime the code has no idea the types of the fields, the names of the 
fields, the number of fields, nor the number of records.

I can decode this easily in Javascript, Coffeescript, etc. How do I decode 
it in elm?


I produce reports in Javascript by pivoting the data. I do not know at 
compile time what the field names are. In Javascript I can simply do the 
following to find the relevant field names:


var foo = JSON.parse(string);
var keys = [];
for(var k in foo[0]) keys.push(k);


then later I can iterate through the records and pull out the relevant data 
doing something like so:


for(var i=0;i

[elm-discuss] Re: Http 401 Interceptor? My 3rd Elm Project - authentication

2016-09-16 Thread 'Rupert Smith' via Elm Discuss
On Friday, September 16, 2016 at 6:07:42 PM UTC+1, Erik Lott wrote:
>
> Rupert, here is a high level overview of how we currently structure our 
> elm SPAs:
>
>1. Top layer: This layer manages routing, page changes, and page 
>resource loading, and current user state
>2. Page layer: These are individual pages - typically one page for 
>each url. There may be deeper levels of widgets/views within each 
>page, but we don't have pages nested within pages.
>
>
> When the top layer is notified of a page change (this mechanism can vary 
> from app to app), it first communicates with the backend server to load any 
> resources that are needed to display that page. If the resource load 
> succeeds, the page is displayed by changing the 'currentRoute' to the new 
> routing state, and providing the loaded resources to the new page. If the 
> load fails, however, you can check the http error to determine why. If 
> you're received a 401 failure, you can quickly change to your "login route" 
> and ask the user to login.
>
> Keep in mind that method allows you to catch and mange http errors that 
> occur during page transitions, and not from http requests made from within 
> a page. We're fine with that tradeoff, because 99% of the time, you'll only 
> need to catch 401 during page transitions anyways.
>
> Hopefully that helps.
>

Very enlightening thanks.

What about POSTs from pages? For example, I have a page which shows user 
accounts (to an admin), and will also allow a new user account to be 
created. It would seem natural to POST the new account from within that 
page. Or do you do that from the top-level too? 

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Getting started with the serial port (in Electron)

2016-09-16 Thread Duane Johnson
Ian, FYI new members to this mailing list have to have their first message
approved by a moderator due to an excess of unsavory spam. I saw that you
send 3 or 4 similar messages. I approved the first one and removed the
others. You should be able to post future messages without a moderation
step here on out.

On Sep 16, 2016 3:53 PM, "Ian"  wrote:

> Hi. I'm looking to build a custom serial port terminal program. It'll
> receive a binary format that needs to be made human readable (and plotted).
>
> It would be awesome if I could write my app in Electron, using Elm.
>
> Besides giving me a framework (so I can avoid React and Angular), it seems
> Elm can also help me decode the protocol in a uniquely concise way.
>
> First things first: how do I access the node serial port (
> https://www.npmjs.com/package/serialport) in Elm?
>
> Looking forward to get started in Elm!
>
> Ian
>
>
>
> PS In the days before Elm 0.17 was released I could sense an excitement in
> the air (and I had no clues about what was to happen, no contact). In
> contrast with this excitement I have the feeling the 0.17 release may be
> seen by the public as a step back: in the eyes of the feeble Elm became a
> framework instead of a language. In other words Elm became less powerful
> and a special purpose tool. I hope to prove myself wrong :)
>
>
>
>
> Some links that might be useful:
>
> - github.com/rwaldron/johnny-five is an Arduino based Robotics platform
>
> - The barebones to using Elm with Electron: https://medium.com/@ezekeal/
> building-an-electron-app-with-elm-part-1-boilerplate-
> 3416a730731f#.9jipph16u
>
> - This may be an even better resource: http://www.gizra.com/content/
> elm-electron-build/
>
>
> Finally, this page has some code showing the kind of serial port code
> needed:
>
>  - http://adilmoujahid.com/posts/2015/07/practical-
> introduction-iot-arduino-nodejs-plotly/
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Elm Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to elm-discuss+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Json Decode Issue

2016-09-16 Thread Janis Voigtländer
Your type "Players" is recursive in a way you almost certainly did not intend. 
That's what leads to this mismatch. 

> Am 16.09.2016 um 04:09 schrieb Mohammad Samman :
> 
> I have the following elm code:
> 
> import Json.Decode as Json exposing(..)
> import Result
> 
> type Players = Players (List Players)
> type alias Player = {
>   name: String
>   , team: Maybe Team
> }
> 
> type alias Team = {
>   name: String
>   , players: Maybe Players
> }
> 
> teamDecoder: Json.Decoder Team
> teamDecoder =
>   Json.object2 Team ("name" := Json.string) (Json.maybe ("players" := 
> playersDecoder))
> 
> playersDecoder: Json.Decoder Players
> playersDecoder =
>   Json.customDecoder (Json.list playerDecoder) (\pl ->
> Result.Ok (Players pl)
>   )
> 
> playerDecoder: Json.Decoder Player
> playerDecoder = 
>   Json.object2 Player ("name" := Json.string) (Json.maybe ("team" := 
> teamDecoder))
> 
> Compiling give me the following error
> 
> -- TYPE MISMATCH 
> ---
> The 2nd argument to function `customDecoder` is causing a mismatch.
> 26|>  Json.customDecoder (Json.list playerDecoder) (\pl ->
> 27|>Result.Ok (Players pl)
> Function `customDecoder` is expecting the 2nd argument to be:
> List { name : String, team : Maybe Team } -> Result String a
> But it is:
> List Players -> Result a Players
> Hint: I always figure out the type of arguments from left to right. If an
> argument is acceptable when I check it, I assume it is "correct" in subsequent
> checks. So the problem may actually be in how previous arguments interact with
> the 2nd.
> 
> I'm wondering why this is the case? Shouldn't the pl variable inside of the 
> anon func be a List Players based on the type signature of 
> Json.customDecoder? How can I decode into the Team type alias?
> 
> Thanks!
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Elm Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to elm-discuss+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Json Decode Issue

2016-09-16 Thread Mohammad Samman
I have the following elm code:

import Json.Decode as Json exposing(..)
import Result

type Players = Players (List Players)
type alias Player = {
  name: String
  , team: Maybe Team
}

type alias Team = {
  name: String
  , players: Maybe Players
}

teamDecoder: Json.Decoder Team
teamDecoder =
  Json.object2 Team ("name" := Json.string) (Json.maybe ("players" := 
playersDecoder))

playersDecoder: Json.Decoder Players
playersDecoder =
  Json.customDecoder (Json.list playerDecoder) (\pl ->
Result.Ok (Players pl)
  )

playerDecoder: Json.Decoder Player
playerDecoder = 
  Json.object2 Player ("name" := Json.string) (Json.maybe ("team" := 
teamDecoder))


Compiling give me the following error


-- TYPE MISMATCH 
--- The 2nd 
argument to function `customDecoder` is causing a mismatch. 26|> 
Json.customDecoder (Json.list playerDecoder) (\pl ->
27|> Result.Ok (Players pl) Function `customDecoder` is expecting the 2nd 
argument to be: List { name : String, team : Maybe Team } -> Result String a 
But 
it is: List Players -> Result a Players Hint: I always figure out the type 
of arguments from left to right. If an
argument is acceptable when I check it, I assume it is "correct" in 
subsequent
checks. So the problem may actually be in how previous arguments interact 
with
the 2nd.

I'm wondering why this is the case? Shouldn't the pl variable inside of the 
anon func be a List Players based on the type signature of 
Json.customDecoder? How can I decode into the Team type alias?

Thanks!

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Getting started with the serial port (in Electron)

2016-09-16 Thread Ian
Hi. I'm looking to build a custom serial port terminal program. It'll 
receive a binary format that needs to be made human readable (and plotted). 

It would be awesome if I could write my app in Electron, using Elm. 

Besides giving me a framework (so I can avoid React and Angular), it seems 
Elm can also help me decode the protocol in a uniquely concise way. 

First things first: how do I access the node serial port 
(https://www.npmjs.com/package/serialport) in Elm? 

Looking forward to get started in Elm!

Ian



PS In the days before Elm 0.17 was released I could sense an excitement in 
the air (and I had no clues about what was to happen, no contact). In 
contrast with this excitement I have the feeling the 0.17 release may be 
seen by the public as a step back: in the eyes of the feeble Elm became a 
framework instead of a language. In other words Elm became less powerful 
and a special purpose tool. I hope to prove myself wrong :)




Some links that might be useful:

- github.com/rwaldron/johnny-five is an Arduino based Robotics platform

- The barebones to using Elm with Electron: 
https://medium.com/@ezekeal/building-an-electron-app-with-elm-part-1-boilerplate-3416a730731f#.9jipph16u

- This may be an even better resource: 
http://www.gizra.com/content/elm-electron-build/  


Finally, this page has some code showing the kind of serial port code 
needed:

 - 
http://adilmoujahid.com/posts/2015/07/practical-introduction-iot-arduino-nodejs-plotly/



-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Re: Http 401 Interceptor? My 3rd Elm Project - authentication

2016-09-16 Thread 'Rupert Smith' via Elm Discuss
On Friday, September 16, 2016 at 4:00:39 PM UTC+1, Rupert Smith wrote:
>
>
> I just need to think about how to add functions to query the auth state on 
> this module too, as it would also be nice to be able to query that all over 
> the place too. Something like
>

So to solve this problem I came up with a record type for the current state:

type alias AuthState =
{ loggedIn : Bool
, permissions : List String
}

Which is part of the Auth model which is kept in the top-level state. Then 
I just parameterize any views that need to use it:

view : Auth.Types.AuthState -> Model -> Html Msg 

and instantiate them accrodingly. Its really only the view where I need to 
know the current logged in state, and what permissions a user may have, in 
order to decide what to show.

That should wrap things up for now. I'll put the code for this one up on 
github soon. I don't know if this is TEA or not, but it seems to work and 
feels fairly workable and gives me what I need.

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Re: Http 401 Interceptor? My 3rd Elm Project - authentication

2016-09-16 Thread 'Rupert Smith' via Elm Discuss
On Friday, September 16, 2016 at 2:49:25 PM UTC+1, Rupert Smith wrote:
>
> I think I am going to try the solution from the "Inter-Component 
> Communication in 0.17" thread. Which is to create pairs of messaging ports.
>

The module I came with is below. I can import it anywhere and make use of 
these auth functions - this will come in handy for setting the not authed 
state on any http request that fails with a 401.

I just need to think about how to add functions to query the auth state on 
this module too, as it would also be nice to be able to query that all over 
the place too. Something like

isLoggedIn : Bool
hasPermission : String -> Bool

would cover my needs. Anyway, here is the Auth module:

port module Auth exposing (..)


type alias Credentials =
{ username : String
, password : String
}


login : Credentials -> Cmd msg
login authRequest =
sendLogin authRequest


logout : Cmd msg
logout =
sendLogout ()


unauthed : Cmd msg
unauthed =
sendUnauthed ()


port sendLogin : Credentials -> Cmd msg


port sendLogout : () -> Cmd msg


port sendUnauthed : () -> Cmd msg

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Can you have a port that takes no args? What is its type?

2016-09-16 Thread 'Rupert Smith' via Elm Discuss

On Friday, September 16, 2016 at 3:17:25 PM UTC+1, Joey Eremondi wrote:
>
> If you need this, make it take an arg of type (), which is just a 
> placeholder (0 element tuple to be precise, carries no data).
>
> port logout : () -> Cmd msg
>

Aha. Thanks.
 

>
> On Fri, Sep 16, 2016 at 7:13 AM, 'Rupert Smith' via Elm Discuss <
> elm-d...@googlegroups.com > wrote:
>
>> port logout : Cmd msg
>>
>> yields:
>>
>> 13| port logout : Cmd msg
>> ^
>> You are saying it should be:
>>
>> Platform.Cmd.Cmd msg
>>
>> But you need to use the particular format described here:
>>  Use --force to continue.
>>
>> But http://guide.elm-lang.org/effect_managers/ sheds no light.
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Elm Discuss" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to elm-discuss...@googlegroups.com .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Can you have a port that takes no args? What is its type?

2016-09-16 Thread Joey Eremondi
If you need this, make it take an arg of type (), which is just a
placeholder (0 element tuple to be precise, carries no data).

port logout : () -> Cmd msg

On Fri, Sep 16, 2016 at 7:13 AM, 'Rupert Smith' via Elm Discuss <
elm-discuss@googlegroups.com> wrote:

> port logout : Cmd msg
>
> yields:
>
> 13| port logout : Cmd msg
> ^
> You are saying it should be:
>
> Platform.Cmd.Cmd msg
>
> But you need to use the particular format described here:
>  Use --force to continue.
>
> But http://guide.elm-lang.org/effect_managers/ sheds no light.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Elm Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to elm-discuss+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Can you have a port that takes no args? What is its type?

2016-09-16 Thread 'Rupert Smith' via Elm Discuss
port logout : Cmd msg

yields:

13| port logout : Cmd msg
^
You are saying it should be:

Platform.Cmd.Cmd msg

But you need to use the particular format described here:
 Use --force to continue.

But http://guide.elm-lang.org/effect_managers/ sheds no light.

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Re: Http 401 Interceptor? My 3rd Elm Project - authentication

2016-09-16 Thread 'Rupert Smith' via Elm Discuss
On Friday, September 16, 2016 at 11:45:44 AM UTC+1, Rupert Smith wrote:
>
>
> On Friday, September 16, 2016 at 10:13:01 AM UTC+1, Rupert Smith wrote:
>>
>> On Friday, September 16, 2016 at 1:32:06 AM UTC+1, art yerkes wrote:
>>>
>>> If your login page can send a message to a parent frame, you can use 
>>> Dom.LowLevel.onWindow to receive it in elm.  This appeared in 0.17 and is 
>>> neat.
>>>
>>
I think I am going to try the solution from the "Inter-Component 
Communication in 0.17" thread. Which is to create a pairs of messaging 
ports. One end of the pair will reside privately inside the Auth module, 
and will receive requests to perform these auth operations defined in 
another port module to be imported wherever it is needed:

login : AuthRequest -> Cmd msg
logout : Cmd msg -- to logout on the server
notauthorized : Cmd msg -- to set state to logged out whenever 401

Then by importing that port module anywhere in the application can reach 
for these global actions. They are all fire and forget actions, if they 
work then the log in state will flip and there is another function to query 
that.


-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Re: Best [ractices for listening to EventSource (Server-Sent Events)

2016-09-16 Thread Maxwell Gurewitz
You can use the elm websockets library, or ports.

On Wednesday, September 14, 2016 at 3:16:10 AM UTC-7, Erwan Queffélec wrote:
>
> Hi,
>
> I'm trying to find the cleanest possible way of subscribing and receiving 
> server side events (SSE) (
> http://www.html5rocks.com/en/tutorials/eventsource/basics/)
>
> So far I found this 
> https://stackoverflow.com/questions/35144530/how-to-capture-server-events-in-elm
>
> And this (code contributed by the SO answer author): 
> https://github.com/lukewestby/elm-http-event-source
>
> Is there any built-in way of doing this with elm yet ? Is that somewhere 
> on the roadmap ?
>
> Regards,
>
> Erwan
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Re: Http 401 Interceptor? My 3rd Elm Project - authentication

2016-09-16 Thread 'Rupert Smith' via Elm Discuss

On Friday, September 16, 2016 at 10:13:01 AM UTC+1, Rupert Smith wrote:
>
> On Friday, September 16, 2016 at 1:32:06 AM UTC+1, art yerkes wrote:
>>
>> If your login page can send a message to a parent frame, you can use 
>> Dom.LowLevel.onWindow to receive it in elm.  This appeared in 0.17 and is 
>> neat.
>>
>
Trying to search github for usages of onWindow, onDocument, sendToAp, but 
its difficult as so many repos check in elm-stuff, so you end up with 
hundreds of hits on the elm core sources. Searches like "language:elm 
onWindow !elm-stuff/*" don't work to exclude elm-stuff.

Anyway, the only usages of these functions I can find are on effects 
modules, I think perhaps this is not what I am after?

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Best [ractices for listening to EventSource (Server-Sent Events)

2016-09-16 Thread Erwan Queffélec
Thanks for your answers. I finally implemented SSE using ports. Works like
a charm and was actually quite trivial to code.

I would still love to see this implemented directly into an elm
package/core package, I'll follow Joaquin's advice and make the proposal to
elm-dev



On Wed, Sep 14, 2016 at 11:11 PM, Joaquín Oltra 
wrote:

>
> I'd recommend making the proposal with concrete use cases in elm-dev to be
> discussed, but then move on and use ports and get the job done with
> JavaScript.
>
> Native is mostly undocumented and apparently discouraged.
>
> Elm is (very) good for what it has been designed, but it's design process
> starts from specific use cases and it takes time.
>
> ---
>
> Personally I'd like to see some kind of promise/task port, since most of
> the times I send to JS I want to listen for a result/error coupled to that
> submission (like making a jsonp call), and promises/tasks ports would make
> that kind of interop feel a lot more natural in Elm.
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Elm Discuss" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/
> topic/elm-discuss/ahvErQ1Mh-o/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> elm-discuss+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Re: Http 401 Interceptor? My 3rd Elm Project - authentication

2016-09-16 Thread 'Rupert Smith' via Elm Discuss
On Friday, September 16, 2016 at 1:32:06 AM UTC+1, art yerkes wrote:
>
> HttpError has BadResponse Int String which you can use to detect non-200 
> results to http requests.  The way to do it in elm is to write a function 
> that wraps the creation of login enabled http requests and translates 401 
> errors into a suitable message (which is why Task.perform has two function 
> arguments: one for errors, one for successes).  As long as your code uses 
> the wrapper, you'll consistently translate errors into useful messages.
>

Very useful, thanks. I was thinking I would have to write the 401 handling 
code on every request - but with a wrapper function that can be done just 
once and put in place with the wrapper.

I still have to put the wrapper on all requests that need it, rather than 
just once in a global interceptor, but that actually sounds like a better 
approach to me.
 

>
> You can use elm-navigation to "save your place" to a degree when 
> redirecting to a separate page and back by storing some state in the url's 
> fragment. (I haven't tried it, but newUrl in this library seems like it'd 
> support redirection too).  You can of course get much fancier.
>

Yes, I kind of discovered this by accident. When not logged in, I show the 
login page, but did not change the url - so once login completes the 
original page comes back again.
 

> If your login page can send a message to a parent frame, you can use 
> Dom.LowLevel.onWindow to receive it in elm.  This appeared in 0.17 and is 
> neat.
>
> In my elm code, I separated out the login http handler with its own Model, 
> init and update functions and delegated handling of login related messages 
> to it via elm architecture style message wrapping, but you don't need to 
> get that fancy unless it makes your code more readable to do so.
>

This is also what I am trying to do. Interaction with the 'auth' module is 
needed from many places in the application - the login page (login), the 
top-level view (isloggedIn), the logout button wherever that lives 
(logout), everywhere that makes http requests (on 401 - setLoggedOut). It 
would be nice to capture all these actions in a module in such a way that 
invoking them from anywhere within the application is easy to do.

If I was doing this in Java, I would write an interface, and pass its 
implementation around wherever it is needed:

public interface AuthHandler {
  boolean isLoggedIn();
  void login(String username, String password);
  void logout();
  void setLoggedOut();
}

Is your code somewhere I can look see?


> On Thursday, September 15, 2016 at 9:27:52 AM UTC-7, Rupert Smith wrote:
>>
>> My next project is delving much deeper into Elm - until till now I mostly 
>> just played around with the view and a little bit of glue to stick it all 
>> together.
>>
>> The code I am porting to Elm is an Angular project that has a fairly 
>> sophisticated way of handling logins. When a resource is accessed on the 
>> server and the user is not logged in, a 401 Unauthorized is sent back. 
>> There was a way in Angular to intercept this, pop-up the log in dialog, and 
>> upon succesful completion of the log in, the request that failed is 
>> replayed. I was getting a bit fancy with that, and don't necessarily need 
>> to do it exactly that way in Elm. What would do for now is 401 -> redirect 
>> to login page -> come back to some starting page for the application.
>>
>> Is there some way in Elm to set up a global HTTP interceptor to look out 
>> for 401s?
>>
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.