Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  File download using  wreq. (Mike Houghton)
   2. Re:  Parsing keywords (Mike Houghton)
   3. Re:  File download using wreq. (Kostiantyn Rybnikov)
   4. Re:  File download using wreq. (Kostiantyn Rybnikov)
   5.  Adding Either around a List monad? (Mario Lang)


----------------------------------------------------------------------

Message: 1
Date: Sun, 4 Oct 2015 16:18:17 +0100
From: Mike Houghton <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] File download using  wreq.
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Hi,

Please can someone explain how,using the wreq package, I can download and save 
a binary file? 
Say the file is at the end of 
http://x.y.z/files/myfile.jpg

and it is a jpeg and no authentication is needed. 

I just want to 

1. Check that the URL is syntactically correct - flag and error if not
2. If the URL is syntactically ok then download the file using GET.
3. Check that the response code is 200 and if so save the file 
3a. if the response code is not 200 then delegate to an  error handling 
function or some simple idiomatic way of error handling.


Thanks once again.

Mike



------------------------------

Message: 2
Date: Sun, 4 Oct 2015 16:25:09 +0100
From: Mike Houghton <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Parsing keywords
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

Thanks. I?ll try that.

Mike

> On 3 Oct 2015, at 10:21, Francesco Ariis <[email protected]> wrote:
> 
> On Sat, Oct 03, 2015 at 10:12:20AM +0100, Mike Houghton wrote:
>> Hi,
>> 
>> What is the idiomatic way of parsing keywords, in a case agnostic
>> fashion, for a ?mini-language??
> 
> I lifted this from `Text.ParserCombinators.Parsec.Rfc2234` and used it
> in a project of mine (lentil).
> 
> 
>    ciString s = mapM ciChar s <?> "case insensitive string"
>        where
>              ciChar :: Char -> ParIssue Char
>              ciChar c = char (C.toLower c) <|> char (C.toUpper c)
> 
> This assumes you are using Parsec parsing library.
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



------------------------------

Message: 3
Date: Mon, 5 Oct 2015 11:18:43 +0300
From: Kostiantyn Rybnikov <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] File download using wreq.
Message-ID:
        <CAAbahfQY3Y9dLY01=R=0qtcv5c5ziavedcd3iow3g5pbnow...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi Mike,

First, if you don't know how wreq acts when it gets invalid url ? I suggest
just launching repl and checking that out. I'll show how to do that
using haskell
stack tool <https://github.com/commercialhaskell/stack>:

?  ~  stack install wreq
...
?  ~  stack ghci
Run from outside a project, using implicit global config
Using resolver: lts-3.5 from global config file:
/Users/kb/.stack/global/stack.yaml
Configuring GHCi with the following packages:
GHCi, version 7.10.2: http://www.haskell.org/ghc/  :? for help
? import Network.Wreq
? get "nonvalid"
*** Exception: InvalidUrlException "nonvalid" "Invalid URL"

You see, it throws InvalidUrlException upon request, which you can catch
and render an error.

Now, to get content, as per tutorial, just do:
? import Control.Lens
? res <- get "http://i.imgur.com/f0IKpky.png";
? res ^. responseBody
... (long binary response goes into your output) ...

That's it. Last thing I should mention ? errors which can be thrown are all
just HttpException type. Go and see possible ones
<http://hackage.haskell.org/package/http-client-0.4.24/docs/Network-HTTP-Client.html#t:HttpException>,
some of which you might want to handle specifically, while others just in a
generic "something bad happened" error.

Hope this helps.



On Sun, Oct 4, 2015 at 6:18 PM, Mike Houghton <[email protected]>
wrote:

> Hi,
>
> Please can someone explain how,using the wreq package, I can download and
> save a binary file?
> Say the file is at the end of
> http://x.y.z/files/myfile.jpg
>
> and it is a jpeg and no authentication is needed.
>
> I just want to
>
> 1. Check that the URL is syntactically correct - flag and error if not
> 2. If the URL is syntactically ok then download the file using GET.
> 3. Check that the response code is 200 and if so save the file
> 3a. if the response code is not 200 then delegate to an  error handling
> function or some simple idiomatic way of error handling.
>
>
> Thanks once again.
>
> Mike
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151005/242fdad7/attachment-0001.html>

