Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift + JSON-RPC

Yosuke Ishikawa
April 25, 2016
41k

Swift + JSON-RPC

Yosuke Ishikawa

April 25, 2016
Tweet

Transcript

  1. Swi + JSON-RPC
    ishkawa

    View Slide

  2. ishkawa?

    View Slide

  3. JSON-RPC?

    View Slide

  4. JSON-RPC is a stateless, light-weight
    remote procedure call (RPC) protocol.

    View Slide

  5. HTTP body
    のJSON
    でリクエストを記述する
    1
    回の通信で複数のリクエストを実行できる

    View Slide

  6. 何に使うの?

    View Slide

  7. View Slide

  8. タイムラインの投稿を取得する
    タイムラインの座標を住所に変換する
    バナー
    を取得する

    View Slide

  9. GET /home ?

    View Slide

  10. 複数の処理をまとめたAPI
    は仕様が壊れやすい

    View Slide

  11. JSON-RPC Batch

    View Slide

  12. 個々
    の処理をメソッドとして定義
    クライアント側で1
    つのリクエストにまとめる
    情報の組み合わせの変更に強い

    View Slide

  13. Request
    [
    {
    "
    i
    d
    "
    : "
    X
    X
    X
    X
    X
    X
    X
    X
    -
    X
    X
    X
    X
    -
    X
    X
    X
    X
    -
    X
    X
    X
    X
    -
    X
    X
    X
    X
    X
    X
    X
    X
    X
    X
    X
    X
    -
    X
    "
    ,
    "
    j
    s
    o
    n
    r
    p
    c
    "
    : "
    2
    .
    0
    "
    ,
    "
    m
    e
    t
    h
    o
    d
    "
    : "
    G
    e
    t
    T
    i
    m
    e
    l
    i
    n
    e
    O
    f
    f
    e
    r
    s
    "
    ,
    "
    p
    a
    r
    a
    m
    s
    "
    : { .
    .
    . }
    }
    ,
    {
    "
    i
    d
    "
    : "
    X
    X
    X
    X
    X
    X
    X
    X
    -
    X
    X
    X
    X
    -
    X
    X
    X
    X
    -
    X
    X
    X
    X
    -
    X
    X
    X
    X
    X
    X
    X
    X
    X
    X
    X
    X
    -
    X
    "
    ,
    "
    j
    s
    o
    n
    r
    p
    c
    "
    : "
    2
    .
    0
    "
    ,
    "
    m
    e
    t
    h
    o
    d
    "
    : "
    G
    e
    t
    B
    a
    n
    n
    e
    r
    s
    "
    ,
    "
    p
    a
    r
    a
    m
    s
    "
    : { .
    .
    . }
    }
    ]

    View Slide

  14. Response
    [
    {
    "
    i
    d
    "
    : "
    X
    X
    X
    X
    X
    X
    X
    X
    -
    X
    X
    X
    X
    -
    X
    X
    X
    X
    -
    X
    X
    X
    X
    -
    X
    X
    X
    X
    X
    X
    X
    X
    X
    X
    X
    X
    -
    X
    "
    ,
    "
    j
    s
    o
    n
    r
    p
    c
    "
    : "
    2
    .
    0
    "
    ,
    "
    r
    e
    s
    u
    l
    t
    "
    : { .
    .
    . }
    }
    ,
    {
    "
    i
    d
    "
    : "
    X
    X
    X
    X
    X
    X
    X
    X
    -
    X
    X
    X
    X
    -
    X
    X
    X
    X
    -
    X
    X
    X
    X
    -
    X
    X
    X
    X
    X
    X
    X
    X
    X
    X
    X
    X
    -
    X
    "
    ,
    "
    j
    s
    o
    n
    r
    p
    c
    "
    : "
    2
    .
    0
    "
    ,
    "
    r
    e
    s
    u
    l
    t
    "
    : { .
    .
    . }
    }
    ]

    View Slide

  15. View Slide

  16. Swi
    でどう表現する?

    View Slide

  17. APIKit
    の仕組みに乗ろう

    View Slide

  18. リクエストの型が決まればレスポンスの型も決まる
    l
    e
    t r
    e
    q
    u
    e
    s
    t = S
    o
    m
    e
    R
    e
    q
    u
    e
    s
    t
    (
    )
    l
    e
    t r
    e
    s
    p
    o
    n
    s
    e = S
    e
    s
    s
    i
    o
    n
    .
    r
    x
    _
    r
    e
    s
    p
    o
    n
    s
    e
    (
    r
    e
    q
    u
    e
    s
    t
    )
    /
    / r
    e
    s
    p
    o
    n
    s
    e
    の型はO
    b
    s
    e
    r
    v
    a
    b
    l
    e
    <
    S
    o
    m
    e
    R
    e
    q
    u
    e
    s
    t
    .
    R
    e
    s
    p
    o
    n
    s
    e
    >

    View Slide

  19. 複数のリクエストに拡張しよう

    View Slide

  20. l
    e
    t r
    e
    q
    u
    e
    s
    t
    A = R
    e
    q
    u
    e
    s
    t
    A
    (
    )
    l
    e
    t r
    e
    q
    u
    e
    s
    t
    B = R
    e
    q
    u
    e
    s
    t
    B
    (
    )
    l
    e
    t r
    e
    s
    p
    o
    n
    s
    e = S
    e
    s
    s
    i
    o
    n
    .
    r
    x
    _
    r
    e
    s
    p
    o
    n
    s
    e
    (
    r
    e
    q
    u
    e
    s
    t
    A
    , r
    e
    q
    u
    e
    s
    t
    B
    )
    /
    / r
    e
    s
    p
    o
    n
    s
    e
    の型はO
    b
    s
    e
    r
    v
    a
    b
    l
    e
    <
    (
    R
    e
    q
    u
    e
    s
    t
    A
    .
    R
    e
    s
    p
    o
    n
    s
    e
    , R
    e
    q
    u
    e
    s
    t
    B
    .
    R
    e
    s
    p
    o
    n
    s
    e
    )
    >

    View Slide

  21. タプルの要素数は?
    S
    e
    s
    s
    i
    o
    n
    .
    r
    x
    _
    r
    e
    s
    p
    o
    n
    s
    e
    (
    r
    e
    q
    u
    e
    s
    t
    A
    , r
    e
    q
    u
    e
    s
    t
    B
    )
    S
    e
    s
    s
    i
    o
    n
    .
    r
    x
    _
    r
    e
    s
    p
    o
    n
    s
    e
    (
    r
    e
    q
    u
    e
    s
    t
    A
    , r
    e
    q
    u
    e
    s
    t
    B
    , r
    e
    q
    u
    e
    s
    t
    C
    )
    S
    e
    s
    s
    i
    o
    n
    .
    r
    x
    _
    r
    e
    s
    p
    o
    n
    s
    e
    (
    r
    e
    q
    u
    e
    s
    t
    A
    , r
    e
    q
    u
    e
    s
    t
    B
    , r
    e
    q
    u
    e
    s
    t
    C
    , r
    e
    q
    u
    e
    s
    t
    D
    )

    View Slide

  22. 筋肉で解決

    View Slide

  23. /
    / a
    p
    p
    l
    e
    /
    s
    w
    i
    f
    t
    における筋肉の活躍
    f
    u
    n
    c =
    =
    <
    A
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , B
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    >
    (
    l
    h
    s
    : (
    A
    , B
    )
    , r
    h
    s
    : (
    A
    , B
    )
    ) -
    > B
    o
    o
    l
    f
    u
    n
    c =
    =
    <
    A
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , B
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , C
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    >
    (
    l
    h
    s
    : (
    A
    , B
    , C
    )
    , r
    h
    s
    : (
    f
    u
    n
    c =
    =
    <
    A
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , B
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , C
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , D
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    >
    (
    l
    h
    s
    : (
    A
    , B
    , C
    f
    u
    n
    c =
    =
    <
    A
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , B
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , C
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , D
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , E
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    >
    f
    u
    n
    c =
    =
    <
    A
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , B
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , C
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , D
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    , E
    : E
    q
    u
    a
    t
    a
    b
    l
    e
    ,

    View Slide

  24. e
    x
    t
    e
    n
    s
    i
    o
    n S
    e
    s
    s
    i
    o
    n {
    f
    u
    n
    c r
    x
    _
    r
    e
    s
    p
    o
    n
    s
    e
    <
    R
    e
    q
    u
    e
    s
    t
    A
    : R
    P
    C
    R
    e
    q
    u
    e
    s
    t
    , R
    e
    q
    u
    e
    s
    t
    B
    : R
    P
    C
    R
    e
    q
    u
    e
    s
    t
    >
    (
    r
    e
    q
    u
    e
    s
    t
    A
    : R
    e
    q
    u
    e
    s
    t
    A
    , r
    e
    q
    u
    e
    s
    t
    B
    : R
    e
    q
    u
    e
    s
    t
    B
    ) -
    >
    O
    b
    s
    e
    r
    v
    a
    b
    l
    e
    <
    (
    R
    e
    q
    u
    e
    s
    t
    A
    .
    R
    e
    s
    p
    o
    n
    s
    e
    , R
    e
    q
    u
    e
    s
    t
    B
    .
    R
    e
    s
    p
    o
    n
    s
    e
    )
    > {
    .
    .
    .
    }
    f
    u
    n
    c r
    x
    _
    r
    e
    s
    p
    o
    n
    s
    e
    <
    R
    e
    q
    u
    e
    s
    t
    A
    : R
    P
    C
    R
    e
    q
    u
    e
    s
    t
    , R
    e
    q
    u
    e
    s
    t
    B
    : R
    P
    C
    R
    e
    q
    u
    e
    s
    t
    , R
    e
    q
    u
    e
    s
    t
    C
    : R
    P
    C
    R
    e
    q
    u
    e
    s
    t
    >
    (
    r
    e
    q
    u
    e
    s
    t
    A
    : R
    e
    q
    u
    e
    s
    t
    A
    , r
    e
    q
    u
    e
    s
    t
    B
    : R
    e
    q
    u
    e
    s
    t
    B
    , r
    e
    q
    u
    e
    s
    t
    C
    : R
    e
    q
    u
    e
    s
    t
    C
    ) -
    >
    O
    b
    s
    e
    r
    v
    a
    b
    l
    e
    <
    (
    R
    e
    q
    u
    e
    s
    t
    A
    .
    R
    e
    s
    p
    o
    n
    s
    e
    , R
    e
    q
    u
    e
    s
    t
    B
    .
    R
    e
    s
    p
    o
    n
    s
    e
    , R
    e
    q
    u
    e
    s
    t
    C
    .
    R
    e
    s
    p
    o
    n
    s
    e
    .
    .
    .
    }
    }

    View Slide

  25. View Slide

  26. l
    e
    t b
    a
    n
    n
    e
    r
    R
    e
    q
    u
    e
    s
    t = B
    a
    n
    n
    e
    r
    R
    e
    q
    u
    e
    s
    t
    (
    )
    l
    e
    t t
    i
    m
    e
    l
    i
    n
    e
    R
    e
    q
    u
    e
    s
    t = T
    i
    m
    e
    l
    i
    n
    e
    R
    e
    q
    u
    e
    s
    t
    (
    )
    /
    / O
    b
    s
    e
    r
    v
    a
    b
    l
    e
    <
    (
    B
    a
    n
    n
    e
    r
    R
    e
    q
    u
    e
    s
    t
    .
    R
    e
    s
    p
    o
    n
    s
    e
    , T
    i
    m
    e
    l
    i
    n
    e
    R
    e
    q
    u
    e
    s
    t
    .
    R
    e
    s
    p
    o
    n
    s
    e
    )
    >
    l
    e
    t b
    a
    t
    c
    h
    R
    e
    s
    p
    o
    n
    s
    e = S
    e
    s
    s
    i
    o
    n
    .
    r
    x
    _
    r
    e
    s
    p
    o
    n
    s
    e
    (
    b
    a
    n
    n
    e
    r
    R
    e
    q
    u
    e
    s
    t
    , t
    i
    m
    e
    l
    i
    n
    e
    R
    e
    q
    u
    e
    s
    t
    )
    .
    s
    h
    a
    r
    e
    R
    e
    p
    l
    a
    y
    (
    1
    )

    View Slide

  27. b
    a
    t
    c
    h
    R
    e
    s
    p
    o
    n
    s
    e
    .
    m
    a
    p { b
    a
    n
    n
    e
    r
    R
    e
    s
    p
    o
    n
    s
    e
    , t
    i
    m
    e
    l
    i
    n
    e
    R
    e
    s
    p
    o
    n
    s
    e i
    n
    r
    e
    t
    u
    r
    n b
    a
    n
    n
    e
    r
    R
    e
    s
    p
    o
    n
    s
    e
    .
    b
    a
    n
    n
    e
    r
    }
    .
    b
    i
    n
    d
    T
    o
    (
    b
    a
    n
    n
    e
    r
    )
    .
    a
    d
    d
    D
    i
    s
    p
    o
    s
    a
    b
    l
    e
    T
    o
    (
    d
    i
    s
    p
    o
    s
    e
    B
    a
    g
    )
    b
    a
    t
    c
    h
    R
    e
    s
    p
    o
    n
    s
    e
    .
    m
    a
    p { b
    a
    n
    n
    e
    r
    R
    e
    s
    p
    o
    n
    s
    e
    , t
    i
    m
    e
    l
    i
    n
    e
    R
    e
    s
    p
    o
    n
    s
    e i
    n
    r
    e
    t
    u
    r
    n t
    i
    m
    e
    l
    i
    n
    e
    R
    e
    s
    p
    o
    n
    s
    e
    .
    o
    f
    f
    e
    r
    s
    }
    .
    b
    i
    n
    d
    T
    o
    (
    o
    f
    f
    e
    r
    s
    )
    .
    a
    d
    d
    D
    i
    s
    p
    o
    s
    a
    b
    l
    e
    T
    o
    (
    d
    i
    s
    p
    o
    s
    e
    B
    a
    g
    )

    View Slide

  28. b
    a
    t
    c
    h
    R
    e
    s
    p
    o
    n
    s
    e
    .
    m
    a
    p { $
    0
    .
    0
    .
    b
    a
    n
    n
    e
    r }
    .
    b
    i
    n
    d
    T
    o
    (
    b
    a
    n
    n
    e
    r
    )
    .
    a
    d
    d
    D
    i
    s
    p
    o
    s
    a
    b
    l
    e
    T
    o
    (
    d
    i
    s
    p
    o
    s
    e
    B
    a
    g
    )
    b
    a
    t
    c
    h
    R
    e
    s
    p
    o
    n
    s
    e
    .
    m
    a
    p { $
    0
    .
    1
    .
    o
    f
    f
    e
    r
    s }
    .
    b
    i
    n
    d
    T
    o
    (
    o
    f
    f
    e
    r
    s
    )
    .
    a
    d
    d
    D
    i
    s
    p
    o
    s
    a
    b
    l
    e
    T
    o
    (
    d
    i
    s
    p
    o
    s
    e
    B
    a
    g
    )

    View Slide

  29. まとめ

    View Slide

  30. JSON-RPC
    は複数のリクエストを1
    個にまとめられる
    APIKit
    の考え方はJSON-RPC Batch
    にも適用できる
    (A, B) -> Observable<(A.Response, B.Response)>
    筋肉をつけよう

    View Slide