Skip to content

Commit

Permalink
Fix client url, work if unauthorized
Browse files Browse the repository at this point in the history
  • Loading branch information
christophhagen committed Oct 7, 2023
1 parent a648d25 commit 0211453
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
28 changes: 22 additions & 6 deletions Sources/NextcloudStatus/Binary/NextcloudData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ extension NextcloudStatus {

public struct NextcloudData {

public let nextcloud: Nextcloud
public let nextcloud: Nextcloud?

public let server: Server
public let server: Server?

public let activeUsers: ActiveUsers
public let activeUsers: ActiveUsers?

public let message: String?
}
}

Expand All @@ -18,6 +20,7 @@ extension NextcloudStatus.NextcloudData: Codable {
case nextcloud = 1
case server = 2
case activeUsers = 3
case message = 4
}
}

Expand All @@ -28,8 +31,21 @@ extension NextcloudStatus.NextcloudData: Equatable {
extension NextcloudStatus.NextcloudData {

init(json: NextcloudStatusJson.OCS.NextcloudData) throws {
self.nextcloud = try .init(json: json.nextcloud)
self.server = try .init(json: json.server)
self.activeUsers = .init(json: json.activeUsers)
if let nextcloud = json.nextcloud {
self.nextcloud = try .init(json: nextcloud)
} else {
self.nextcloud = nil
}
if let server = json.server {
self.server = try .init(json: server)
} else {
self.server = nil
}
if let activeUsers = json.activeUsers {
self.activeUsers = .init(json: activeUsers)
} else {
self.activeUsers = nil
}
self.message = json.message
}
}
8 changes: 5 additions & 3 deletions Sources/NextcloudStatus/JSON/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ extension NextcloudStatusJson.OCS {

public struct NextcloudData {

public let nextcloud: Nextcloud
public let nextcloud: Nextcloud?

public let server: Server
public let server: Server?

public let activeUsers: ActiveUsers
public let activeUsers: ActiveUsers?

public let message: String?
}
}

Expand Down
29 changes: 24 additions & 5 deletions Sources/NextcloudStatus/NextcloudStatusClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ import FoundationNetworking
occ config:app:set serverinfo token --value <token>
```
*/
public let nextcloudStatusUrlSuffix = "/ocs/v2.php/apps/serverinfo/api/v1/info?format=json"
public let nextcloudStatusUrlSuffix = "/ocs/v2.php/apps/serverinfo/api/v1/info"

/**
The query parameter to request JSON output instead of XML.

Used together with ``nextcloudStatusUrlSuffix`` to build the full url to the nextcloud JSON status.
*/
public let nextcloudStatusUrlJsonFormatQueryParameter = "?format=json"

/**
The HTTP request header key to transmit the access token during a Nextcloud status request.
Expand All @@ -25,13 +32,22 @@ public let nextcloudStatusAccessTokenHeaderKey = "NC-Token"

public struct NextcloudStatusClient {

public let url: URL
/**
The authentication token of the nextcloud instance

The access token must be set for the nextcloud instance.
With nextcloud-snap, this can be achieved with the following command:
```
nextcloud.occ config:app:set serverinfo token --value <token>
*/
public let token: String

/**
The URL session to use for the requests.
*/
public let session: URLSession

let request: URLRequest
private let request: URLRequest

/**
Create a new client.
Expand All @@ -50,11 +66,14 @@ public struct NextcloudStatusClient {
```
*/
public init(url: URL, token: String, session: URLSession = .shared) {
self.url = url
self.token = token
self.session = session
var request = URLRequest(url: url.appendingPathComponent(nextcloudStatusUrlSuffix))

let urlString = url.appendingPathComponent(nextcloudStatusUrlSuffix).absoluteString + nextcloudStatusUrlJsonFormatQueryParameter
let url = URL(string: urlString)!
var request = URLRequest(url: url)
request.setValue(token, forHTTPHeaderField: nextcloudStatusAccessTokenHeaderKey)

self.request = request
}

Expand Down
10 changes: 10 additions & 0 deletions Tests/NextcloudStatusTests/NextcloudStatusTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ final class NextcloudStatusTests: XCTestCase {
try encodeAndCompare(input: nextcloudTestData1)
try encodeAndCompare(input: nextcloudTestData2)
}

func testUnauthorized() throws {
let info = try decode(input: unauthorizedData)
XCTAssertEqual(info.ocs.data.message, "Unauthorized")
}
}


Expand Down Expand Up @@ -271,3 +276,8 @@ private let nextcloudTestData2 =
"""

let unauthorizedData =
"""
{"ocs":{"meta":{"status":"failure","statuscode":401,"message":""},"data":{"message":"Unauthorized"}}}
"""

0 comments on commit 0211453

Please sign in to comment.