' Collection of code snippets by Arne Vajhøj 
' (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Net.Http
Imports System.Net.Http.Headers
Imports System.Runtime.Serialization

Namespace Test2
    <DataContract(Name := "t1", [Namespace] := "")> _
    Public Class T1
        <DataMember(Name := "f1")> _
        Public Property F1() As Integer
        <DataMember(Name := "f2")> _
        Public Property F2() As String
        Public Sub New()
            Me.New(0, "")
        End Sub
        Public Sub New(f1 As Integer, f2 As String)
            Me.F1 = f1
            Me.F2 = f2
        End Sub
        Public Overrides Function ToString() As String
            Return String.Format("{{{0},{1}}}", F1, F2)
        End Function

    End Class
    <CollectionDataContract(Name := "t1array", [Namespace] := "")> _
    Public Class T1Array
        Inherits List(Of T1)
        Public Sub New()
            Me.New(New T1(-1) {})
        End Sub
        Public Sub New(array As T1())
            MyBase.New(array)
        End Sub
    End Class
    Public Class ClientWebApi
        Private Shared Sub TestGetOne(urlprefix As String, f1 As Integer, acctyp As String)
            Dim client As New HttpClient()
            client.DefaultRequestHeaders.Accept.Clear()
            client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue(acctyp))
            Dim response As HttpResponseMessage = client.GetAsync(urlprefix & "/t1/" & f1).Result
            Console.WriteLine(response.IsSuccessStatusCode)
            Console.WriteLine(response.Content.ReadAsAsync(Of T1)().Result)
        End Sub
        Private Shared Sub TestGetAll(urlprefix As String, acctyp As String)
            Dim client As New HttpClient()
            client.DefaultRequestHeaders.Accept.Clear()
            client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue(acctyp))
            Dim response As HttpResponseMessage = client.GetAsync(urlprefix & "/t1").Result
            Console.WriteLine(response.IsSuccessStatusCode)
            Dim a As T1()
            If acctyp = "application/xml" Then
                a = response.Content.ReadAsAsync(Of T1Array)().Result.ToArray()
            Else
                a = response.Content.ReadAsAsync(Of T1())().Result
            End If
            For Each o As T1 In a
                Console.Write(o)
            Next
            Console.WriteLine()
        End Sub
        Private Shared Sub TestGetSome(urlprefix As String, start As Integer, finish As Integer, acctyp As String)
            Dim client As New HttpClient()
            client.DefaultRequestHeaders.Accept.Clear()
            client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue(acctyp))
            Dim response As HttpResponseMessage = client.GetAsync(urlprefix & "/t1?start=" & start & "&finish=" & finish).Result
            Console.WriteLine(response.IsSuccessStatusCode)
            Dim a As T1()
            If acctyp = "application/xml" Then
                a = response.Content.ReadAsAsync(Of T1Array)().Result.ToArray()
            Else
                a = response.Content.ReadAsAsync(Of T1())().Result
            End If
            For Each o As T1 In a
                Console.Write(o)
            Next
            Console.WriteLine()
        End Sub
        Private Shared Sub TestPost(urlprefix As String, contyp As String, o As T1, acctyp As String)
            Dim client As New HttpClient()
            client.DefaultRequestHeaders.Accept.Clear()
            client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue(acctyp))
            Dim response As HttpResponseMessage
            If contyp = "application/xml" Then
                response = client.PostAsXmlAsync(urlprefix & "/t1", o).Result
            Else
                response = client.PostAsJsonAsync(urlprefix & "/t1", o).Result
            End If
            Console.WriteLine(response.IsSuccessStatusCode)
            Console.WriteLine(response.Content.ReadAsAsync(Of T1)().Result)
        End Sub
        Private Shared Sub TestPut(urlprefix As String, contyp As String, o As T1)
            Dim client As New HttpClient()
            Dim response As HttpResponseMessage
            If contyp = "application/xml" Then
                response = client.PutAsXmlAsync(urlprefix & "/t1/" & o.F1, o).Result
            Else
                response = client.PutAsJsonAsync(urlprefix & "/t1/" & o.F1, o).Result
            End If
            Console.WriteLine(response.IsSuccessStatusCode)
        End Sub
        Private Shared Sub TestDelete(urlprefix As String, f1 As Integer)
            Dim client As New HttpClient()
            Dim response As HttpResponseMessage = client.DeleteAsync(urlprefix & "/t1/" & f1).Result
            Console.WriteLine(response.IsSuccessStatusCode)
        End Sub
        Private Shared Sub Test(urlprefix As String, typ As String)
            Console.WriteLine("**** " & urlprefix & " (" & typ & ") ****")
            TestGetOne(urlprefix, 123, typ)
            TestGetAll(urlprefix, typ)
            TestGetSome(urlprefix, 10, 12, typ)
            TestPost(urlprefix, typ, New T1(123, "ABC"), typ)
            TestPut(urlprefix, typ, New T1(123, "ABC"))
            TestDelete(urlprefix, 123)
        End Sub
        Private Shared Sub Test(urlprefix As String)
            Test(urlprefix, "application/xml")
            Test(urlprefix, "application/json")
        End Sub
        Public Shared Sub Main(args As String())
            Test("http://localhost:8080/wsrest/testapi1")
            Test("http://localhost:8080/wsrest/testapi2")
            Test("http://localhost:8080/wsrest/testapi3")
            Test("http://localhost/testapi.ashx")
            Test("http://localhost/testapi.svc")
            Test("http://localhost/testapi")
            Test("http://localhost:81/testapi.php")
        End Sub
    End Class
End Namespace
