# Demo

    library(ODataQuery)

## Introduction

Here I’ll show some examples to use this package

## Create service

    service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW")
    service$all()
    #>                name           kind
    #> 1            Photos      EntitySet
    #> 2            People      EntitySet
    #> 3          Airlines      EntitySet
    #> 4          Airports      EntitySet
    #> 5                Me      Singleton
    #> 6 GetNearestAirport FunctionImport
    #>                                                                                             url
    #> 1            http://services.odata.org/V4/(S(tbiq44cis1sraps0oabu0vat))/TripPinServiceRW/Photos
    #> 2            http://services.odata.org/V4/(S(tbiq44cis1sraps0oabu0vat))/TripPinServiceRW/People
    #> 3          http://services.odata.org/V4/(S(tbiq44cis1sraps0oabu0vat))/TripPinServiceRW/Airlines
    #> 4          http://services.odata.org/V4/(S(tbiq44cis1sraps0oabu0vat))/TripPinServiceRW/Airports
    #> 5                http://services.odata.org/V4/(S(tbiq44cis1sraps0oabu0vat))/TripPinServiceRW/Me
    #> 6 http://services.odata.org/V4/(S(tbiq44cis1sraps0oabu0vat))/TripPinServiceRW/GetNearestAirport

## Entity set

    people_entity <- service$path("People")

    # Get first page
    people_entity$retrieve(count=TRUE)
    #> $`@odata.count`
    #> [1] 20
    #>
    #> $`@odata.nextLink`
    #> [1] "https://services.odata.org/V4/(S(bdudzig0mg0k43vo4f4uxfoa))/TripPinServiceRW/People?%24count=true&%24skiptoken=8"
    #>
    #> $value
    #>           UserName FirstName  LastName                                   Emails
    #> 1     russellwhyte   Russell     Whyte Russell@example.com, Russell@contoso.com
    #> 2     scottketchum     Scott   Ketchum                        Scott@example.com
    #> 3      ronaldmundy    Ronald     Mundy   Ronald@example.com, Ronald@contoso.com
    #> 4     javieralfred    Javier    Alfred   Javier@example.com, Javier@contoso.com
    #> 5    willieashmore    Willie   Ashmore   Willie@example.com, Willie@contoso.com
    #> 6 vincentcalabrese   Vincent Calabrese Vincent@example.com, Vincent@contoso.com
    #> 7       clydeguess     Clyde     Guess                        Clyde@example.com
    #> 8    keithpinckney     Keith  Pinckney     Keith@example.com, Keith@contoso.com
    #>                                             AddressInfo Gender  Concurrency
    #> 1             187 Suffolk Ln., United States, Boise, ID   Male 6.376092e+17
    #> 2       2817 Milton Dr., United States, Albuquerque, NM   Male 6.376092e+17
    #> 3                                                  NULL   Male 6.376092e+17
    #> 4 89 Jefferson Way Suite 2, United States, Portland, WA   Male 6.376092e+17
    #> 5                                                  NULL   Male 6.376092e+17
    #> 6         55 Grizzly Peak Rd., United States, Butte, MT   Male 6.376092e+17
    #> 7                                                  NULL   Male 6.376092e+17
    #> 8                                                  NULL   Male 6.376092e+17

    # Get all pages
    people_entity$select("UserName", "FirstName", "LastName")$all()
    #>            UserName FirstName  LastName
    #> 1      russellwhyte   Russell     Whyte
    #> 2      scottketchum     Scott   Ketchum
    #> 3       ronaldmundy    Ronald     Mundy
    #> 4      javieralfred    Javier    Alfred
    #> 5     willieashmore    Willie   Ashmore
    #> 6  vincentcalabrese   Vincent Calabrese
    #> 7        clydeguess     Clyde     Guess
    #> 8     keithpinckney     Keith  Pinckney
    #> 9     marshallgaray  Marshall     Garay
    #> 10    ryantheriault      Ryan Theriault
    #> 11    elainestewart    Elaine   Stewart
    #> 12    salliesampson    Sallie   Sampson
    #> 13      jonirosales      Joni   Rosales
    #> 14   georginabarlow  Georgina    Barlow
    #> 15     angelhuffman     Angel   Huffman
    #> 16     laurelosborn    Laurel    Osborn
    #> 17      sandyosborn     Sandy    Osborn
    #> 18     ursulabright    Ursula    Bright
    #> 19  genevievereeves Genevieve    Reeves
    #> 20       kristakemp    Krista      Kemp

