Skip to content

Instantly share code, notes, and snippets.

@vbjay
Created October 20, 2018 13:12
Show Gist options
  • Save vbjay/295dd6a15a6729d031b4e0881efe5e48 to your computer and use it in GitHub Desktop.
Save vbjay/295dd6a15a6729d031b4e0881efe5e48 to your computer and use it in GitHub Desktop.
Simple usage of restsharp in linqpad showing the power and ease of creating REST requests and flushing them into instances of your classes. Use jsonutils.com to convert json into classes.
<Query Kind="VBProgram">
<NuGetReference>RestSharp</NuGetReference>
<Namespace>RestSharp</Namespace>
</Query>
Async Sub Main
Dim client As New RestClient("https://jsonplaceholder.typicode.com")
Dim req As New RestRequest("/todos/", method.GET)
Dim respTsk = client.ExecuteTaskAsync(Of List(Of Todo))(req).Dump("ToDos response")
Dim response = Await respTsk
Dim list = response.Data
list.Dump("ToDo List")
Dim byStatus=list.GroupBy(Function(t) t.completed).Dump("Grouped by Complete status")
byStatus.Select(Function(gt) New With {.ToDos=gt, .Count=gt.Count, .Status=$"{gt.Count} out of {list.Count} tasks are{If(gt.Key, " "," not ")}complete.", .Percent=(gt.Count/list.Count).ToString("P")}).Dump("Status")
Dim rnd As New random
Dim id As Integer = rnd.Next(list.Min(Function(t) t.id), list.Max(Function(t) t.id) + 1)
req.Resource &= "{id}"
req.AddUrlSegment("id", id.ToString)
Dim resp2Tsk = client.ExecuteTaskAsync(Of Todo)(req).Dump("Single ToDo response")
Dim resp2 = Await resp2Tsk
resp2.Data.Dump("Random ToDo")
End Sub
' Define other methods and classes here
Public Class Todo
Public Property userId As Integer
Public Property id As Integer
Public Property title As String
Public Property completed As Boolean
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment