This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.

ElmArk.Peer

Handles building requests for the peer endpoints and encoding/decoding their json.

Models

type alias Peer = { ip : String , inboundPort : Int , version : String , errors : Int , os : String , height : Int , status : String , delay : Int }

Peer record for making up the list of peers in getPeers response Sample JSON:

{
    "ip": "185.183.156.115",
    "port": 4001,
    "version": "1.0.2",
    "errors": 0,
    "os": "linux4.4.0-104-generic",
    "height": 2956067,
    "status": "OK",
    "delay": 199
}
type alias Peers = { success : Bool , peers : List Peer }

List of peers returned by getPeers Sample JSON:

{
    "success": true,
    "peers": [
        {
            "ip": "54.38.48.170",
            "port": 4001,
            "version": "1.0.2",
            "errors": 1,
            "os": "linux4.4.0-104-generic",
            "height": 2953767,
            "status": "OK",
            "delay": 463
        },
        {
            "ip": "185.183.156.115",
            "port": 4001,
            "version": "1.0.2",
            "errors": 0,
            "os": "linux4.4.0-104-generic",
            "height": 2956067,
            "status": "OK",
            "delay": 199
        }
    ]
}
type alias GetPeer = { success : Bool , peer : Peer }

GetPeer is the record for the response of getPeer Sample JSON:

{
    "success": true,
    "peer": {
        "ip": "54.38.48.170",
        "port": 4001,
        "version": "1.0.2",
        "errors": 6,
        "os": "linux4.4.0-104-generic",
        "height": 2953767,
        "status": "ETIMEOUT",
        "delay": 1060
    }
}
type alias PeerVersion = { success : Bool , version : String , build : String }

PeerVersion is the record representing the JSON response of getPeer Sample JSON:

{
    "success": true,
    "version": "1.0.2",
    "build": ""
}

Requests

getPeers : RequestHead -> ( RequestHead, RequestBody )

Get all the peers

mainNet
    |> getPeers
getPeer : String -> String -> RequestHead -> ( RequestHead, RequestBody )

Get a single peer by IP and port

mainNet
    |> getPeer "54.38.48.170" "4001"
getPeerVersion : RequestHead -> ( RequestHead, RequestBody )

Gets the peer version

mainNet
    |> getPeerVersion

JSON

decodePeer : Json.Decode.Decoder Peer

Parse JSON into Peer record

decodePeers : Json.Decode.Decoder Peers

Parse JSON into Peers record

encodePeer : Peer -> Json.Encode.Value

Turn Peer record into JSON string

encodePeers : Peers -> Json.Encode.Value

Turn Peers record into JSON

decodeGetPeer : Json.Decode.Decoder GetPeer

Parse JSON into GetPeer record

encodeGetPeer : GetPeer -> Json.Encode.Value

Turn GetPeer record into JSON string

decodePeerVersion : Json.Decode.Decoder PeerVersion

Parses JSON into PeerVersion record

encodePeerVersion : PeerVersion -> Json.Encode.Value

Turn PeerVersion record into JSON string

module ElmArk.Peer exposing (..)

{-| Handles building requests for the peer endpoints
and encoding/decoding their json.


# Models

@docs Peer, Peers, GetPeer, PeerVersion


# Requests

@docs getPeers, getPeer, getPeerVersion


# JSON

@docs decodePeer, decodePeers, encodePeer, encodePeers, decodeGetPeer, encodeGetPeer, decodePeerVersion, encodePeerVersion

-}

import ElmArk.Api.Model exposing (RequestBody(..), RequestHead)
import Json.Encode
import Json.Decode
import Json.Decode.Pipeline


{-| Get all the peers

    mainNet
        |> getPeers

-}
getPeers : RequestHead -> ( RequestHead, RequestBody )
getPeers heads =
    ( heads, GetRequest (heads.url ++ "api/peers") [] )


{-| List of peers returned by getPeers
Sample JSON:

    {
        "success": true,
        "peers": [
            {
                "ip": "54.38.48.170",
                "port": 4001,
                "version": "1.0.2",
                "errors": 1,
                "os": "linux4.4.0-104-generic",
                "height": 2953767,
                "status": "OK",
                "delay": 463
            },
            {
                "ip": "185.183.156.115",
                "port": 4001,
                "version": "1.0.2",
                "errors": 0,
                "os": "linux4.4.0-104-generic",
                "height": 2956067,
                "status": "OK",
                "delay": 199
            }
        ]
    }

-}
type alias Peers =
    { success : Bool
    , peers : List Peer
    }


