Skip to content

Commit

Permalink
Merge pull request #291 from SergejDK/webrequestdocumentation
Browse files Browse the repository at this point in the history
added a documentation for webrequest
  • Loading branch information
TimLariviere authored Jan 25, 2019
2 parents 104157a + 5d23b7b commit 605ded1
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 146 deletions.
1 change: 1 addition & 0 deletions Packages.targets
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project>
<ItemGroup>
<PackageReference Update="FSharp.Core" Version="4.5.2" />
<PackageReference Update="FSharp.Data" Version="3.0.0" />
<PackageReference Update="Xamarin.Forms" Version="3.4.0.1009999" />
<PackageReference Update="Xamarin.Forms.Platform.WPF" Version="3.4.0.1009999" />
<PackageReference Update="Xamarin.Forms.Maps" Version="3.4.0.1009999" />
Expand Down
48 changes: 48 additions & 0 deletions docs/update.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,54 @@ let update msg model =
| PictureTaken -> ...
```

Webrequests in a Command
----
Sometimes it is needed to make some web requests. Which tool you use here does not matter. For example you could use FSharp.Data to make HttpRequests.
These are the steps that you have to do, to make it work:
1. Create a case in the message type for a successful and failure webrequests
```fsharp
type Msg =
| LoginClicked
| LoginSuccess
| AuthError
```
2. Implement the Command and return the correct message
```fsharp
let authUser (username : string) (password : string) =
async {
do! Async.SwitchToThreadPool()
// make your http call
// FSharp.Data.HTTPUtil is used here
let! response = Http.AsyncRequest
(url = URL, body = TextRequest """ {"username": "test", "password": "testpassword"} """,
httpMethod = "POST", silentHttpErrors = true)
let r =
match response.StatusCode with
| 200 -> LoginSuccess
| _ -> AuthError
return r
}
|> Cmd.ofAsyncMsg
```
3. Call the Command from update e.g. when a button is clicked
```fsharp
let update msg model =
match msg with
| LoginClicked -> { model with IsRunning = true }, authUser model.Username model.Password // Call the Command
| LoginSuccess ->
{ model with IsLoggedIn = true
IsRunning = false }, Cmd.none
| AuthError ->
{ model with IsLoggedIn = false
IsRunning = false }, Cmd.none
```
4. Create your view as you need
```fsharp
match model.IsLoggedIn with
| true -> LoggedInSuccesful
| false -> LoginView
```

Platform-specific dispatch
-----

Expand Down
7 changes: 6 additions & 1 deletion paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ group androidapp
nuget Xamarin.Android.Support.v7.RecyclerView 27.0.2.1
nuget Xamarin.Android.Support.Vector.Drawable 27.0.2.1
nuget Newtonsoft.Json 11.0.2
nuget FSharp.Data 3.0.0

# Dependencies used when compiling and running iOS samples. We can normally use the latest FSharp.Core and
# latest Xamarin.Forms though we pin them for now.
Expand All @@ -50,13 +51,15 @@ group iosapp
nuget FSharp.Core 4.5.2
nuget Xamarin.Forms 3.4.0.1009999
nuget Newtonsoft.Json 11.0.2
nuget FSharp.Data 3.0.0

group macosapp
framework: xamarinmac20
source https://api.nuget.org/v3/index.json
nuget FSharp.Core 4.5.2
nuget Xamarin.Forms 3.4.0.1009999
nuget Newtonsoft.Json 11.0.2
nuget FSharp.Data 3.0.0

group wpfapp
framework: net472
Expand All @@ -65,11 +68,13 @@ group wpfapp
nuget Xamarin.Forms 3.4.0.1009999
nuget Newtonsoft.Json 11.0.2
nuget Xamarin.Forms.Platform.WPF 3.4.0.1009999
nuget FSharp.Data 3.0.0

group gtkapp
framework: net471
source https://api.nuget.org/v3/index.json
nuget FSharp.Core 4.5.2
nuget Xamarin.Forms 3.4.0.1009999
nuget Newtonsoft.Json 11.0.2
nuget Xamarin.Forms.Platform.GTK 3.4.0.1009999
nuget Xamarin.Forms.Platform.GTK 3.4.0.1009999
nuget FSharp.Data 3.0.0
102 changes: 7 additions & 95 deletions paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ NUGET
System.Threading.Thread (>= 4.0)
System.Threading.ThreadPool (>= 4.0.10)
System.Threading.Timer (>= 4.0.1)
FSharp.Data (3.0)
Microsoft.CSharp (4.5)
Microsoft.NETCore.Platforms (2.2)
Microsoft.NETCore.Targets (2.1)
Microsoft.Win32.Primitives (4.3)
NETStandard.Library (2.0.3)
Microsoft.NETCore.Platforms (>= 1.1)
Expand Down Expand Up @@ -84,46 +84,6 @@ NUGET
System.Runtime.Serialization.Formatters (>= 4.3)
System.Runtime.Serialization.Primitives (>= 4.3)
System.Xml.XmlDocument (>= 4.3)
runtime.debian.8-x64.runtime.native.System.Security.Cryptography.OpenSsl (4.3.3)
runtime.debian.9-x64.runtime.native.System.Security.Cryptography.OpenSsl (4.3.3)
runtime.fedora.23-x64.runtime.native.System.Security.Cryptography.OpenSsl (4.3.3)
runtime.fedora.24-x64.runtime.native.System.Security.Cryptography.OpenSsl (4.3.3)
runtime.fedora.27-x64.runtime.native.System.Security.Cryptography.OpenSsl (4.3.3)
runtime.fedora.28-x64.runtime.native.System.Security.Cryptography.OpenSsl (4.3.3)
runtime.native.System (4.3.1)
Microsoft.NETCore.Platforms (>= 1.1.1)
Microsoft.NETCore.Targets (>= 1.1.3)
runtime.native.System.Net.Http (4.3.1)
Microsoft.NETCore.Platforms (>= 1.1.1)
Microsoft.NETCore.Targets (>= 1.1.3)
runtime.native.System.Security.Cryptography.Apple (4.3.1)
runtime.osx.10.10-x64.runtime.native.System.Security.Cryptography.Apple (>= 4.3.1)
runtime.native.System.Security.Cryptography.OpenSsl (4.3.3)
runtime.debian.8-x64.runtime.native.System.Security.Cryptography.OpenSsl (>= 4.3.3)
runtime.debian.9-x64.runtime.native.System.Security.Cryptography.OpenSsl (>= 4.3.3)
runtime.fedora.23-x64.runtime.native.System.Security.Cryptography.OpenSsl (>= 4.3.3)
runtime.fedora.24-x64.runtime.native.System.Security.Cryptography.OpenSsl (>= 4.3.3)
runtime.fedora.27-x64.runtime.native.System.Security.Cryptography.OpenSsl (>= 4.3.3)
runtime.fedora.28-x64.runtime.native.System.Security.Cryptography.OpenSsl (>= 4.3.3)
runtime.opensuse.13.2-x64.runtime.native.System.Security.Cryptography.OpenSsl (>= 4.3.3)
runtime.opensuse.42.1-x64.runtime.native.System.Security.Cryptography.OpenSsl (>= 4.3.3)
runtime.opensuse.42.3-x64.runtime.native.System.Security.Cryptography.OpenSsl (>= 4.3.3)
runtime.osx.10.10-x64.runtime.native.System.Security.Cryptography.OpenSsl (>= 4.3.3)
runtime.rhel.7-x64.runtime.native.System.Security.Cryptography.OpenSsl (>= 4.3.3)
runtime.ubuntu.14.04-x64.runtime.native.System.Security.Cryptography.OpenSsl (>= 4.3.3)
runtime.ubuntu.16.04-x64.runtime.native.System.Security.Cryptography.OpenSsl (>= 4.3.3)
runtime.ubuntu.16.10-x64.runtime.native.System.Security.Cryptography.OpenSsl (>= 4.3.3)
runtime.ubuntu.18.04-x64.runtime.native.System.Security.Cryptography.OpenSsl (>= 4.3.3)
runtime.opensuse.13.2-x64.runtime.native.System.Security.Cryptography.OpenSsl (4.3.3)
runtime.opensuse.42.1-x64.runtime.native.System.Security.Cryptography.OpenSsl (4.3.3)
runtime.opensuse.42.3-x64.runtime.native.System.Security.Cryptography.OpenSsl (4.3.3)
runtime.osx.10.10-x64.runtime.native.System.Security.Cryptography.Apple (4.3.1)
runtime.osx.10.10-x64.runtime.native.System.Security.Cryptography.OpenSsl (4.3.3)
runtime.rhel.7-x64.runtime.native.System.Security.Cryptography.OpenSsl (4.3.3)
runtime.ubuntu.14.04-x64.runtime.native.System.Security.Cryptography.OpenSsl (4.3.3)
runtime.ubuntu.16.04-x64.runtime.native.System.Security.Cryptography.OpenSsl (4.3.3)
runtime.ubuntu.16.10-x64.runtime.native.System.Security.Cryptography.OpenSsl (4.3.3)
runtime.ubuntu.18.04-x64.runtime.native.System.Security.Cryptography.OpenSsl (4.3.3)
System.AppContext (4.3)
System.Runtime (>= 4.3)
System.Collections (4.3)
Expand Down Expand Up @@ -161,63 +121,9 @@ NUGET
System.Runtime.Serialization.Formatters (4.3)
System.Runtime.Serialization.Primitives (4.3)
System.Security.Cryptography.Algorithms (4.3.1)
Microsoft.NETCore.Platforms (>= 1.1)
runtime.native.System.Security.Cryptography.Apple (>= 4.3.1)
runtime.native.System.Security.Cryptography.OpenSsl (>= 4.3.2)
System.Collections (>= 4.3)
System.IO (>= 4.3)
System.Resources.ResourceManager (>= 4.3)
System.Runtime (>= 4.3)
System.Runtime.Extensions (>= 4.3)
System.Runtime.Handles (>= 4.3)
System.Runtime.InteropServices (>= 4.3)
System.Runtime.Numerics (>= 4.3)
System.Security.Cryptography.Encoding (>= 4.3)
System.Security.Cryptography.Primitives (>= 4.3)
System.Text.Encoding (>= 4.3)
System.Security.Cryptography.Cng (4.5)
System.Security.Cryptography.Csp (4.3)
System.Security.Cryptography.Encoding (4.3)
System.Security.Cryptography.OpenSsl (4.5)
System.Collections (>= 4.3)
System.IO (>= 4.3)
System.Resources.ResourceManager (>= 4.3)
System.Runtime (>= 4.3)
System.Runtime.Extensions (>= 4.3)
System.Runtime.Handles (>= 4.3)
System.Runtime.InteropServices (>= 4.3)
System.Runtime.Numerics (>= 4.3)
System.Security.Cryptography.Algorithms (>= 4.3)
System.Security.Cryptography.Encoding (>= 4.3)
System.Security.Cryptography.Primitives (>= 4.3)
System.Text.Encoding (>= 4.3)
System.Security.Cryptography.Primitives (4.3)
System.Security.Cryptography.X509Certificates (4.3.2)
Microsoft.NETCore.Platforms (>= 1.1)
runtime.native.System (>= 4.3)
runtime.native.System.Net.Http (>= 4.3)
runtime.native.System.Security.Cryptography.OpenSsl (>= 4.3.2)
System.Collections (>= 4.3)
System.Diagnostics.Debug (>= 4.3)
System.Globalization (>= 4.3)
System.Globalization.Calendars (>= 4.3)
System.IO (>= 4.3)
System.IO.FileSystem (>= 4.3)
System.IO.FileSystem.Primitives (>= 4.3)
System.Resources.ResourceManager (>= 4.3)
System.Runtime (>= 4.3)
System.Runtime.Extensions (>= 4.3)
System.Runtime.Handles (>= 4.3)
System.Runtime.InteropServices (>= 4.3)
System.Runtime.Numerics (>= 4.3)
System.Security.Cryptography.Algorithms (>= 4.3)
System.Security.Cryptography.Cng (>= 4.3)
System.Security.Cryptography.Csp (>= 4.3)
System.Security.Cryptography.Encoding (>= 4.3)
System.Security.Cryptography.OpenSsl (>= 4.3)
System.Security.Cryptography.Primitives (>= 4.3)
System.Text.Encoding (>= 4.3)
System.Threading (>= 4.3)
System.Text.Encoding (4.3)
System.Text.Encoding.Extensions (4.3)
System.Text.RegularExpressions (4.3)
Expand Down Expand Up @@ -258,6 +164,8 @@ RESTRICTION: == net471
NUGET
remote: https://api.nuget.org/v3/index.json
FSharp.Core (4.5.2)
FSharp.Data (3.0)
FSharp.Core (>= 4.0.0.1)
Newtonsoft.Json (11.0.2)
Xamarin.Forms (3.4.0.1009999)
Xamarin.Forms.Platform.GTK (3.4.0.1009999)
Expand Down Expand Up @@ -291,6 +199,7 @@ NUGET
System.Threading.Thread (>= 4.0)
System.Threading.ThreadPool (>= 4.0.10)
System.Threading.Timer (>= 4.0.1)
FSharp.Data (3.0)
Microsoft.CSharp (4.5)
Microsoft.NETCore.Platforms (2.2)
Microsoft.Win32.Primitives (4.3)
Expand Down Expand Up @@ -427,6 +336,7 @@ NUGET
System.Threading.Thread (>= 4.0)
System.Threading.ThreadPool (>= 4.0.10)
System.Threading.Timer (>= 4.0.1)
FSharp.Data (3.0)
Microsoft.CSharp (4.5)
Microsoft.NETCore.Platforms (2.2)
Microsoft.Win32.Primitives (4.3)
Expand Down Expand Up @@ -559,6 +469,8 @@ RESTRICTION: == net472
NUGET
remote: https://api.nuget.org/v3/index.json
FSharp.Core (4.5.2)
FSharp.Data (3.0)
FSharp.Core (>= 4.0.0.1)
Newtonsoft.Json (11.0.2)
OpenTK (3.0.1)
OpenTK.GLControl (3.0.1)
Expand Down
Loading

0 comments on commit 605ded1

Please sign in to comment.