https://github.com/engagesoftware/elm-dnn-http
Helpers for working with DNN Web API
https://github.com/engagesoftware/elm-dnn-http
Last synced: 11 months ago
JSON representation
Helpers for working with DNN Web API
- Host: GitHub
- URL: https://github.com/engagesoftware/elm-dnn-http
- Owner: EngageSoftware
- License: apache-2.0
- Created: 2018-01-11T20:49:25.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-09-11T13:06:16.000Z (almost 2 years ago)
- Last Synced: 2024-09-11T20:16:43.641Z (almost 2 years ago)
- Language: Elm
- Homepage: http://package.elm-lang.org/packages/EngageSoftware/elm-dnn-http/latest
- Size: 92.8 KB
- Stars: 0
- Watchers: 4
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# elm-dnn-http
Helpers for working with Engage's DNN Web API.
Provides a decoder to extract necessary API details from your flags:
```elm
flagsDecoder : Json.Decode.Decoder { httpConfig : Engage.Http.Config, localization : Engage.Localization.Localization }
flagsDecoder =
Json.Decode.map2
(\httpConfig localization -> { httpConfig = httpConfig, localization = localization })
(Json.Decode.field "httpConfig" Engage.Http.configDecoder)
(Json.Decode.field "localization" Engage.Localization.decoder)
init : Json.Encode.Value -> ( Model, Cmd Msg )
init flagsJson =
case Json.Decode.decodeValue flagsDecoder flagsJson of
Ok { httpConfig, localization } ->
( { model | httpConfig = httpConfig, localization = localization }, Cmd.none )
Err error ->
( { model | errorMessage = Just (Json.Decode.errorToString error) }, Cmd.none )
```
Used within DNN, your initializing code may look like this:
```asp
<%@ Register TagPrefix="dnn" Namespace="DotNetNuke.Web.Client.ClientResourceManagement" Assembly="DotNetNuke.Web.Client" %>
<%@ Import Namespace="Engage.Dnn.Framework" %>
<%@ Import Namespace="Engage.Util" %>
jQuery(function ($) {
var sf = $.ServicesFramework(<%:this.ModuleId%>);
Elm.MyClient.ViewMyProject.Main.init({
node: document.getElementById(<%: ApplicationPanel.ClientID.AsQuotedJavaScriptString() %>),
flags: {
httpConfig: {
baseUrl: sf.getServiceRoot('MyClient') + 'MyProject',
headers: {
TabId: <%:ModuleContext.TabId.ToString(CultureInfo.InvariantCulture).AsQuotedJavaScriptString()%>,
ModuleId: <%:ModuleContext.ModuleId.ToString(CultureInfo.InvariantCulture).AsQuotedJavaScriptString()%>,
RequestVerificationToken: sf.getAntiForgeryValue()
}
},
localization: <%: LocalizationJson %>
}
});
});
/// <summary>Gets a JSON array with localized text for this control's resource file.</summary>
/// <value>An HTML string with a JSON array of objects with <c>key</c> and <c>value</c> properties.</value>
private IHtmlString LocalizationJson => LocalizationUtility.GetResources(this.LocalResourceFile)
.Where(keyValue => !string.IsNullOrEmpty(keyValue.Value))
.Select(keyValue => new { key = keyValue.Key, value = keyValue.Value })
.ToJson()
.AsRawHtml();
```
Provides opinionated methods for calling HTTP endpoints:
```elm
updateProfile : Engage.Http.Config -> Json.Encode.Value -> Cmd Msg
updateProfile httpConfig body =
Engage.Http.post
httpConfig
ProfileUpdated
(decodeUpdateResponse)
{ methodName = "Profile", value = body }
```
Provides opinionated method for retrieving error messages from a response:
```elm
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
ProfileUpdated response ->
case response of
NotAsked ->
( { model | currentRequest = response }, Cmd.none )
Loading ->
( { model | currentRequest = response }, Cmd.none )
Failure error ->
( { model | currentRequest = response, errorMessage = Just (Engage.Http.getErrorMessage model error) }, Cmd.none )
Success result ->
( { model | currentRequest = response }, Cmd.none )
```