{-| Parse JSON into Peers record
-}
decodePeers : Json.Decode.Decoder Peers
decodePeers =
    Json.Decode.Pipeline.decode Peers
        |> Json.Decode.Pipeline.required "success" (Json.Decode.bool)
        |> Json.Decode.Pipeline.required "peers" (Json.Decode.list decodePeer)


{-| Turn Peers record into JSON
-}
encodePeers : Peers -> Json.Encode.Value
encodePeers record =
    Json.Encode.object
        [ ( "success", Json.Encode.bool <| record.success )
        , ( "peers", Json.Encode.list <| List.map encodePeer <| record.peers )
        ]


{-| Peer record for making up the list of peers in getPeers response
Sample JSON:

    {
        "ip": "185.183.156.115",
        "port": 4001,
        "version": "1.0.2",
        "errors": 0,
        "os": "linux4.4.0-104-generic",
        "height": 2956067,
        "status": "OK",
        "delay": 199
    }

-}
type alias Peer =
    { ip : String
    , inboundPort : Int
    , version : String
    , errors : Int
    , os : String
    , height : Int
    , status : String
    , delay : Int
    }


{-| Parse JSON into Peer record
-}
decodePeer : Json.Decode.Decoder Peer
decodePeer =
    Json.Decode.Pipeline.decode Peer
        |> Json.Decode.Pipeline.required "ip" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "port" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "version" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "errors" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "os" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "height" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "status" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "delay" (Json.Decode.int)


{-| Turn Peer record into JSON string
-}
encodePeer : Peer -> Json.Encode.Value
encodePeer record =
    Json.Encode.object
        [ ( "ip", Json.Encode.string <| record.ip )
        , ( "port", Json.Encode.int <| record.inboundPort )
        , ( "version", Json.Encode.string <| record.version )
        , ( "errors", Json.Encode.int <| record.errors )
        , ( "os", Json.Encode.string <| record.os )
        , ( "height", Json.Encode.int <| record.height )
        , ( "status", Json.Encode.string <| record.status )
        , ( "delay", Json.Encode.int <| record.delay )
        ]


{-| Get a single peer by IP and port

    mainNet
        |> getPeer "54.38.48.170" "4001"

-}
getPeer : String -> String -> RequestHead -> ( RequestHead, RequestBody )
getPeer ip inboundPort heads =
    ( heads, GetRequest (heads.url ++ "api/peers/get") [ ( "ip", ip ), ( "port", inboundPort ) ] )


{-| GetPeer is the record for the response of getPeer
Sample JSON:

    {
        "success": true,
        "peer": {
            "ip": "54.38.48.170",
            "port": 4001,
            "version": "1.0.2",
            "errors": 6,
            "os": "linux4.4.0-104-generic",
            "height": 2953767,
            "status": "ETIMEOUT",
            "delay": 1060
        }
    }

-}
type alias GetPeer =
    { success : Bool
    , peer : Peer
    }


{-| Parse JSON into GetPeer record
-}
decodeGetPeer : Json.Decode.Decoder GetPeer
decodeGetPeer =
    Json.Decode.Pipeline.decode GetPeer
        |> Json.Decode.Pipeline.required "success" (Json.Decode.bool)
        |> Json.Decode.Pipeline.required "peer" (decodePeer)


{-| Turn GetPeer record into JSON string
-}
encodeGetPeer : GetPeer -> Json.Encode.Value
encodeGetPeer record =
    Json.Encode.object
        [ ( "success", Json.Encode.bool <| record.success )
        , ( "peer", encodePeer <| record.peer )
        ]


{-| Gets the peer version

    mainNet
        |> getPeerVersion

-}
getPeerVersion : RequestHead -> ( RequestHead, RequestBody )
getPeerVersion heads =
    ( heads, GetRequest (heads.url ++ "api/peers/version") [] )


{-| PeerVersion is the record representing the JSON response of getPeer
Sample JSON:

    {
        "success": true,
        "version": "1.0.2",
        "build": ""
    }

-}
type alias PeerVersion =
    { success : Bool
    , version : String
    , build : String
    }


{-| Parses JSON into PeerVersion record
-}
decodePeerVersion : Json.Decode.Decoder PeerVersion
decodePeerVersion =
    Json.Decode.Pipeline.decode PeerVersion
        |> Json.Decode.Pipeline.required "success" (Json.Decode.bool)
        |> Json.Decode.Pipeline.required "version" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "build" (Json.Decode.string)


{-| Turn PeerVersion record into JSON string
-}
encodePeerVersion : PeerVersion -> Json.Encode.Value
encodePeerVersion record =
    Json.Encode.object
        [ ( "success", Json.Encode.bool <| record.success )
        , ( "version", Json.Encode.string <| record.version )
        , ( "build", Json.Encode.string <| record.build )
        ]