Closed suglasp closed 5 years ago
Hi @suglasp, Thank you for the question. Polaris is what I would call "opinionated", in that it has no restrictions or opinions on how you build your API but I'll write a quick sample here of one way you could implement a paging system that might be somewhat of a best practice in the web world.
# My data that I want to make available via REST
$StringArray = @"
String1
String2
String3
String4
String5
String6
String7
String8
String9
String10
String11
String12
String13
String14
String15
String16
String17
String18
String19
String20
String21
String22
String23
String24
String25
String26
String27
String28
String29
String30
String31
String32
String33
String34
String35
String36
String37
String38
String39
String40
String41
String42
String43
String44
String45
String46
String47
String48
String49
String50
"@ -split "`r`n"
New-PolarisGetRoute -Path '/ArrayOfStrings' -Scriptblock {
# Casting to their type so I get some autocomplete
[PolarisRequest]$Request = $Request
[PolarisResponse]$Response = $Response
# Setting an initial default response in case things go sideways
$JsonResponse = '[]';
# Check the query string for the 'top' parameter
# 'query strings' in the web world are where you would put optional parameters
# A query string parameter must be placed after the ? and in the form of <parameter name>=<parameter value> and each parameter separated by a &
# Exmaple: /ArrayOfStrings?top=10&skip=10
if($Request.Query.Keys -contains 'top') {
[int]$top = $Request.Query['top']
[int]$skip = 0
if($Request.Query.Keys -contains 'skip') {
$skip = $Request.Query['skip']
}
$JsonResponse = $StringArray[$skip..($skip+$top)] | ConvertTo-Json
} else {
$JsonResponse = $StringArray | ConvertTo-Json
}
$Response.Json($JsonResponse)
}
Start-Polaris
Without skip and top it returns the full array of strings:
With ?top=5 it returns the first 5 strings of the array:
With ?top=5&skip=5 it skips the first 5 and then returns the next 5 in the array:
Now as I said at the start this is just one of the ways you could do it. You can name the parameters whatever you want. You can set some defaults. The data of course is also completely up to you the parameters could be passed to a SQL query or used in any other way to fetch your data in a paginated fashion.
All right, thanks for the Example @Tiberriver256
Polaris Feature Request
Build-In module feature for REST API Pagination or a documentation reference
Is your feature request related to a problem? Please describe
Some backend requests take long to process because of large data sets to return results. So i was looking to cache the results, or create a pagination structure.
Describe the solution you'd like
Question is, how or what is the best solution in Polaris to do pagination? Or is there a solution or documentation available?
Describe alternatives you've considered
Tried to cache the result. Problem is, keep the cache consistent.
Additional context