Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve paging of Invoke-WikiMethod #50

Closed
lipkau opened this issue May 23, 2017 · 6 comments
Closed

Improve paging of Invoke-WikiMethod #50

lipkau opened this issue May 23, 2017 · 6 comments

Comments

@lipkau
Copy link
Member

lipkau commented May 23, 2017

Description

The current implementation of Invoke-WikiMethod's paging does not support the pipeline correctly.

Demonstration

$result = Get-WikiPage -Space Foo -PageSize 100
$result.Count # = 1000

This code will have invoked Invoke-WikiMethod 10 time where each invocation returned a result set of 100 entries. Resulting in a collection of 1000 in total.

$result = Get-WikiPage -Space Foo -PageSize 100 | Select-Object -First 50
$result.Count # = 1000

This code will have done the same as the first, but will later select the first 50 entries of the result set and return them - where only 1 invocation of Invoke-WikiMethod would have been enough.

Fix

Invoke-WikiMethod must be updated so that it will no longer append to arrays, but return to StdOut at once:

# None paginated results / first page of pagination
if (($result) -and ($result | Get-Member -Name results)) {
    $result = $result.results
}
if($OutputType) { 
    $result | & $converter
} else {
    $result
}

if ($result._links.next) {
   Write-Verbose "[Invoke-WikiMethod] Invoking pagination"

   # Self-Invoke function for recursion
   $parameters = @{
       URi = "{0}{1}" -f $result._links.base, $result._links.next
       Method = $Method
   }
   if ($Body) {$parameters["Body"] = $Body}
   if ($Headers) {$parameters["Headers"] = $Headers}
   if ($GetParameters) {$parameters["Get$GetParameters"] = $GetParameters}

   Write-Output Invoke-WikiMethod @parameters
}

Additionally

[CmdletBinding(SupportsPaging = $true)] enables a function to support paging on itself.
This exposes parameters such as -Skip, -IncludeTotalCount and -First.
this could be implemented in the likes of:

$PSBoundParameters["Skip"] = $PSCmdlet.PagingParameters.Skip + $result.Count
$PSBoundParameters["First"] = $PSCmdlet.PagingParameters.First - $result.Count
Invoke-WikiMethod @PSBoundParameters
@lipkau lipkau self-assigned this May 23, 2017
@lipkau
Copy link
Member Author

lipkau commented May 23, 2017

@JohnLBevan
Copy link

JohnLBevan commented May 26, 2017

Relating to SupportsPaging, I knocked up some PoC code to ensure I've understood how that should be implemented... https://codereview.stackexchange.com/questions/164252/powershell-supports-paging

We may also be able to make use of some of the logic from the Invoke-DummyRestMethod when writing related tests/mocks.

@JohnLBevan
Copy link

If the function's called without making use of the paging parameters; or if the page size is greater than the web service's supported page size, is there any preference on behaviour?

i.e. With SupportsPaging, the calling code needs to be aware of the page size, and to implement logic to loop through page by page if you want all results. If we want all results returned by a single call to the method, we want the function's internal code to loop through those pages. Would we still want to specify a page size for the function (i.e. which gets passed to the web service's limit parameter), or where we want all results would we not make use of the function's SupportsPaging, even though behind the scenes we are making paged calls to the web service?

@lipkau
Copy link
Member Author

lipkau commented May 30, 2017

If no stepsize is provided, use the systems default or leave the limit out.
If no paging parameter is provided, SupportPaging will still be active but not used.

@lipkau
Copy link
Member Author

lipkau commented May 30, 2017

  • We will return everything unless the SupportPaging parameters restrict it.
  • The StepSize will only influence the amount of results per call. Which is useful for slow connection and for | Select -First 10.
  • In order for the pipe to be able to terminate the calls, we are not allowed to collect the results in an array before writing to output.

@lipkau
Copy link
Member Author

lipkau commented Jun 19, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants