Skip to content

Commit

Permalink
Push notifications added (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianKahnert authored Mar 5, 2025
1 parent e05e4de commit 831639b
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Sources/TibberSwift/Entities/PushNotificationResult.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// The PushNotificationResult structure
public struct PushNotificationResult: Codable {
public struct Response: Codable {
public let successful: Bool
public let pushedToNumberOfDevices: Int
}
public let sendPushNotification: Response
}
6 changes: 5 additions & 1 deletion Sources/TibberSwift/GraphQL/GraphQLResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ struct GraphQLResult<T: Decodable>: Decodable {
let container = try decoder.container(keyedBy: CodingKeys.self)

let dataObj = try container.decodeIfPresent(ViewerResult<T>.self, forKey: .data)
self.object = dataObj?.viewer
if let view = dataObj?.viewer {
self.object = view
} else {
self.object = try container.decodeIfPresent(T.self, forKey: .data)
}

var errorMessages: [String] = []
if let errors = try container.decodeIfPresent([Error].self, forKey: .errors) {
Expand Down
27 changes: 27 additions & 0 deletions Sources/TibberSwift/Operations/PushNotificationOperation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Foundation

public extension GraphQLOperation where Input == EmptyInput, Output == PushNotificationResult {

/// Sends a push notification to all logged-in devices of the user
/// - Parameters:
/// - title: Title of the notification
/// - message: Message of the notification
/// - Returns: ``GraphQLOperation`` for the notification
static func pushNotification(
title: String,
message: String
) throws -> Self {
GraphQLOperation(input: EmptyInput(),
operationString: """
mutation {
sendPushNotification(input: {
title: \"\(title)\",
message: \"\(message)\"
}){
successful
pushedToNumberOfDevices
}
}
""")
}
}
10 changes: 10 additions & 0 deletions Sources/TibberSwift/TibberSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ public extension TibberSwift {
let operation = try GraphQLOperation.consumption(resolution: .hourly, last: 100)
return try await performOperation(operation)
}

/// Sends a push notification to all logged-in devices of the user
/// - Parameters:
/// - title: Title of the notification
/// - message: Message of the notification
/// - Returns: ``PushNotificationResult`` for the notification
func sendPushNotification(title: String, message: String) async throws -> PushNotificationResult {
let operation = try GraphQLOperation.pushNotification(title: title, message: message)
return try await performOperation(operation)
}

/// Custom GraphQL operation
/// - Parameter operation: The operation in question
Expand Down
17 changes: 17 additions & 0 deletions Tests/TibberSwiftTests/TibberSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,21 @@ final class TibberSwiftTests: XCTestCase {
"Authorization": "consumptionKey",
"User-Agent": "TibberSwift"])
}

// MARK: - Test Push Notification

func testPushNotification() async throws {
// Given
let data = TestDataManager.getData(forFile: "json/PushNotificationResult.json")!
urlSessionMock.dataForReturnValue = (data, TestDataManager.dummyResponse())

let sut = TibberSwift(apiKey: "pushNotificationKey", urlSession: urlSessionMock)

// When
let result = try await sut.sendPushNotification(title: "test-title", message: "test-message")

// Then
XCTAssertTrue(result.sendPushNotification.successful)
XCTAssertEqual(result.sendPushNotification.pushedToNumberOfDevices, 1)
}
}
8 changes: 8 additions & 0 deletions Tests/TibberSwiftTests/json/PushNotificationResult.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"data": {
"sendPushNotification": {
"successful": true,
"pushedToNumberOfDevices": 1
}
}
}

0 comments on commit 831639b

Please sign in to comment.