Skip to content

Commit

Permalink
Merge pull request #2 from NeedleInAJayStack/feature/linux-compat
Browse files Browse the repository at this point in the history
Linux compatibility
  • Loading branch information
NeedleInAJayStack authored Dec 28, 2022
2 parents 35b9e00 + e30666a commit bd945ee
Show file tree
Hide file tree
Showing 18 changed files with 705 additions and 133 deletions.
23 changes: 15 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ on:
push: { branches: [ main ] }

jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
darwin-test:
runs-on: macos-latest
steps:
- uses: fwal/setup-swift@v1
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: latest
- uses: actions/checkout@v2
- name: Run tests
run: swift test
- name: Darwin build & test
run: swift test --skip IntegrationTests
linux-build:
runs-on: ubuntu-latest
container:
image: swift:latest
steps:
- uses: actions/checkout@v2
- name: Linux build
run: swift build --target HaystackClientNIO
95 changes: 95 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{
"pins" : [
{
"identity" : "async-http-client",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swift-server/async-http-client.git",
"state" : {
"revision" : "5bee16a79922e3efcb5cea06ecd27e6f8048b56b",
"version" : "1.13.1"
}
},
{
"identity" : "swift-atomics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-atomics.git",
"state" : {
"revision" : "ff3d2212b6b093db7f177d0855adbc4ef9c5f036",
"version" : "1.0.3"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "937e904258d22af6e447a0b72c0bc67583ef64a2",
"version" : "1.0.4"
}
},
{
"identity" : "swift-crypto",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-crypto.git",
"state" : {
"revision" : "92a04c10fc5ce0504f8396aac7392126033e547c",
"version" : "2.2.2"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"revision" : "6fe203dc33195667ce1759bf0182975e4653ba1c",
"version" : "1.4.4"
}
},
{
"identity" : "swift-nio",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio.git",
"state" : {
"revision" : "e855380cb5234e96b760d93e0bfdc403e381e928",
"version" : "2.45.0"
}
},
{
"identity" : "swift-nio-extras",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-extras.git",
"state" : {
"revision" : "91dd2d61fb772e1311bb5f13b59266b579d77e42",
"version" : "1.15.0"
}
},
{
"identity" : "swift-nio-http2",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-http2.git",
"state" : {
"revision" : "d6656967f33ed8b368b38e4b198631fc7c484a40",
"version" : "1.23.1"
}
},
{
"identity" : "swift-nio-ssl",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-ssl.git",
"state" : {
"revision" : "4fb7ead803e38949eb1d6fabb849206a72c580f3",
"version" : "2.23.0"
}
},
{
"identity" : "swift-nio-transport-services",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-transport-services.git",
"state" : {
"revision" : "c0d9a144cfaec8d3d596aadde3039286a266c15c",
"version" : "1.15.0"
}
}
],
"version" : 2
}
55 changes: 49 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,65 @@ import PackageDescription

let package = Package(
name: "Haystack",
platforms: [
.macOS(.v12),
.iOS(.v15),
.tvOS(.v15),
.watchOS(.v8)
],
products: [
.library(
name: "Haystack",
targets: ["Haystack"]
),
.library(
name: "HaystackClient",
targets: ["HaystackClient"]
name: "HaystackClientDarwin",
targets: [
"HaystackClient",
"HaystackClientDarwin"
]
),
.library(
name: "HaystackClientNIO",
targets: [
"HaystackClient",
"HaystackClientNIO"
]
),
],
dependencies: [],
dependencies: [
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "3.0.0"),
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.9.0")
],
targets: [
.target(
name: "Haystack",
dependencies: []
),
.target(
name: "HaystackClient",
dependencies: ["Haystack"]
dependencies: [
"Haystack",
.product(name: "Crypto", package: "swift-crypto"),
]
),
.target(
name: "HaystackClientDarwin",
dependencies: [
"Haystack",
"HaystackClient",
]
),
.target(
name: "HaystackClientNIO",
dependencies: [
"Haystack",
"HaystackClient",
.product(name: "AsyncHTTPClient", package: "async-http-client"),
]
),

// Tests
.testTarget(
name: "HaystackTests",
dependencies: ["Haystack"]
Expand All @@ -33,8 +72,12 @@ let package = Package(
dependencies: ["HaystackClient"]
),
.testTarget(
name: "HaystackClientIntegrationTests",
dependencies: ["HaystackClient"]
name: "HaystackClientNIOIntegrationTests",
dependencies: ["HaystackClientNIO"]
),
.testTarget(
name: "HaystackClientDarwinIntegrationTests",
dependencies: ["HaystackClientDarwin"]
),
]
)
119 changes: 118 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,120 @@
# Swift Haystack

