LoginSignup
0
0

【Pleasanter】APIで複数レコードを取得する処理をVB.NETで書いてみた

Last updated at Posted at 2023-06-13

サンプルデータとして登録されていた「顧客名簿」のデータを外部アプリから参照する処理は以下のとおりです。

VB.NET
Imports System.Net.Http
Imports System.Text
Imports System.Threading.Tasks
Imports Newtonsoft.Json

Public Class Form1
    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim jsonStr = Await getRecords()

        'Json文字列をJson形式データに復元する
        Dim jsonObj = JsonConvert.DeserializeObject(jsonStr)

        'データ格納用のDatatableを作成する。
        Dim dt As New DataTable
        makeSchema(dt)

        For Each js In jsonObj("Response")("Data")
            Dim row As DataRow
            row = dt.NewRow
            row("UpdatedTime") = js("UpdatedTime")
            row("ClassB") = js("ClassHash")("ClassB")
            row("ClassE") = js("ClassHash")("ClassE")
            dt.Rows.Add(row)
        Next

        'DataGridViewへ表示する。
        dgv.DataSource = dt
        Me.lblCnt.Text = dt.Rows.Count
    End Sub

    Private Async Function getRecords() As Task(Of String)
        Dim uri As String = "http://{サーバ指定}/api/items/{サイトID}/get"
        Dim param As Dictionary(Of String, String) = New Dictionary(Of String, String)() From {
            {"ApiVersion", "1.1"},
            {"ApiKey", "ApiKeyをコピペ"}
        }
        Using httpClient As HttpClient = New HttpClient()
            Dim json As String = JsonConvert.SerializeObject(param, Formatting.None, New JsonSerializerSettings With {.NullValueHandling = NullValueHandling.Ignore})
            Dim content As HttpContent = New StringContent(json, Encoding.UTF8, "application/json")
            Dim response As HttpResponseMessage = Await httpClient.PostAsync(uri, content)
            Dim result As String = Await response.Content.ReadAsStringAsync()
            Return result
        End Using
    End Function

    Private Sub makeSchema(ByRef dt As DataTable)
        dt.Columns.Add("UpdatedTime", Type.GetType("System.String"))
        dt.Columns.Add("ClassB", Type.GetType("System.String"))
        dt.Columns.Add("ClassE", Type.GetType("System.String"))
    End Sub

End Class

レコード追加の例

VB.NET
    ''' <summary>
    ''' PleasanterAPIでレコードを追加する。
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Async Function insertRecords() As Task(Of String)

        Dim uri As String = serverUrl & "/api/items/1/create"

        Dim str As String = "{""ApiVersion"": 1.1, ""ApiKey"": """ & ApiKey & """,""ClassHash"": { ""ClassB"": ""山田太郎"" , ""ClassE"": ""01203334444"" , ""ClassR"": ""vb.netでapiテスト"" }}"
        Dim param As Dictionary(Of String, Object) = JsonConvert.DeserializeObject(Of Dictionary(Of String, Object))(str)

        Using httpClient As HttpClient = New HttpClient()
            Dim json As String = JsonConvert.SerializeObject(param, Formatting.None, New JsonSerializerSettings With {.NullValueHandling = NullValueHandling.Ignore})

            Dim content As HttpContent = New StringContent(json, Encoding.UTF8, "application/json")

            Dim response As HttpResponseMessage = Await httpClient.PostAsync(uri, content)
            Dim result As String = Await response.Content.ReadAsStringAsync()
            Return result
        End Using

    End Function

残っている課題

  1. 「変更履歴」レコードの取り出し方が不明
  2. 「コメント」レコードの取り出し方が不明

PleasanterのApi説明についての詳細は以下

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0