------------------------------

Message: 4
Date: Mon, 5 Oct 2015 11:21:31 +0300
From: Kostiantyn Rybnikov <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] File download using wreq.
Message-ID:
        <CAAbahfT-KxK1W6xxnfHBCoKkE1X=-_aioqewuv_f+fd48sv...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Just few more things which might help:

? res ^. responseStatus
Status {statusCode = 200, statusMessage = "OK"}
? :t res ^. responseStatus
res ^. responseStatus :: Status
? :i Status
data Status
  = Network.HTTP.Types.Status.Status {Network.HTTP.Types.Status.statusCode
:: Int,

Network.HTTP.Types.Status.statusMessage ::
Data.ByteString.Internal.ByteString}
        -- Defined in ?Network.HTTP.Types.Status?
instance Enum Status -- Defined in ?Network.HTTP.Types.Status?
instance Eq Status -- Defined in ?Network.HTTP.Types.Status?
instance Ord Status -- Defined in ?Network.HTTP.Types.Status?
instance Show Status -- Defined in ?Network.HTTP.Types.Status?

You can see how to get the status, where it comes from. So you can just do
"if res ^. responseStatus /= status200 then ...".

Cheers.

On Sun, Oct 4, 2015 at 6:18 PM, Mike Houghton <[email protected]>
wrote:

> Hi,
>
> Please can someone explain how,using the wreq package, I can download and
> save a binary file?
> Say the file is at the end of
> http://x.y.z/files/myfile.jpg
>
> and it is a jpeg and no authentication is needed.
>
> I just want to
>
> 1. Check that the URL is syntactically correct - flag and error if not
> 2. If the URL is syntactically ok then download the file using GET.
> 3. Check that the response code is 200 and if so save the file
> 3a. if the response code is not 200 then delegate to an  error handling
> function or some simple idiomatic way of error handling.
>
>
> Thanks once again.
>
> Mike
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151005/652e0926/attachment-0001.html>

------------------------------

Message: 5
Date: Mon, 05 Oct 2015 12:06:14 +0200
From: Mario Lang <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] Adding Either around a List monad?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

Hi.

Consider this structure:

vs :: Rational -> [Input] -> [[Output]]
vs _ []     = return []
vs l (x:xs) = pms l x >>= \pm -> (pm :) <$> vs (l - dur pm) xs

pms :: Rational -> Input -> [Output]
pms l x = [x, x+1, x+2, ...] -- Just an example, not real code.
                             -- in reality, l is used to determine
                             -- the result of pms.

This is basically traverse, but with a state (l) added to it.
So without the state, vs could be written as

vs = traverse pms

Now, I want to add Either e to this, like:

vs :: Rational -> [Input] -> Either e [[Output]]
pms :: Rational -> Input -> Either e [Output]

However, I have no idea how to implement vs.

Interestingly, adding Either e to vs without changing the code lets it
compile, but it gives me the wrong result:

vs :: Rational -> [Input] -> Either e [[Output]]
vs _ []     = return []
vs l (x:xs) = pms l x >>= \pm -> (pm :) <$> vs (l - pm) xs

Since I am in the Either monad now, >>= does not do non-determinism, it
simply unwraps the Either from pms.  I have to admit, I dont fully
understand why this compiles, and what exactly it does wrong.  I only
see from testing that the results can't be right.

On IRC, Gurkenglas suggested to use the State monad, like this:

vs :: Rational -> [Input] -> Either e [[Output]]
vs l = `evalStateT l` . mapM v where
  v x = do l <- get
           pm <- lift $ pms l x
           put (l - dur pm)
           return pm

This compiles, but also yields unexpected results.

I have invested several hours now trying to add Either around this
algorithm, so that I can emit hard failures.  I am sort of frustrated
and out of ideas.  Somehow, I can't figure out what these
transformations actually change in behaviour.  I am being told, by quite
experienced Haskell programmers, that this is supposed to be correct,
but my testing tells me otherwise.  So before I just give up on this,
could someone please have a look and let me know if I have missed
something obvious?

-- 
CYa,
  ?????


------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 88, Issue 3
****************************************

Reply via email to