An implementation of Project Haystack in Swift.
An implementation of [Project Haystack](https://project-haystack.org/) in Swift.

## Getting Started

To use this package, add it to your `Package.swift` dependencies:

```swift
dependencies: [
.package(url: "https://github.com/NeedleInAJayStack/swift-haystack.git", from: "0.0.0"),
],
targets: [
.target(
name: "MyTarget",
dependencies: [
.product(name: "Haystack", package: "swift-haystack"),
]
),
]
```

You can then import and use the different libraries:

```swift
import Haystack

func testGrid() throws -> Grid {
return try ZincReader(
"""
ver:"3.0" foo:"bar"
dis dis:"Equip Name", equip, siteRef, installed
"RTU-1", M, @153c-699a HQ, 2005-06-01
"RTU-2", M, @153c-699b Library, 1997-07-12
"""
).readGrid()
}
```

See below for available libraries and descriptions.

## Available Packages

### Haystack

This contains the
[Haystack type-system primitives](https://project-haystack.org/doc/docHaystack/Kinds)
and utilities to interact with them.

### HaystackClientDarwin

A Darwin-only client driver for the
[Haystack HTTP API](https://project-haystack.org/doc/docHaystack/HttpApi) that
requires minimal dependencies. Use this if you are only deploying to MacOS, iOS, etc and want
to reduce dependencies.

Here's an example of how to use it:

```swift
import HaystackClientDarwin

func client() throws -> Client {
return try Client(
baseUrl: "http://mydomain.com/api/",
username: "username",
password: "password"
)
}
```

### HaystackClientNIO

A cross-platform client driver for the
[Haystack HTTP API](https://project-haystack.org/doc/docHaystack/HttpApi) that
has larger dependency requirements. Use this if you are only deploying to Linux or if you
are deploying to Darwin platforms and are willing to accept more dependencies.

Here's an example of how to use it:

```swift
import HaystackClientNIO

func client() throws -> Client {
return try Client(
baseUrl: "http://mydomain.com/api/",
username: "username",
password: "password",
eventLoopGroup: MultiThreadedEventLoopGroup(numberOfThreads: 1)
)
}
```

### HaystackClient

This defines the main functionality of Haystack API clients. It should not be imported directly;
its assets are imported automatically by `HaystackClientDarwin` or `HaystackClientNIO`.

Once you create a client, you can use it to make requests:

```swift
func yesterdaysValues() async throws -> Grid {
let client = ...

// Open and authenticate. This must be called before requests can be made
try await client.open()

// Request the historical values for @28e7fb7d-e20316e0
let grid = try await client.hisRead(id: Ref("28e7fb7d-e20316e0"), range: .yesterday)

// Close the client session and log out
try await client.close()

return grid
}
```

## License

This package is licensed under the Academic Free License 3.0 for maximum compatibility with
Project Haystack itself.
7 changes: 3 additions & 4 deletions Sources/HaystackClient/Authentication/AuthHash.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import CryptoKit
import Crypto

@available(macOS 10.15, *)
enum AuthHash: String {
case SHA512 = "SHA-512"
case SHA256 = "SHA-256"

var hash: any HashFunction.Type {
switch self {
case .SHA256:
return CryptoKit.SHA256.self
return Crypto.SHA256.self
case .SHA512:
return CryptoKit.SHA512.self
return Crypto.SHA512.self
}
}
}
Loading

0 comments on commit bd945ee

Please sign in to comment.