Skip to content

Commit

Permalink
Add fetch app
Browse files Browse the repository at this point in the history
  • Loading branch information
aromanov91 committed Nov 15, 2024
1 parent 8723ecc commit 1237cb0
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
55 changes: 55 additions & 0 deletions Sources/OversizeAppStoreServices/Models/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,61 @@ public struct App: Identifiable, Sendable {
self.included = nil
}
}

public init?(schema: AppStoreAPI.App, included: [AppStoreAPI.AppResponse.IncludedItem]? = nil) {
guard let attributes = schema.attributes,
let bundleID = attributes.bundleID,
let name = attributes.name,
let sku = attributes.sku,
let primaryLocaleRawValue = attributes.primaryLocale,
let primaryLocale = AppStoreLanguage(rawValue: primaryLocaleRawValue)
else { return nil }

id = schema.id
self.name = name
self.bundleID = bundleID
self.sku = sku
self.primaryLocale = primaryLocale
contentRightsDeclaration = attributes.contentRightsDeclaration
.flatMap { ContentRightsDeclaration(rawValue: $0.rawValue) }
isOrEverWasMadeForKids = attributes.isOrEverWasMadeForKids
subscriptionStatusURL = attributes.subscriptionStatusURL
subscriptionStatusURLForSandbox = attributes.subscriptionStatusURLForSandbox
subscriptionStatusURLVersion = attributes.subscriptionStatusURLVersion
.flatMap { SubscriptionStatusURLVersion(rawValue: $0.rawValue) } ?? .none
subscriptionStatusURLVersionForSandbox = attributes.subscriptionStatusURLVersionForSandbox
.flatMap { SubscriptionStatusURLVersion(rawValue: $0.rawValue) } ?? .none

if let includedItems = included {
var appStoreVersions: [AppStoreAPI.AppStoreVersion] = []
var builds: [AppStoreAPI.Build] = []
var prereleaseVersions: [AppStoreAPI.PrereleaseVersion] = []

for includedItem in includedItems {
switch includedItem {
case let .appStoreVersion(includedAppStoreVersion):
appStoreVersions.append(includedAppStoreVersion)
case let .build(includedBuild):
builds.append(includedBuild)
default:
continue
}
}

self.included = Included(
appStoreVersions: appStoreVersions.compactMap { appStoreVersion in
.init(
schema: appStoreVersion,
builds: []
)
},
builds: builds.compactMap { .init(schema: $0) }.sorted(by: { $0.uploadedDate > $1.uploadedDate }),
prereleaseVersions: prereleaseVersions.compactMap { .init(schema: $0, builds: []) }
)
} else {
self.included = nil
}
}
}

public extension App {
Expand Down
22 changes: 22 additions & 0 deletions Sources/OversizeAppStoreServices/Services/AppsService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,28 @@ public actor AppsService {
}
}

public func fetchAppIncludeAppStoreVersionsAndBuildsAndPreReleaseVersions(appId: String) async -> Result<App, AppError> {
do {
guard let client = client else {
return .failure(.network(type: .unauthorized))
}
let request = Resources.v1.apps.id(appId).get(
include: [
.builds,
.appStoreVersions,
.preReleaseVersions,
]
)
let result = try await client.send(request)
guard let app: App = .init(schema: result.data, included: result.included) else {
return .failure(.network(type: .decode))
}
return .success(app)
} catch {
return .failure(.network(type: .noResponse))
}
}

public func fetchAppsIncludeActualAppStoreVersionsAndBuilds() async -> Result<[App], AppError> {
guard let client = client else { return .failure(.network(type: .unauthorized)) }
let request = Resources.v1.apps.get(
Expand Down

0 comments on commit 1237cb0

Please sign in to comment.