Skip to content

Commit

Permalink
Add UpdateEmail and fix docs
Browse files Browse the repository at this point in the history
The last commit didn't have the CodingKeys implemented
in order to maintain the consistency of the JSON name when
renaming the property in the Swift model. This commit fixes
that.
  • Loading branch information
MasterJ93 committed Feb 27, 2024
1 parent 4595f13 commit a209b7f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ import Foundation
public struct ServerRevokeAppPassword: Codable {
/// The name associated with the App Password.
public let appPasswordName: String

enum CodingKeys: String, CodingKey {
case appPasswordName = "name"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// AtprotoServerUpdateEmail.swift
//
//
// Created by Christopher Jr Riley on 2024-02-27.
//

import Foundation

/// The main data model definition for updating the user's email address.
///
/// - Note: According to the AT Protocol specifications: "Update an account's email."
///
/// - SeeAlso: This is based on the [`com.atproto.server.updateEmail`][github] lexicon.
///
/// [github]: https://github.com/bluesky-social/atproto/blob/main/lexicons/com/atproto/server/updateEmail.json
public struct ServerUpdateEmail: Codable {
/// The email associated with the user's account.
public let email: String
/// The token that's used if the email has been confirmed. Optional.
///
/// - Note: According to the AT Protocol specifications: "Requires a token from com.atproto.sever.requestEmailUpdate if the account's email has been confirmed."
public let token: String?
}
29 changes: 29 additions & 0 deletions Sources/ATProtoKit/Networking/CoreAPI/UpdateEmail.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// UpdateEmail.swift
//
//
// Created by Christopher Jr Riley on 2024-02-27.
//

import Foundation

extension ATProtoKit {
/// Updates the email address associated with the user's account.
///
public func updateEmail(_ email: String, token: String? = nil) async throws {
guard let sessionURL = session.pdsURL,
let requestURL = URL(string: "\(sessionURL)/xrpc/om.atproto.server.updateEmail") else {
throw NSError(domain: "", code: -1, userInfo: [NSLocalizedDescriptionKey : "Invalid URL"])
}

let requestBody = ServerUpdateEmail(email: email, token: token)

do {
let request = APIClientService.createRequest(forRequest: requestURL, andMethod: .post, acceptValue: nil, contentTypeValue: "application/json", authorizationValue: "Bearer \(session.accessToken)")

try await APIClientService.sendRequest(request, withEncodingBody: requestBody)
} catch {
throw error
}
}
}

0 comments on commit a209b7f

Please sign in to comment.