## Singleton

    russellwhyte <- people_entity$get("russellwhyte")
    first_person <- people_entity$top(1)$one()
    head(first_person)
    #> $UserName
    #> [1] "russellwhyte"
    #>
    #> $FirstName
    #> [1] "Russell"
    #>
    #> $LastName
    #> [1] "Whyte"
    #>
    #> $Emails
    #> [1] "Russell@example.com" "Russell@contoso.com"
    #>
    #> $AddressInfo
    #>           Address City.CountryRegion City.Name City.Region
    #> 1 187 Suffolk Ln.      United States     Boise          ID
    #>
    #> $Gender
    #> [1] "Male"

    russellwhyte_friends <- russellwhyte$path("Friends")
    russellwhyte_friends$all()
    #>       UserName FirstName LastName                                 Emails
    #> 1 scottketchum     Scott  Ketchum                      Scott@example.com
    #> 2  ronaldmundy    Ronald    Mundy Ronald@example.com, Ronald@contoso.com
    #> 3 javieralfred    Javier   Alfred Javier@example.com, Javier@contoso.com
    #> 4 angelhuffman     Angel  Huffman                      Angel@example.com
    #>                                             AddressInfo Gender  Concurrency
    #> 1       2817 Milton Dr., United States, Albuquerque, NM   Male 6.376092e+17
    #> 2                                                  NULL   Male 6.376092e+17
    #> 3 89 Jefferson Way Suite 2, United States, Portland, WA   Male 6.376092e+17
    #> 4                                                  NULL Female 6.376092e+17

## Function call

    get_nearest_airport <- service$func('GetNearestAirport')
    get_nearest_airport(lat = 33, lon = -118)
    #> $IcaoCode
    #> [1] "KLAX"
    #>
    #> $Name
    #> [1] "Los Angeles International Airport"
    #>
    #> $IataCode
    #> [1] "LAX"
    #>
    #> $Location
    #> $Location$Address
    #> [1] "1 World Way, Los Angeles, CA, 90045"
    #>
    #> $Location$City
    #> $Location$City$CountryRegion
    #> [1] "United States"
    #>
    #> $Location$City$Name
    #> [1] "Los Angeles"
    #>
    #> $Location$City$Region
    #> [1] "California"
    #>
    #>
    #> $Location$Loc
    #> $Location$Loc$type
    #> [1] "Point"
    #>
    #> $Location$Loc$coordinates
    #> [1] -118.4081   33.9425
    #>
    #> $Location$Loc$crs
    #> $Location$Loc$crs$type
    #> [1] "name"
    #>
    #> $Location$Loc$crs$properties
    #> $Location$Loc$crs$properties$name
    #> [1] "EPSG:4326"

## Querying

    people_query <- people_entity$
      top(5)$
      select('FirstName', 'LastName')$
      filter(Concurrency.gt = 500)$
      expand('Friends($count=true)')$
      orderby('LastName')

    people_query$all()
    #>   FirstName  LastName Friends@odata.count
    #> 1    Javier    Alfred                   3
    #> 2    Willie   Ashmore                   2
    #> 3  Georgina    Barlow                   1
    #> 4    Ursula    Bright                   1
    #> 5   Vincent Calabrese                   2
    #>                                                                                                                                                                                                                                                                                                                                                               Friends
    #> 1 willieashmore, vincentcalabrese, georginabarlow, Willie, Vincent, Georgina, Ashmore, Calabrese, Barlow, Willie@example.com, Willie@contoso.com, Vincent@example.com, Vincent@contoso.com, Georgina@example.com, Georgina@contoso.com, 55 Grizzly Peak Rd., United States, Butte, MT, Male, Male, Female, 637609246868923904, 637609246868923904, 637609246868923904
    #> 2                                                      javieralfred, vincentcalabrese, Javier, Vincent, Alfred, Calabrese, Javier@example.com, Javier@contoso.com, Vincent@example.com, Vincent@contoso.com, 89 Jefferson Way Suite 2, United States, Portland, WA, 55 Grizzly Peak Rd., United States, Butte, MT, Male, Male, 637609246868923904, 637609246868923904
    #> 3                                                                                                                                                                                                               javieralfred, Javier, Alfred, Javier@example.com, Javier@contoso.com, 89 Jefferson Way Suite 2, United States, Portland, WA, Male, 637609246868923904
    #> 4                                                                                                                                                                                                                                                                      keithpinckney, Keith, Pinckney, Keith@example.com, Keith@contoso.com, Male, 637609246868923904
    #> 5                                                                                                             javieralfred, willieashmore, Javier, Willie, Alfred, Ashmore, Javier@example.com, Javier@contoso.com, Willie@example.com, Willie@contoso.com, 89 Jefferson Way Suite 2, United States, Portland, WA, Male, Male, 637609246868923904, 637609246868923904

## Other endpoints

### Statistics, the Netherlands

    opendata_service <- ODataQuery$new("http://beta-odata4.cbs.nl/")
    entity_81589NED  <- opendata_service$path("CBS", "81589NED", Observations")
    dataset_81589NED <- entity_81589NED$all()

### Northwind (OData v2)

Connecting with older OData v2 works, but some features have been changed.

    northwind_service <- ODataQuery$new("https://services.odata.org/V2/Northwind/Northwind.svc/")
    customer_entity <- northwind_service$path("Customers")
    customer_entity$
      select("CompanyName", "Address", "Phone")$
      filter(Country.eq = "Germany", City.eq = "Berlin")

### The Hague

So far, I haven’t been able to figure out how their data model works, but at least we can connect to it.

    denhaag_service <- ODataQuery$new("https://denhaag.incijfers.nl/jiveservices/odata/")
    denhaag_service$path('DataSources')