Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
petrpavlik committed Aug 21, 2024
1 parent 05f0d16 commit 25dff3a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
42 changes: 21 additions & 21 deletions Sources/IndiePitcherSwift/IndiePitcherSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public struct IndiePitcher {
}

private func buildUri(path: String) -> URI {
URI(stringLiteral: "https://api.indiepitcher.com/v2" + path)
URI(stringLiteral: "https://api.indiepitcher.com/v1" + path)
}

/// Add a new contact to the contact list, or update an existing one if `updateIfExists` is set to `true`.
/// Add a new contact to the mailing list, or update an existing one if `updateIfExists` is set to `true`.
/// - Parameter contact: Contact properties.
/// - Returns: Created contact.
@discardableResult public func addContact(contact: CreateContact) async throws -> DataResponse<Contact> {
Expand Down Expand Up @@ -59,8 +59,8 @@ public struct IndiePitcher {
return try response.content.decode(DataResponse<Contact>.self)
}

/// Deletes a contact with provided email from the contact list
/// - Parameter email: The email address of the contact you wish to remove from the contact list
/// Deletes a contact with provided email from the mailing list
/// - Parameter email: The email address of the contact you wish to remove from the mailing list
/// - Returns: A generic empty response.
@discardableResult public func deleteContact(email: String) async throws -> EmptyResposne {

Expand All @@ -75,7 +75,7 @@ public struct IndiePitcher {
return try response.content.decode(EmptyResposne.self)
}

/// Returns a paginated list of stored contacts in the contact list.
/// Returns a paginated list of stored contacts in the mailing list.
/// - Parameters:
/// - page: Page to fetch, the first page has index 1.
/// - perPage: How many contacts to return per page.
Expand All @@ -100,8 +100,8 @@ public struct IndiePitcher {
return try response.content.decode(EmptyResposne.self)
}

/// Send a personalized email to one more (up to 100 using 1 API call) contacts subscribed to a proviced contact list. This is the recommended way to send an email to members of a team of your product.
/// All provided emails must belong to your contact list and must be members of provided contact list. All contacts are automatically subscribed to `important` default contact list. You can use peronalization tags such as `Hi {{firstName|default:"there"}}` to peronalize individual sent emails, and scheduled it to be sent with a delay.
/// Send a personalized email to one more (up to 100 using 1 API call) contacts subscribed to a proviced mailing list. This is the recommended way to send an email to members of a team of your product.
/// All provided emails must belong to your mailing list and must be members of provided mailing list. All contacts are automatically subscribed to `important` default mailing list. You can use peronalization tags such as `Hi {{firstName|default:"there"}}` to peronalize individual sent emails, and scheduled it to be sent with a delay.
/// - Parameter data: Input params.
/// - Returns: A genereic response with no return data.
@discardableResult public func sendEmailToContact(data: SendEmailToContact) async throws -> EmptyResposne {
Expand All @@ -112,53 +112,53 @@ public struct IndiePitcher {
return try response.content.decode(EmptyResposne.self)
}

/// Send a personalized email to all contacts subscribed to a provided contact list. This is the recommendat way to send a newsletter, by creating a list called something like `Newsletter`.
/// All contacts are automatically subscribed to `important` default contact list. You can use peronalization tags such as `Hi {{firstName|default:"there"}}` to peronalize individual sent emails, and scheduled it to be sent with a delay.
/// Send a personalized email to all contacts subscribed to a provided mailing list. This is the recommendat way to send a newsletter, by creating a list called something like `Newsletter`.
/// All contacts are automatically subscribed to `important` default mailing list. You can use peronalization tags such as `Hi {{firstName|default:"there"}}` to peronalize individual sent emails, and scheduled it to be sent with a delay.
/// - Parameter data: Input params.
/// - Returns: A genereic response with no return data.
@discardableResult public func sendEmailToContactList(data: SendEmailToContactList) async throws -> EmptyResposne {
let response = try await client.post(buildUri(path: "/email/contact_list"),
@discardableResult public func sendEmailToMailingList(data: SendEmailToMailingList) async throws -> EmptyResposne {
let response = try await client.post(buildUri(path: "/email/list"),
headers: commonHeaders,
content: data)

return try response.content.decode(EmptyResposne.self)
}

/// Returns contact lists contacts can subscribe to.
/// Returns mailing lists contacts can subscribe to.
/// - Parameters:
/// - page: Page to fetch, the first page has index 1.
/// - perPage: How many contacts to return per page.
/// - Returns: A paginated array of contact lists
public func listContactLists(page: Int = 1, perPage: Int = 10) async throws -> PagedDataResponse<ContactList> {
/// - Returns: A paginated array of mailing lists
public func listMailingLists(page: Int = 1, perPage: Int = 10) async throws -> PagedDataResponse<MailingList> {

struct Payload: Content {
let page: Int
let per: Int
}

let response = try await client.get(buildUri(path: "/contact_lists?page=\(page)&per=\(perPage)"),
let response = try await client.get(buildUri(path: "/lists?page=\(page)&per=\(perPage)"),
headers: commonHeaders)

return try response.content.decode(PagedDataResponse<ContactList>.self)
return try response.content.decode(PagedDataResponse<MailingList>.self)
}


/// Generates a new public URL for a contact with provided email to manage their contact list subscriptions.
/// Generates a new public URL for a contact with provided email to manage their mailing list subscriptions.
/// - Parameters:
/// - contactEmail: The email of a contact this session is for.
/// - returnURL: The URL to redirect to when the user is done editing their contact list, or when the session has expired.
/// - returnURL: The URL to redirect to when the user is done editing their mailing list, or when the session has expired.
/// - Returns: Newly created URL session.
public func createContactListsPortalSession(contactEmail: String, returnURL: URL) async throws -> DataResponse<ContactListPortalSession> {
public func createMailingListsPortalSession(contactEmail: String, returnURL: URL) async throws -> DataResponse<MailingListPortalSession> {

struct Payload: Content {
let contactEmail: String
let returnURL: URL
}

let response = try await client.post(buildUri(path: "/contact_lists/portal_session"),
let response = try await client.post(buildUri(path: "/lists/portal_session"),
headers: commonHeaders,
content: Payload(contactEmail: contactEmail, returnURL: returnURL))

return try response.content.decode(DataResponse<ContactListPortalSession>.self)
return try response.content.decode(DataResponse<MailingListPortalSession>.self)
}
}
16 changes: 8 additions & 8 deletions Sources/IndiePitcherSwift/dtos.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public enum CustomContactPropertyValue: Codable, Equatable {
}
}

/// A contact in the contact list
/// A contact in the mailing list
public struct Contact: Content {
public init(email: String, userId: String? = nil, avatarUrl: String? = nil, name: String? = nil, hardBouncedAt: Date? = nil, subscribedToLists: [String], customProperties: [String : CustomContactPropertyValue], languageCode: String? = nil) {
self.email = email
Expand Down Expand Up @@ -164,7 +164,7 @@ public struct CreateMultipleContacts: Content {
public var contacts: [CreateContact]
}

/// The payload to update a contact in the contact list. The email is required to identify the contact.
/// The payload to update a contact in the mailing list. The email is required to identify the contact.
public struct UpdateContact: Content {

/// Initializer
Expand Down Expand Up @@ -284,7 +284,7 @@ public struct SendEmailToContact: Content {
public var delayUntilDate: Date?
}

public struct SendEmailToContactList: Content {
public struct SendEmailToMailingList: Content {

/// Initializer
/// - Parameters:
Expand Down Expand Up @@ -322,19 +322,19 @@ public struct SendEmailToContactList: Content {
public var delayUntilDate: Date?
}

/// Represents a contact list contacts can subscribe to, such as `Monthly newsletter` or `Onboarding`.
public struct ContactList: Content {
/// Represents a mailing list contacts can subscribe to, such as `Monthly newsletter` or `Onboarding`.
public struct MailingList: Content {

public init(name: String, title: String, numSubscribers: Int) {
self.name = name
self.title = title
self.numSubscribers = numSubscribers
}

/// The unique name of the contact list meant to be used by the public API. Not intended to be be shown to the end users, that's what `title` is for.
/// The unique name of the mailing list meant to be used by the public API. Not intended to be be shown to the end users, that's what `title` is for.
public var name: String

/// A human readable name of the contact list.
/// A human readable name of the mailing list.
public var title: String

/// The number of contacts subscribed to this list.
Expand All @@ -343,7 +343,7 @@ public struct ContactList: Content {


/// A portal session that allows a contact to manage their email list subscriptions when redirected to returned `url`. A session is valid for 30 minutes.
public struct ContactListPortalSession: Content {
public struct MailingListPortalSession: Content {
public init(url: URL, expiresAt: Date, returnURL: URL) {
self.url = url
self.expiresAt = expiresAt
Expand Down
6 changes: 3 additions & 3 deletions Tests/IndiePitcherSwiftTests/IndiePitcherSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ final class IndiePitcherSwiftTests: XCTestCase {
bodyFormat: .markdown))
}

func testGetContactLists() async throws {
let listsResponse = try await indiePitcher.listContactLists()
func testGetMailingLists() async throws {
let listsResponse = try await indiePitcher.listMailingLists()
expect(listsResponse.metadata.total) == 2
expect(listsResponse.data.count) == 2
}

func testSendMarketingEmailToList() async throws {
try await indiePitcher.sendEmailToContactList(data: .init(subject: "Test marketing email from IP Swift SDK unit tests",
try await indiePitcher.sendEmailToMailingList(data: .init(subject: "Test marketing email from IP Swift SDK unit tests",
body: "This is a test body of a marketing email that supports **markdown**.",
bodyFormat: .markdown,
list: "test_list_1"))
Expand Down

0 comments on commit 25dff3a

Please sign in to comment.