Giter VIP home page Giter VIP logo

Comments (1)

phill-holbrook avatar phill-holbrook commented on June 21, 2024

I think I've found a solution for this by removing Invoke-WebRequest and using System.Net.Http.HttpClient directly.

function Get-APIResource {
    param(
        [Parameter (Mandatory=$true)]$Resource,
        [Parameter (Mandatory=$true)]$SearchQuery
    )

    $BaseURL = ""

    $httpClient = New-Object System.Net.Http.HttpClient
    $httpClient.DefaultRequestHeaders.Add("ApiCode", $env:ApiCode)
    $httpClient.DefaultRequestHeaders.Add("UserName", $env:APIUser)
    $httpClient.DefaultRequestHeaders.Add("APIKey", $env:APIKey)

    $ResourceURL = $BaseURL + $Resource + '/query?search=' + $SearchQuery

    $i = 1

    try {

        do {

            Write-Warning "Getting page $i of $Resource"
            $responseStream = $httpClient.GetStreamAsync($ResourceURL).Result
            $streamReader = [System.IO.StreamReader]::new($responseStream)
            $responseContent = $streamReader.ReadToEnd()
            $Response = $ResponseContent | ConvertFrom-Json
            
            If ($Response.items){

                Write-Warning "($i) Retrieved $($Response.items.count) items"

                $Response.items | ForEach-Object{
                    $_
                    #$ReturnList.Add($_)
                }
            }
    
            If ($Response.item){

                Write-Warning "($i) Retrieved $($Response.item.count) ITEM"

                $Response.item | ForEach-Object{
                    $_
                    #$ReturnList.Add($_)
                }
            }
    
            $ResourceURL = $Response.pagedetails.nextPageUrl

            If ($null -eq $ResourceURL){
                Write-Warning "($i) ResourceURL is null"
            } Else {
                Write-Warning "($i) Next page URL: $ResourceURL"
            }

            $i++

        } while ($null -ne $ResourceURL)

    }
    catch {
        Write-Error "Connecting to API Failed."
        throw "API Error: $($_.Exception.Message)"    
    }

}

I have no idea why I'm unable to reproduce the error calling the class directly. My guess is it's some perfect storm between the language runtime, Invoke-WebRequest, and the remote API in question. Hope this helps someone else.

from azure-functions-durable-powershell.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.