Skip to content

Commit

Permalink
feat: idiomatic improvements (#25)
Browse files Browse the repository at this point in the history
* chore(ci): updated `SwiftLint` to 0.42.0
  • Loading branch information
bednar authored Mar 2, 2021
1 parent eabb299 commit 0ec0428
Show file tree
Hide file tree
Showing 41 changed files with 1,056 additions and 637 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ commands:
- run: |
apt-get update
apt-get install wget unzip --yes
wget https://github.com/realm/SwiftLint/releases/download/0.40.3/swiftlint_linux.zip
wget https://github.com/realm/SwiftLint/releases/download/0.42.0/swiftlint_linux.zip
unzip -n swiftlint_linux.zip
ln -s /root/project/swiftlint /usr/bin/swiftlint
check-example:
Expand Down
5 changes: 2 additions & 3 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ opt_in_rules:
- conditional_returns_on_newline
- empty_count
- empty_string
- explicit_acl
- explicit_init
- explicit_self
- fatal_error_message
Expand Down Expand Up @@ -45,10 +44,10 @@ excluded:
- Tests/InfluxDBSwiftApisTests/XCTestManifests.swift

type_body_length:
warning: 250
warning: 500

file_length:
warning: 500
warning: 1000

cyclomatic_complexity:
warning: 15
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
## 0.2.0 [unreleased]

### API
1. [#25](https://github.com/influxdata/influxdb-client-swift/pull/25): Updated client API to be more Swift like
1. [#23](https://github.com/influxdata/influxdb-client-swift/pull/23): Updated swagger to latest version

### CI
1. [#25](https://github.com/influxdata/influxdb-client-swift/pull/25): Updated `SwiftLint` to 0.42.0

## 0.1.0 [2021-01-29]

### InfluxDBSwift
Expand Down
4 changes: 2 additions & 2 deletions Examples/CreateNewBucket/Sources/CreateNewBucket/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct CreateNewBucket: ParsableCommand {
retentionRules: [RetentionRule(type: RetentionRule.ModelType.expire, everySeconds: self.retention)])

// Create Bucket
api.getBucketsAPI().postBuckets(postBucketRequest: request) { bucket, error in
api.bucketsAPI.postBuckets(postBucketRequest: request) { bucket, error in
// For error exit
if let error = error {
self.atExit(client: client, error: error)
Expand All @@ -58,7 +58,7 @@ struct CreateNewBucket: ParsableCommand {
])

// Create Authorization
api.getAuthorizationsAPI().postAuthorizations(authorization: request) { authorization, error in
api.authorizationsAPI.postAuthorizations(authorization: request) { authorization, error in
// For error exit
if let error = error {
atExit(client: client, error: error)
Expand Down
4 changes: 2 additions & 2 deletions Examples/DeleteData/Sources/DeleteData/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct DeleteData: ParsableCommand {
stop: Date(),
predicate: predicate)

client.getDeleteAPI().delete(predicate: predicateRequest, bucket: bucket, org: org) { result, error in
client.deleteAPI.delete(predicate: predicateRequest, bucket: bucket, org: org) { result, error in
// For handle error
if let error = error {
self.atExit(client: client, error: error)
Expand All @@ -63,7 +63,7 @@ struct DeleteData: ParsableCommand {
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
"""

client.getQueryAPI().query(query: query) { response, error in
client.queryAPI.query(query: query) { response, error in
// For handle error
if let error = error {
self.atExit(client: client, error: error)
Expand Down
2 changes: 1 addition & 1 deletion Examples/QueryCpu/Sources/QueryCpu/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct QueryCpu: ParsableCommand {

print("\nQuery to execute:\n\n\(query)")

client.getQueryAPI().query(query: query) { response, error in
client.queryAPI.query(query: query) { response, error in
// For handle error
if let error = error {
self.atExit(client: client, error: error)
Expand Down
2 changes: 1 addition & 1 deletion Examples/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Examples

## Writes
- [WriteData](WriteData#writedata) - How to write data with different type of records
- [WriteData](WriteData#writedata) - How to write data with Data Point structure

## Queries
- [QueryCpu](QueryCpu#querycpu) - How to query data into sequence of `FluxRecord`
Expand Down
4 changes: 1 addition & 3 deletions Examples/WriteData/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# WriteData

This is an example how to write data with different type of records.
This is an example how to write data with Data Point structure.

## Prerequisites:
- Docker
Expand Down Expand Up @@ -52,10 +52,8 @@ This is an example how to write data with different type of records.
```bash
Written data:
- demo,type=string value=1i
- Point: measurement:demo, tags:["type": Optional("point")], fields:["value": Optional(2)], time:nil
- Point: measurement:demo, tags:["type": Optional("point-timestamp")], fields:["value": Optional(2)], time:2020-12-10 11:16:29 +0000
- (measurement: "demo", tags: ["type": "tuple"], fields: ["value": 3])
Success!
```
20 changes: 5 additions & 15 deletions Examples/WriteData/Sources/WriteData/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,31 @@ struct WriteData: ParsableCommand {
token: token,
options: InfluxDBClient.InfluxDBOptions(bucket: self.bucket, org: self.org))

//
// Record defined as String
//
let recordString = "demo,type=string value=1i"
//
// Record defined as Data Point
//
let recordPoint = InfluxDBClient
.Point("demo")
.addTag(key: "type", value: "point")
.addField(key: "value", value: 2)
.addField(key: "value", value: .int(2))
//
// Record defined as Data Point with Timestamp
//
let recordPointDate = InfluxDBClient
.Point("demo")
.addTag(key: "type", value: "point-timestamp")
.addField(key: "value", value: 2)
.time(time: Date())
//
// Record defined as Tuple
//
let recordTuple = (measurement: "demo", tags: ["type": "tuple"], fields: ["value": 3])

let records: [Any] = [recordString, recordPoint, recordPointDate, recordTuple]
.addField(key: "value", value: .int(2))
.time(time: .date(Date()))

client.getWriteAPI().writeRecords(records: records) { result, error in
client.makeWriteAPI().write(points: [recordPoint, recordPointDate]) { result, error in
// For handle error
if let error = error {
self.atExit(client: client, error: error)
}

// For Success write
if result != nil {
print("Written data:\n\n\(records.map { "\t- \($0)" }.joined(separator: "\n"))")
print("Written data:\n\n\([recordPoint, recordPointDate].map { "\t- \($0)" }.joined(separator: "\n"))")
print("\nSuccess!")
}

Expand Down
50 changes: 23 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ client.close()
|---|---|---|---|
| bucket | Default destination bucket for writes | String | none |
| org | Default organization bucket for writes | String | none |
| precision | Default precision for the unix timestamps within the body line-protocol | WritePrecision | ns |
| precision | Default precision for the unix timestamps within the body line-protocol | TimestampPrecision | ns |
| timeoutIntervalForRequest | The timeout interval to use when waiting for additional data. | TimeInterval | 60 sec |
| timeoutIntervalForResource | The maximum amount of time that a resource request should be allowed to take. | TimeInterval | 5 min |
| enableGzip | Enable Gzip compression for HTTP requests. | Bool | false |
Expand All @@ -113,7 +113,7 @@ client.close()
let options: InfluxDBClient.InfluxDBOptions = InfluxDBClient.InfluxDBOptions(
bucket: "my-bucket",
org: "my-org",
precision: InfluxDBClient.WritePrecision.ns)
precision: .ns)

let client = InfluxDBClient(url: "http://localhost:8086", token: "my-token", options: options)

Expand Down Expand Up @@ -149,7 +149,7 @@ The data could be written as:
1. Tuple style mapping with keys: `measurement`, `tags`, `fields` and `time`
1. Array of above items

The following example demonstrates how to write data with different type of records. For further information see docs and [examples](/Examples).
The following example demonstrates how to write data with Data Point structure. For further information see docs and [examples](/Examples).

```swift
import ArgumentParser
Expand All @@ -176,41 +176,32 @@ struct WriteData: ParsableCommand {
token: token,
options: InfluxDBClient.InfluxDBOptions(bucket: self.bucket, org: self.org))

//
// Record defined as String
//
let recordString = "demo,type=string value=1i"
//
// Record defined as Data Point
//
let recordPoint = InfluxDBClient
.Point("demo")
.addTag(key: "type", value: "point")
.addField(key: "value", value: 2)
.addField(key: "value", value: .int(2))
//
// Record defined as Data Point with Timestamp
//
let recordPointDate = InfluxDBClient
.Point("demo")
.addTag(key: "type", value: "point-timestamp")
.addField(key: "value", value: 2)
.time(time: Date())
//
// Record defined as Tuple
//
let recordTuple = (measurement: "demo", tags: ["type": "tuple"], fields: ["value": 3])

let records: [Any] = [recordString, recordPoint, recordPointDate, recordTuple]
.addField(key: "value", value: .int(2))
.time(time: .date(Date()))

client.getWriteAPI().writeRecords(records: records) { result, error in
client.makeWriteAPI().write(points: [recordPoint, recordPointDate]) { result, error in
// For handle error
if let error = error {
self.atExit(client: client, error: error)
}

// For Success write
if result != nil {
print("Successfully written data:\n\n\(records)")
print("Written data:\n\n\([recordPoint, recordPointDate].map { "\t- \($0)" }.joined(separator: "\n"))")
print("\nSuccess!")
}

self.atExit(client: client)
Expand Down Expand Up @@ -280,7 +271,7 @@ struct QueryCpu: ParsableCommand {

print("\nQuery to execute:\n\n\(query)")

client.getQueryAPI().query(query: query) { response, error in
client.queryAPI.query(query: query) { response, error in
// For handle error
if let error = error {
self.atExit(client: client, error: error)
Expand Down Expand Up @@ -358,7 +349,7 @@ struct QueryCpuData: ParsableCommand {
|> last()
"""

client.getQueryAPI().queryRaw(query: query) { response, error in
client.queryAPI.queryRaw(query: query) { response, error in
// For handle error
if let error = error {
self.atExit(client: client, error: error)
Expand Down Expand Up @@ -430,7 +421,7 @@ struct DeleteData: ParsableCommand {
stop: Date(),
predicate: predicate)

client.getDeleteAPI().delete(predicate: predicateRequest, bucket: bucket, org: org) { result, error in
client.deleteAPI.delete(predicate: predicateRequest, bucket: bucket, org: org) { result, error in
// For handle error
if let error = error {
self.atExit(client: client, error: error)
Expand All @@ -457,7 +448,7 @@ struct DeleteData: ParsableCommand {
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
"""

client.getQueryAPI().query(query: query) { response, error in
client.queryAPI.query(query: query) { response, error in
// For handle error
if let error = error {
self.atExit(client: client, error: error)
Expand Down Expand Up @@ -553,7 +544,7 @@ struct CreateNewBucket: ParsableCommand {
retentionRules: [RetentionRule(type: RetentionRule.ModelType.expire, everySeconds: self.retention)])

// Create Bucket
api.getBucketsAPI().postBuckets(postBucketRequest: request) { bucket, error in
api.bucketsAPI.postBuckets(postBucketRequest: request) { bucket, error in
// For error exit
if let error = error {
self.atExit(client: client, error: error)
Expand All @@ -576,7 +567,7 @@ struct CreateNewBucket: ParsableCommand {
])

// Create Authorization
api.getAuthorizationsAPI().postAuthorizations(authorization: request) { authorization, error in
api.authorizationsAPI.postAuthorizations(authorization: request) { authorization, error in
// For error exit
if let error = error {
atExit(client: client, error: error)
Expand Down Expand Up @@ -628,16 +619,21 @@ client = InfluxDBClient(
token: "my-token",
options: InfluxDBClient.InfluxDBOptions(bucket: "my-bucket", org: "my-org"))

let tuple: InfluxDBClient.Point.Tuple
= (measurement: "mem", tags: ["tag": "a"], fields: ["value": .int(3)], time: nil)

let records: [Any] = [
InfluxDBClient.Point("mining").addTag(key: "sensor_state", value: "normal").addField(key: "depth", value: 2),
(measurement: "mining", tags: ["sensor_state": "normal"], fields: ["pressure": 3])
InfluxDBClient.Point("mining")
.addTag(key: "sensor_state", value: "normal")
.addField(key: "depth", value: .int(2)),
tuple
]

let defaultTags = InfluxDBClient.PointSettings()
.addDefaultTag(key: "customer", value: "California Miner")
.addDefaultTag(key: "sensor_id", value: "${env.SENSOR_ID}")

let writeAPI = client.getWriteAPI(pointSettings: defaultTags)
let writeAPI = client.makeWriteAPI(pointSettings: defaultTags)
writeAPI.writeRecords(records: records) { _, error in
if let error = error {
print("Error: \(error)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ internal class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}

override internal func execute(_ apiResponseQueue: DispatchQueue, _ completion: @escaping (_ result: Swift.Result<Response<T>, InfluxDBClient.InfluxDBError>) -> Void) {
let urlSession = self.influxDB2API.getURLSession()
let urlSession = self.influxDB2API.urlSession
let parameters: [String: Any] = self.parameters ?? [:]
Expand Down
4 changes: 2 additions & 2 deletions Sources/InfluxDBSwift/DeleteAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import FoundationNetworking
/// stop: Date(),
/// predicate: "_measurement=\"server\" AND production=\"no\"")
///
/// client.getDeleteAPI().delete(predicate: predicateRequest, bucket: "my-bucket", org: "my-org") { result, error in
/// client.deleteAPI.delete(predicate: predicateRequest, bucket: "my-bucket", org: "my-org") { result, error in
/// // For handle error
/// if let error = error {
/// print("Error:\n\n\(error)")
Expand All @@ -39,7 +39,7 @@ public class DeleteAPI {
///
/// - Parameters
/// - client: Client with shared configuration and http library.
public init(client: InfluxDBClient) {
init(client: InfluxDBClient) {
self.client = client
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/InfluxDBSwift/FluxCSVParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class FluxCSVParser {
private var startNewTable = true
private var groups: ArraySlice<String> = []

internal init(data: Data) throws {
init(data: Data) throws {
csv = try CSVReader(stream: InputStream(data: data))
}

Expand Down
Loading

0 comments on commit 0ec0428

Please sign in to comment.