Error attempting to decode with wreq - http
I'm trying really hard to understand how to use lenses and wreq and its turning out to really slow me down.
The error seems to be claiming there's some mismatched type here. I'm not sure exactly how to handle that though. I'm still fairly new to haskell and these lenses are pretty confusing. However, wreq seems to be cleaner, which is why I chose to use it. Can anyone help me understand what the error is, and how to fix it? I seem to run into alot of these type errors. I am aware that Maybe TestInfo won't be returned by my code at the moment. That's ok. That error know how to handle. This error however, I don't.
Here is my code:
Module TestInformation:
{-# LANGUAGE OverloadedStrings #-}
module TestInformation where
import Auth
import Network.Wreq
import Control.Lens
import Data.Aeson
import Data.Aeson.Lens (_String)
type TestNumber = String
data TestInfo = TestInfo {
TestId :: Int,
TestName :: String,
}
instance FromJSON TestInfo
getTestInfo :: Key -> TestNumber -> Maybe TestInfo
getTestInfo key test =
decode (res ^. responseBody . _String)
where opts = defaults & auth ?~ oauth2Bearer key
res = getWith opts ("http://testsite.com/v1/tests/" ++ test)
Module Auth:
module Auth where
import qualified Data.ByteString as B
type Key = B.ByteString
The error:
GHCi, version 7.10.1: http://www.haskell.org/ghc/ :? for help
[1 of 2] Compiling Auth ( Auth.hs, interpreted )
[2 of 2] Compiling TestInformation ( TestInformation.hs, interpreted )
TestInformation.hs:36:18:
Couldn't match type ‘Response body10’
with ‘IO (Response Data.ByteString.Lazy.Internal.ByteString)’
Expected type: (body10
-> Const Data.ByteString.Lazy.Internal.ByteString body10)
-> IO (Response Data.ByteString.Lazy.Internal.ByteString)
-> Const
Data.ByteString.Lazy.Internal.ByteString
(IO (Response Data.ByteString.Lazy.Internal.ByteString))
Actual type: (body10
-> Const Data.ByteString.Lazy.Internal.ByteString body10)
-> Response body10
-> Const Data.ByteString.Lazy.Internal.ByteString (Response body10)
In the first argument of ‘(.)’, namely ‘responseBody’
In the second argument of ‘(^.)’, namely ‘responseBody . _String’
TestInformation.hs:36:33:
Couldn't match type ‘Data.ByteString.Lazy.Internal.ByteString’
with ‘Data.Text.Internal.Text’
Expected type: (Data.ByteString.Lazy.Internal.ByteString
-> Const
Data.ByteString.Lazy.Internal.ByteString
Data.ByteString.Lazy.Internal.ByteString)
-> body10 -> Const Data.ByteString.Lazy.Internal.ByteString body10
Actual type: (Data.Text.Internal.Text
-> Const
Data.ByteString.Lazy.Internal.ByteString Data.Text.Internal.Text)
-> body10 -> Const Data.ByteString.Lazy.Internal.ByteString body10
In the second argument of ‘(.)’, namely ‘_String’
In the second argument of ‘(^.)’, namely ‘responseBody . _String’
Failed, modules loaded: Auth.
Leaving GHCi.
This type checks for me:
getTestInfo :: Key -> TestNumber -> IO (Maybe TestInfo)
getTestInfo key test = do
res <- getWith opts ("http://testsite.com/v1/tests/" ++ test)
return $ decode (res ^. responseBody)
where opts = defaults & auth ?~ oauth2Bearer key
getWith is an IO action, so to get its return value you need to use the monadic binding operator <-.
Full program: http://lpaste.net/133443 http://lpaste.net/133498
Related
Error: Could not find main or io in tokio, invalid return type `impl Future`
I'm on my way of converting to Rust from the ML family, but I'm finding it hard at some strange places I'm not used to having problems. I'm trying to use hyper for http handling but can't seem to get tokio to work. I have tried to copy paste this example: use hyper::{body::HttpBody as _, Client}; use tokio::io::{self, AsyncWriteExt as _}; type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>; #[tokio::main] async fn main() -> Result<()> { // ... fetch_url(url).await } async fn fetch_url(url: hyper::Uri) -> Result<()> { // ... Ok(()) } Here is my Cargo.toml: [package] name = "projectname" version = "0.1.0" authors = ["username"] edition = "2018" [dependencies] hyper = "0.14.4" tokio = "1.2.0" It is complaining that it can't find the io crate, and that main has an invalid type impl Future, and that it can't find main in tokio: error[E0433]: failed to resolve: could not find `main` in `tokio` --> src/main.rs:9:10 | 9 | #[tokio::main] | ^^^^ could not find `main` in `tokio` error[E0277]: `main` has invalid return type `impl Future` --> src/main.rs:10:20 | 10 | async fn main() -> Result<()> { | ^^^^^^^^^^ `main` can only return types that implement `Termination` error[E0432]: unresolved import `hyper::Client` --> src/main.rs:3:34 | 3 | use hyper::{body::HttpBody as _, Client}; | ^^^^^^ no `Client` in the root error[E0425]: cannot find function `stdout` in module `io` --> src/main.rs:45:13 | 45 | io::stdout().write_all(&chunk).await?; | ^^^^^^ not found in `io` | error[E0432]: unresolved import `tokio::io::AsyncWriteExt` --> src/main.rs:4:23 | 4 | use tokio::io::{self, AsyncWriteExt as _}; | -------------^^^^^ | | | no `AsyncWriteExt` in `io` | help: a similar name exists in the module: `AsyncWrite` Is #[tokio::main] and client not in hyper?
The tokio::main macro converts an async main to a regular main that spawns a runtime. However, because the macro is not found is scope, it cannot transform your main function, and the compiler is complaining that your main has an invalid return type of impl Future. To fix this, you have to enable the required features to import the main macro: tokio = { version = "1.2.0", features = ["rt", "macros"] } You also have to enable the io-util feature to access io::AsyncWriteExt, and the io-std feature to access io::stdout. To simplify this, tokio provides the full feature flag, which will enable all optional features: tokio = { version = "1.2.0", features = ["full"] } You also need hyper's client and http feature flags to resolve the Client import: hyper = { version = "0.14.4", features = ["client", "http1", "http2"] }
Encoding problem with GET requests in Haskell
I'm trying to get some Json data from a Jira server using Haskell. I'm counting this as "me having problems with Haskell" rather than encodings or Jira because my problem is when doing this in Haskell. The problem occurs when the URL (or query) has plus signs. After building my request for theproject+order+by+created, Haskell prints it as: Request { host = "myjiraserver.com" port = 443 secure = True requestHeaders = [("Content-Type","application/json"),("Authorization","<REDACTED>")] path = "/jira/rest/api/2/search" queryString = "?jql=project%3Dtheproject%2Border%2Bby%2Bcreated" method = "GET" proxy = Nothing rawBody = False redirectCount = 10 responseTimeout = ResponseTimeoutDefault requestVersion = HTTP/1.1 } But the request fails with this response: - 'Error in the JQL Query: The character ''+'' is a reserved JQL character. You must enclose it in a string or use the escape ''\u002b'' instead. (line 1, character 21)' So it seems like Jira didn't like Haskell's %2B. Do you have any suggestions on what I can do to fix this, or any resources that might be helpful? The same request sans the +order+by+created part is successful. The code (patched together from these examples): {-# LANGUAGE OverloadedStrings #-} import Data.Aeson import qualified Data.ByteString.Char8 as S8 import qualified Data.Yaml as Yaml import Network.HTTP.Simple import System.Environment (getArgs) -- auth' is echo -e "username:passwd" | base64 foo urlBase proj' auth' = do let proj = S8.pack (proj' ++ "+order+by+created") auth = S8.pack auth' request'' <- parseRequest urlBase let request' = setRequestMethod "GET" $ setRequestPath "/jira/rest/api/2/search" $ setRequestHeader "Content-Type" ["application/json"] $ request'' request = setRequestQueryString [("jql", Just (S8.append "project=" proj))] $ setRequestHeader "Authorization" [S8.append "Basic " auth] $ request' return request main :: IO () main = do args <- getArgs case args of (urlBase:proj:auth:_) -> do request <- foo urlBase proj auth putStrLn $ show request response <- httpJSON request S8.putStrLn $ Yaml.encode (getResponseBody response :: Value) -- apparently this is required putStrLn "" _ -> putStrLn "usage..." (If you know a simpler way to do the above then I'd take such suggestions as well, I'm just trying to do something analogous to this Python: import requests import sys if len(sys.argv) >= 4: urlBase = sys.argv[1] proj = sys.argv[2] auth = sys.argv[3] urlBase += "/jira/rest/api/2/search?jql=project=" proj += "+order+by+created" h = {} h["content-type"] = "application/json" h["authorization"] = "Basic " + auth r = requests.get(urlBase + proj, headers=h) print(r.json()) )
project+order+by+created is the URL-encoded string for the actual request project order by created (with spaces instead of +). The function setRequestQueryString expects a raw request (with spaces, not URL-encoded), and URL-encodes it. The Python script you give for comparison essentially does the URL-encoding by hand. So the fix is to put the raw request in proj: foo urlBase proj' auth' = do let proj = S8.pack (proj' ++ " order by created") -- spaces instead of + ...
Ecto.Repo receives a struct that does not implement Access behaviour
i have a problem with an Ecto Repo and a schema in one of my tests. The schema is the following: defmodule Elixirserver.Transactions.Bank do #behaviour Elixirserver.ContentDump use Ecto.Schema import Ecto.Changeset alias Elixirserver.Transactions.Account #attrs [:name, :code] schema "banks" do field(:name, :string) field(:code, :string) has_many(:account, Account) timestamps() end #doc false def changeset(bank, attrs \\ []) do bank |> cast(attrs, #attrs) |> validate_required(#attrs) end def to_json(bank) do %{ id: bank.id, name: bank.name, code: bank.code, type: "BANK" } end end When i try to execute a test i obtain the following: (UndefinedFunctionError) function Elixirserver.Transactions.Bank.fetch/2 is undefined (Elixirserver.Transactions.Bank does not implement the Access behaviour) The test is this: def create(conn, %{"bank" => bank_params}) do with {:ok, %Bank{} = bank} <- Transactions.create_bank(bank_params) do conn |> put_status(:created) |> put_resp_header("location", bank_path(conn, :show, bank)) |> render("show.json", id: bank["id"]) end end Now, apparently this is because the Access behaviour is not implemented. Do i have to provide it explicitly ? I am using ExMachina to generate fixtures, and i generated the resources with mix phx.gen.json.
bank["id"] is most probably the problem. Structs don't implement the access interface, you should use the dot so this should work: bank.id. Details can be found here.
Ejabberd: error in simple module to handle offline messages
I have an Ejabberd 17.01 installation where I need to push a notification in case a recipient is offline. This seems the be a common task and solutions using a customized Ejabberd module can be found everywhere. However, I just don't get it running. First, here's me script: -module(mod_offline_push). -behaviour(gen_mod). -export([start/2, stop/1]). -export([push_message/3]). -include("ejabberd.hrl"). -include("logger.hrl"). -include("jlib.hrl"). start(Host, _Opts) -> ?INFO_MSG("mod_offline_push loading", []), ejabberd_hooks:add(offline_message_hook, Host, ?MODULE, push_message, 10), ok. stop(Host) -> ?INFO_MSG("mod_offline_push stopping", []), ejabberd_hooks:add(offline_message_hook, Host, ?MODULE, push_message, 10), ok. push_message(From, To, Packet) -> ?INFO_MSG("mod_offline_push -> push_message", [To]), Type = fxml:get_tag_attr_s(<<"type">>, Packet), % Supposedly since 16.04 %Type = xml:get_tag_attr_s(<<"type">>, Packet), % Supposedly since 13.XX %Type = xml:get_tag_attr_s("type", Packet), %Type = xml:get_tag_attr_s(list_to_binary("type"), Packet), ?INFO_MSG("mod_offline_push -> push_message", []), ok. The problem is the line Type = ... line in method push_message; without that line the last info message is logged (so the hook definitely works). When browsing online, I can find all kinds of function calls to extract elements from Packet. As far as I understand it changed over time with new releases. But it's not good, all variants lead in some kind of error. The current way returns: 2017-01-25 20:38:08.701 [error] <0.21678.0>#ejabberd_hooks:run1:332 {function_clause,[{fxml,get_tag_attr_s,[<<"type">>,{message,<<>>,normal,<<>>,{jid,<<"homer">>,<<"xxx.xxx.xxx.xxx">>,<<"conference">>,<<"homer">>,<<"xxx.xxx.xxx.xxx">>,<<"conference">>},{jid,<<"carl">>,<<"xxx.xxx.xxx.xxx">>,<<>>,<<"carl">>,<<"xxx.xxx.xxx.xxx">>,<<>>},[],[{text,<<>>,<<"sfsdfsdf">>}],undefined,[],#{}}],[{file,"src/fxml.erl"},{line,169}]},{mod_offline_push,push_message,3,[{file,"mod_offline_push.erl"},{line,33}]},{ejabberd_hooks,safe_apply,3,[{file,"src/ejabberd_hooks.erl"},{line,382}]},{ejabberd_hooks,run1,3,[{file,"src/ejabberd_hooks.erl"},{line,329}]},{ejabberd_sm,route,3,[{file,"src/ejabberd_sm.erl"},{line,126}]},{ejabberd_local,route,3,[{file,"src/ejabberd_local.erl"},{line,110}]},{ejabberd_router,route,3,[{file,"src/ejabberd_router.erl"},{line,87}]},{ejabberd_c2s,check_privacy_route,5,[{file,"src/ejabberd_c2s.erl"},{line,1886}]}]} running hook: {offline_message_hook,[{jid,<<"homer">>,<<"xxx.xxx.xxx.xxx">>,<<"conference">>,<<"homer">>,<<"xxx.xxx.xxx.xxx">>,<<"conference">>},{jid,<<"carl">>,<<"xxx.xxx.xxx.xxx">>,<<>>,<<"carl">>,<<"xxx.xxx.xxx.xxx">>,<<>>},{message,<<>>,normal,<<>>,{jid,<<"homer">>,<<"xxx.xxx.xxx.xxx">>,<<"conference">>,<<"homer">>,<<"xxx.xxx.xxx.xxx">>,<<"conference">>},{jid,<<"carl">>,<<"xxx.xxx.xxx.xxx">>,<<>>,<<"carl">>,<<"xxx.xxx.xxx.xxx">>,<<>>},[],[{text,<<>>,<<"sfsdfsdf">>}],undefined,[],#{}}]} I'm new Ejabberd and Erlang, so I cannot really interpret the error, but the Line 33 as mentioned in {mod_offline_push,push_message,3,[{file,"mod_offline_push.erl"}, {line,33}]} is definitely the line calling get_tag_attr_s. UPDATE 2017/01/27: Since this cost me a lot of headache -- and I'm still not perfectly happy -- I post here my current working module in the hopes it might help others. My setup is Ejabberd 17.01 running on Ubuntu 16.04. Most stuff I tried and failed with seem to for older versions of Ejabberd: -module(mod_fcm_fork). -behaviour(gen_mod). %% public methods for this module -export([start/2, stop/1]). -export([push_notification/3]). %% included for writing to ejabberd log file -include("ejabberd.hrl"). -include("logger.hrl"). -include("xmpp_codec.hrl"). %% Copied this record definition from jlib.hrl %% Including "xmpp_codec.hrl" and "jlib.hrl" resulted in errors ("XYZ already defined") -record(jid, {user = <<"">> :: binary(), server = <<"">> :: binary(), resource = <<"">> :: binary(), luser = <<"">> :: binary(), lserver = <<"">> :: binary(), lresource = <<"">> :: binary()}). start(Host, _Opts) -> ?INFO_MSG("mod_fcm_fork loading", []), % Providing the most basic API to the clients and servers that are part of the Inets application inets:start(), % Add hook to handle message to user who are offline ejabberd_hooks:add(offline_message_hook, Host, ?MODULE, push_notification, 10), ok. stop(Host) -> ?INFO_MSG("mod_fcm_fork stopping", []), ejabberd_hooks:add(offline_message_hook, Host, ?MODULE, push_notification, 10), ok. push_notification(From, To, Packet) -> % Generate JID of sender and receiver FromJid = lists:concat([binary_to_list(From#jid.user), "#", binary_to_list(From#jid.server), "/", binary_to_list(From#jid.resource)]), ToJid = lists:concat([binary_to_list(To#jid.user), "#", binary_to_list(To#jid.server), "/", binary_to_list(To#jid.resource)]), % Get message body MessageBody = Packet#message.body, % Check of MessageBody is not empty case MessageBody/=[] of true -> % Get first element (no idea when this list can have more elements) [First | _ ] = MessageBody, % Get message data and convert to string MessageBodyText = binary_to_list(First#text.data), send_post_request(FromJid, ToJid, MessageBodyText); false -> ?INFO_MSG("mod_fcm_fork -> push_notification: MessageBody is empty",[]) end, ok. send_post_request(FromJid, ToJid, MessageBodyText) -> %?INFO_MSG("mod_fcm_fork -> send_post_request -> MessageBodyText = ~p", [Demo]), Method = post, PostURL = gen_mod:get_module_opt(global, ?MODULE, post_url,fun(X) -> X end, all), % Add data as query string. Not nice, query body would be preferable % Problem: message body itself can be in a JSON string, and I couldn't figure out the correct encoding. URL = lists:concat([binary_to_list(PostURL), "?", "fromjid=", FromJid,"&tojid=", ToJid,"&body=", edoc_lib:escape_uri(MessageBodyText)]), Header = [], ContentType = "application/json", Body = [], ?INFO_MSG("mod_fcm_fork -> send_post_request -> URL = ~p", [URL]), % ADD SSL CONFIG BELOW! %HTTPOptions = [{ssl,[{versions, ['tlsv1.2']}]}], HTTPOptions = [], Options = [], httpc:request(Method, {URL, Header, ContentType, Body}, HTTPOptions, Options), ok.
Actually it fails with second arg Packet you pass to fxml:get_tag_attr_s in push_message function {message,<<>>,normal,<<>>, {jid,<<"homer">>,<<"xxx.xxx.xxx.xxx">>,<<"conference">>, <<"homer">>,<<"xxx.xxx.xxx.xxx">>,<<"conference">>}, {jid,<<"carl">>,<<"xxx.xxx.xxx.xxx">>,<<>>,<<"carl">>, <<"xxx.xxx.xxx.xxx">>,<<>>}, [], [{text,<<>>,<<"sfsdfsdf">>}], undefined,[],#{}} because it is not xmlel Looks like it is record "message" defined in tools/xmpp_codec.hrl with <<>> id and type 'normal' xmpp_codec.hrl -record(message, {id :: binary(), type = normal :: 'chat' | 'error' | 'groupchat' | 'headline' | 'normal', lang :: binary(), from :: any(), to :: any(), subject = [] :: [#text{}], body = [] :: [#text{}], thread :: binary(), error :: #error{}, sub_els = [] :: [any()]}). Include this file and use just Type = Packet#message.type or, if you expect binary value Type = erlang:atom_to_binary(Packet#message.type, utf8)
The newest way to do that seems to be with xmpp:get_type/1: Type = xmpp:get_type(Packet), It returns an atom, in this case normal.
Haskell Network.Browser HTTPS Connection
Is there a way to make https calls with the Network.Browser package. I'm not seeing it in the documentation on Hackage. If there isn't a way to do it with browse is there another way to fetch https pages? My current test code is import Network.HTTP import Network.URI (parseURI) import Network.HTTP.Proxy import Data.Maybe (fromJust) import Control.Applicative ((<$>)) import Network.Browser retrieveUrl :: String -> IO String retrieveUrl url = do rsp <- browse $ request (Request (fromJust uri) POST [] "Body") return $ snd (rspBody <$> rsp) where uri = parseURI url I've been running nc -l -p 8000 and watching the output. I see that it doesn't encrypt it when I do retrieveUrl https://localhost:8000 Also when I try a real https site I get: Network.Browser.request: Error raised ErrorClosed *** Exception: user error (Network.Browser.request: Error raised ErrorClosed) Edit: Network.Curl solution (For doing a SOAP call) import Network.Curl (curlGetString) import Network.Curl.Opts soapHeader s = CurlHttpHeaders ["Content-Type: text/xml", "SOAPAction: " ++ s] proxy = CurlProxy "proxy.foo.org" envelope = "myRequestEnvelope.xml" headers = readFile envelope >>= (\x -> return [ soapHeader "myAction" , proxy , CurlPost True , CurlPostFields [x]]) main = headers >>= curlGetString "https://service.endpoint"
An alternative and perhaps more "haskelly" solution as Travis Brown put it with http-conduit: To just fetch https pages: import Network.HTTP.Conduit import qualified Data.ByteString.Lazy as L main = simpleHttp "https://www.noisebridge.net/wiki/Noisebridge" >>= L.putStr The below shows how to pass urlencode parameters. {-# LANGUAGE OverloadedStrings #-} import Network.HTTP.Conduit import qualified Data.ByteString.Lazy as L main = do initReq <- parseUrl "https://www.googleapis.com/urlshortener/v1/url" let req' = initReq { secure = True } -- Turn on https let req = (flip urlEncodedBody) req' $ [ ("longUrl", "http://www.google.com/") -- , ] response <- withManager $ httpLbs req L.putStr $ responseBody response You can also set the method, content-type, and request body manually. The api is the same as in http-enumerator a good example is: https://stackoverflow.com/a/5614946
I've wondered about this in the past and have always ended up just using the libcurl bindings. It would be nice to have a more Haskelly solution, but Network.Curl is very convenient.
If all you want to do is fetch a page, Network.HTTP.Wget is the most simple way. Exhibit a: import Network.HTTP.Wget main = putStrLn =<< wget "https://www.google.com" [] []