Skip to content

Push notifications added #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
}