Skip to content

Commit 2d4ce6e

Browse files
authored
feat: add support for async/await (#49)
1 parent da50d69 commit 2d4ce6e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+3493
-128
lines changed

.circleci/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,9 @@ workflows:
314314
- tests-linux:
315315
name: tests-linux-5.5
316316
swift-image: "swift:5.5"
317+
- tests-linux:
318+
name: tests-linux-5.6
319+
swift-image: "swift:5.6"
317320
- tests-macOS
318321
nightly:
319322
triggers:

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
## 1.2.0 [unreleased]
22

3+
### Features
4+
1. [#49](https://github.com/influxdata/influxdb-client-swift/pull/49): Add support `async/await` functions
5+
6+
### API
7+
1. [#49](https://github.com/influxdata/influxdb-client-swift/pull/49): Update InfluxDB API to latest version [OpenAPI]
8+
39
### CI
410
1. [#47](https://github.com/influxdata/influxdb-client-swift/pull/47): Use new Codecov uploader for reporting code coverage
11+
1. [#49](https://github.com/influxdata/influxdb-client-swift/pull/49): Add Swift 5.6 to CI
512

613
## 1.1.0 [2022-02-18]
714

Examples/AsyncAwait/.swiftlint.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
opt_in_rules:
2+
- array_init
3+
- collection_alignment
4+
- contains_over_first_not_nil
5+
- closure_end_indentation
6+
- closure_spacing
7+
- conditional_returns_on_newline
8+
- empty_count
9+
- empty_string
10+
- explicit_init
11+
- explicit_self
12+
- fatal_error_message
13+
- first_where
14+
- implicit_return
15+
- missing_docs
16+
- modifier_order
17+
- multiline_arguments
18+
- multiline_literal_brackets
19+
- multiline_parameters
20+
- operator_usage_whitespace
21+
- pattern_matching_keywords
22+
- redundant_nil_coalescing
23+
- redundant_type_annotation
24+
- sorted_first_last
25+
- sorted_imports
26+
- trailing_closure
27+
- unneeded_parentheses_in_closure_argument
28+
- unused_import
29+
- unused_declaration
30+
- vertical_parameter_alignment_on_call
31+
- vertical_whitespace_closing_braces
32+
- vertical_whitespace_opening_braces
33+
34+
excluded:
35+
- Package.swift
36+
- .build/

Examples/AsyncAwait/Package.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// swift-tools-version:5.5
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "AsyncAwait",
7+
platforms: [
8+
.macOS(.v10_15)
9+
],
10+
products: [
11+
.executable(name: "async-await", targets: ["AsyncAwait"])
12+
],
13+
dependencies: [
14+
.package(name: "influxdb-client-swift", path: "../../"),
15+
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.1.2")
16+
],
17+
targets: [
18+
.executableTarget(name: "AsyncAwait", dependencies: [
19+
.product(name: "InfluxDBSwiftApis", package: "influxdb-client-swift"),
20+
.product(name: "ArgumentParser", package: "swift-argument-parser"),
21+
])
22+
]
23+
)

Examples/AsyncAwait/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# AsyncAwait
2+
3+
This is an example how to use `async/await` with the InfluxDB client.
4+
5+
## Prerequisites:
6+
- Docker
7+
- Cloned examples:
8+
```bash
9+
git clone [email protected]:influxdata/influxdb-client-swift.git
10+
cd Examples/AsyncAwait
11+
```
12+
13+
## Sources:
14+
- [Package.swift](/Examples/AsyncAwait/Package.swift)
15+
- [AsyncAwait.swift](/Examples/AsyncAwait/Sources/AsyncAwait/AsyncAwait.swift)
16+
17+
## How to test:
18+
1. Start InfluxDB:
19+
```bash
20+
docker run --rm \
21+
--name influxdb_v2 \
22+
--detach \
23+
--publish 8086:8086 \
24+
influxdb:latest
25+
```
26+
1. Configure your username, password, organization, bucket and token:
27+
```bash
28+
docker run --rm \
29+
--link influxdb_v2 \
30+
curlimages/curl -s -i -X POST http://influxdb_v2:8086/api/v2/setup \
31+
-H 'accept: application/json' \
32+
-d '{"username": "my-user", "password": "my-password", "org": "my-org", "bucket": "my-bucket", "token": "my-token"}'
33+
```
34+
1. Start SwiftCLI by:
35+
```bash
36+
docker run --rm \
37+
--link influxdb_v2 \
38+
--privileged \
39+
--interactive \
40+
--tty \
41+
--volume $PWD/../..:/client \
42+
--workdir /client/Examples/AsyncAwait \
43+
swift:5.6 /bin/bash
44+
```
45+
1. Use `async/await` with `WriteAPI`, `QueryAPI` and `BucketsAPI`:
46+
```bash
47+
swift run async-await --bucket my-bucket --org my-org --token my-token --url http://influxdb_v2:8086
48+
```
49+
50+
## Expected output
51+
52+
```bash
53+
Written data:
54+
> Optional("demo,type=point value=2i")
55+
Query results:
56+
> value: 2
57+
Buckets:
58+
> Optional("10c59301e9077c50"): my-bucket
59+
> Optional("552d1011d2cdab2d"): _tasks
60+
> Optional("a1a02f67f7189b89"): _monitoring
61+
```
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//
2+
// Created by Jakub Bednář on 04/22/2022.
3+
//
4+
5+
import ArgumentParser
6+
import Foundation
7+
import InfluxDBSwift
8+
import InfluxDBSwiftApis
9+
10+
@main
11+
struct AsyncAwait: AsyncParsableCommand {
12+
@Option(name: .shortAndLong, help: "The name or id of the bucket destination.")
13+
private var bucket: String
14+
15+
@Option(name: .shortAndLong, help: "The name or id of the organization destination.")
16+
private var org: String
17+
18+
@Option(name: .shortAndLong, help: "Authentication token.")
19+
private var token: String
20+
21+
@Option(name: .shortAndLong, help: "HTTP address of InfluxDB.")
22+
private var url: String
23+
}
24+
25+
extension AsyncAwait {
26+
mutating func run() async throws {
27+
//
28+
// Initialize Client with default Bucket and Organization
29+
//
30+
let client = InfluxDBClient(
31+
url: url,
32+
token: token,
33+
options: InfluxDBClient.InfluxDBOptions(bucket: self.bucket, org: self.org))
34+
35+
//
36+
// Asynchronous write
37+
//
38+
let point = InfluxDBClient
39+
.Point("demo")
40+
.addTag(key: "type", value: "point")
41+
.addField(key: "value", value: .int(2))
42+
try await client.makeWriteAPI().write(point: point)
43+
print("Written data:\n > \(try point.toLineProtocol())")
44+
45+
//
46+
// Asynchronous query
47+
//
48+
let query = """
49+
from(bucket: "\(self.bucket)")
50+
|> range(start: -10m)
51+
|> filter(fn: (r) => r["_measurement"] == "demo")
52+
"""
53+
let records = try await client.queryAPI.query(query: query)
54+
print("Query results:")
55+
try records.forEach { print(" > \($0.values["_field"]!): \($0.values["_value"]!)") }
56+
57+
//
58+
// List all Buckets
59+
//
60+
let api = InfluxDB2API(client: client)
61+
let buckets = try await api.bucketsAPI.getBuckets()
62+
print("Buckets:")
63+
buckets?.buckets?.forEach { print(" > \($0.id): \($0.name)") }
64+
65+
client.close()
66+
}
67+
}

Examples/CreateNewBucket/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ This is an example how to create new bucket with permission to write.
3434
```
3535
1. Navigate to http://localhost:8086/ and find the id of your organization:
3636
- Username: `my-user`, Password: `my-password`
37-
- https://docs.influxdata.com/influxdb/v2.0/organizations/view-orgs/
37+
- https://docs.influxdata.com/influxdb/latest/organizations/view-orgs/
3838
1. Start SwiftCLI by:
3939
```bash
4040
docker run --rm \

Examples/ParameterizedQuery/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# ParameterizedQuery
22

33
This is an example how to query with query parameters.
4-
The Telegraf sends data from [CPU Input Plugin](https://github.com/influxdata/telegraf/blob/master/plugins/inputs/cpu/README.md) into InfluxDB 2.0.
4+
The Telegraf sends data from [CPU Input Plugin](https://github.com/influxdata/telegraf/blob/master/plugins/inputs/cpu/README.md) into InfluxDB 2.x.
55

66
## Prerequisites:
77
- Docker

Examples/QueryCpu/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# QueryCpu
22

33
This is an example how to query data into sequence of `FluxRecord`.
4-
The Telegraf sends data from [CPU Input Plugin](https://github.com/influxdata/telegraf/blob/master/plugins/inputs/cpu/README.md) into InfluxDB 2.0.
4+
The Telegraf sends data from [CPU Input Plugin](https://github.com/influxdata/telegraf/blob/master/plugins/inputs/cpu/README.md) into InfluxDB 2.x.
55

66
## Prerequisites:
77
- Docker

Examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313

1414
## Others
1515
- [DeleteData](DeleteData#deletedata) - How to delete data with specified predicate
16+
- [AsyncAwait](AsyncAwait#asyncawait) - How to use `async/await` with the InfluxDB client.
1617
- [InfluxDBStatus](InfluxDBStatus#influxdbstatus) - This is an example how to check status of InfluxDB
1718

0 commit comments

Comments
 (0)