|
1 | 1 | # Swift Haystack |
2 | 2 |
|
3 | | -An implementation of Project Haystack in Swift. |
| 3 | +An implementation of [Project Haystack](https://project-haystack.org/) in Swift. |
| 4 | + |
| 5 | +## Getting Started |
| 6 | + |
| 7 | +To use this package, add it to your `Package.swift` dependencies: |
| 8 | + |
| 9 | +```swift |
| 10 | +dependencies: [ |
| 11 | + .package(url: "https://github.com/NeedleInAJayStack/swift-haystack.git", from: "0.0.0"), |
| 12 | +], |
| 13 | +targets: [ |
| 14 | + .target( |
| 15 | + name: "MyTarget", |
| 16 | + dependencies: [ |
| 17 | + .product(name: "Haystack", package: "swift-haystack"), |
| 18 | + ] |
| 19 | + ), |
| 20 | +] |
| 21 | +``` |
| 22 | + |
| 23 | +You can then import and use the different libraries: |
| 24 | + |
| 25 | +```swift |
| 26 | +import Haystack |
| 27 | + |
| 28 | +func testGrid() throws -> Grid { |
| 29 | + return try ZincReader( |
| 30 | + """ |
| 31 | + ver:"3.0" foo:"bar" |
| 32 | + dis dis:"Equip Name", equip, siteRef, installed |
| 33 | + "RTU-1", M, @153c-699a HQ, 2005-06-01 |
| 34 | + "RTU-2", M, @153c-699b Library, 1997-07-12 |
| 35 | + """ |
| 36 | + ).readGrid() |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +See below for available libraries and descriptions. |
| 41 | + |
| 42 | +## Available Packages |
| 43 | + |
| 44 | +### Haystack |
| 45 | + |
| 46 | +This contains the |
| 47 | +[Haystack type-system primitives](https://project-haystack.org/doc/docHaystack/Kinds) |
| 48 | +and utilities to interact with them. |
| 49 | + |
| 50 | +### HaystackClientDarwin |
| 51 | + |
| 52 | +A Darwin-only client driver for the |
| 53 | +[Haystack HTTP API](https://project-haystack.org/doc/docHaystack/HttpApi) that |
| 54 | +requires minimal dependencies. Use this if you are only deploying to MacOS, iOS, etc and want |
| 55 | +to reduce dependencies. |
| 56 | + |
| 57 | +Here's an example of how to use it: |
| 58 | + |
| 59 | +```swift |
| 60 | +import HaystackClientDarwin |
| 61 | + |
| 62 | +func client() throws -> Client { |
| 63 | + return try Client( |
| 64 | + baseUrl: "http://mydomain.com/api/", |
| 65 | + username: "username", |
| 66 | + password: "password" |
| 67 | + ) |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +### HaystackClientNIO |
| 72 | + |
| 73 | +A cross-platform client driver for the |
| 74 | +[Haystack HTTP API](https://project-haystack.org/doc/docHaystack/HttpApi) that |
| 75 | +has larger dependency requirements. Use this if you are only deploying to Linux or if you |
| 76 | +are deploying to Darwin platforms and are willing to accept more dependencies. |
| 77 | + |
| 78 | +Here's an example of how to use it: |
| 79 | + |
| 80 | +```swift |
| 81 | +import HaystackClientNIO |
| 82 | + |
| 83 | +func client() throws -> Client { |
| 84 | + return try Client( |
| 85 | + baseUrl: "http://mydomain.com/api/", |
| 86 | + username: "username", |
| 87 | + password: "password", |
| 88 | + eventLoopGroup: MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 89 | + ) |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### HaystackClient |
| 94 | + |
| 95 | +This defines the main functionality of Haystack API clients. It should not be imported directly; |
| 96 | +its assets are imported automatically by `HaystackClientDarwin` or `HaystackClientNIO`. |
| 97 | + |
| 98 | +Once you create a client, you can use it to make requests: |
| 99 | + |
| 100 | +```swift |
| 101 | +func yesterdaysValues() async throws -> Grid { |
| 102 | + let client = ... |
| 103 | + |
| 104 | + // Open and authenticate. This must be called before requests can be made |
| 105 | + try await client.open() |
| 106 | + |
| 107 | + // Request the historical values for @28e7fb7d-e20316e0 |
| 108 | + let grid = try await client.hisRead(id: Ref("28e7fb7d-e20316e0"), range: .yesterday) |
| 109 | + |
| 110 | + // Close the client session and log out |
| 111 | + try await client.close() |
| 112 | + |
| 113 | + return grid |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +## License |
| 118 | + |
| 119 | +This package is licensed under the Academic Free License 3.0 for maximum compatibility with |
| 120 | +Project Haystack itself. |
0 commit comments