Skip to content

ODataSwift is simple utility swift wrapper to generate OData Query V4 that works on iOS and OS X. Makes using ODataSwift builder pattern APIs extremely easy and much more palatable to use in Swift

License

Notifications You must be signed in to change notification settings

developer-celusion/ODataSwift

Repository files navigation

ODataSwift

ODataSwift is simple utility swift wrapper to generate OData Query V4 that works on iOS and OS X. Makes using ODataSwift builder pattern APIs extremely easy and much more palatable to use in Swift

💡 Features

  • Used builder pattern to generate query
  • Support $select, $expand, $filter
  • Support odata pagination like $top, $skip
  • Support unbound and bound functions
  • Support almost all predicates like eq, lt, gt
  • Support date time funtions like now()

📖 Usage

🔑 Basics

Setting OData URL

ODataQueryBuilder.configure(url: "https://services.odata.org/V4")

🔑 Querying Data

$filter

The $filter system query option allows clients to filter a collection of resources that are addressed by a request URL

let filter = FilterExp("FirstName").eq().value("Scott").and([FilterExp("FirstName").eq().value("Scott")])
print(ODataQueryBuilder().entity("People").filter(filter).build())

$orderby

The $orderby system query option allows clients to request resources in either ascending order using asc or descending order using desc

let order = Order("EndsAt", orderType: .desc)
print(ODataQueryBuilder().entity("People").order(order).build())

$top

The $top system query option requests the number of items in the queried collection to be included in the result

print(ODataQueryBuilder().entity("People").top(2).build())

$skip

The $skip query option requests the number of items in the queried collection that are to be skipped and not included in the result

print(ODataQueryBuilder().entity("People").skip(18).build())

$count

The $count system query option allows clients to request a count of the matching resources included with the resources in the response It is available in three as per odata request .onlyCount() .withCount() inlineCount(property)

print(ODataQueryBuilder().entity("People").withCount().build())

$expand

The $expand system query option specifies the related resources to be included in line with retrieved resources along with $select, $filter like system query options

let filter = FilterExp("FirstName").eq().value("Scott").and([FilterExp("FirstName").eq().value("Scott")])
let expand = Expand("Trips").filter(filter)
print(ODataQueryBuilder().entity("People").expand(expand).build())

$select

The $select system query option allows the clients to requests a limited set of properties for each entity

print(ODataQueryBuilder().entity("Airports").selects(["Name","IcaoCode"]).build())

$search

The $search system query option restricts the result to include only those entities matching the specified search expression

print(ODataQueryBuilder().entity("People").search(Search("Name")).build())

Advanced Query Generation by OData Functions

let url = ODataQueryBuilder().entity("People").filter(FilterExp(PropertyFunc("name", .length)).eq().value(30)).build()
print(url)
let url = ODataQueryBuilder().entity("People").filter(FilterExp(PropertyFunc("designation", .substring).value(1)).eq().value("di")).build()
print(url)
let url = ODataQueryBuilder().entity("People").filter(FilterExp(PropertyFunc("CreatedOn", .year)).eq().value(2020)).build()
print(url)

URL Encode

Use default .encode() method to get string presentation of OData URL for network calls

let urlEncoded = ODataQueryBuilder().entity("People").search(Search("Name")).encode()
print(urlEncoded)

Requirements

v1.0.0 It is designed in Swift 5 and supports iOS, macOS, watchOS, tvOS

CocoaPods

ODataSwift is available through CocoaPods. To install it, simply add the following lines to your Podfile:

use_frameworks!
pod 'ODataSwift'

Swift Package Manager

ODataSwift is also available through Swift Package Manager.

Xcode

Select File > Swift Packages > Add Package Dependency...,
add https://github.com/developer-celusion/ODataSwift.git

CLI

First, create Package.swift that its package declaration includes:

// swift-tools-version:5.0
import PackageDescription

let package = Package(
    name: "MyLibrary",
    products: [
        .library(name: "MyLibrary", targets: ["MyLibrary"]),
    ],
    dependencies: [
        .package(url: "https://github.com/developer-celusion/ODataSwift.git", from: "1.0.0"),
    ],
    targets: [
        .target(name: "MyLibrary", dependencies: ["ODataSwift"]),
    ]
)

Then, type

$ swift build

To manually add to your project

  1. Add Lib/ODataSwift.xcodeproj to your project
  2. Link ODataSwift.framework with your target
  3. Add Copy Files Build Phase to include the framework to your application bundle

See iOS Example Project as reference.

Author

Swapnil Nandgave, developer@celusion.com

License

ODataSwift is available under the MIT license. See the LICENSE file for more info.

About

ODataSwift is simple utility swift wrapper to generate OData Query V4 that works on iOS and OS X. Makes using ODataSwift builder pattern APIs extremely easy and much more palatable to use in Swift

Resources

License

Stars

Watchers

Forks

Packages

No packages published