From fef55de5a656ddc2ce2e84477f7b7c5e21aa7b2c Mon Sep 17 00:00:00 2001 From: Alexander Romanov Date: Mon, 25 Sep 2023 00:03:23 +0300 Subject: [PATCH] Add Swift Open Api generated methods --- .gitignore | 1 + Package.resolved | 48 +-- Package.swift | 53 ++- Sources/OversizeNetwork/NetworkService.swift | 38 --- .../Repositories/NetworkService.swift | 91 +++++ .../ServiceRegistering.swift | 2 - .../openapi-generator-config.yaml | 0 .../OversizeNetwork/Repositories/openapi.yaml | 313 ++++++++++++++++++ Sources/main.swift | 23 -- Sources/openapi.yaml | 50 --- 10 files changed, 455 insertions(+), 164 deletions(-) delete mode 100644 Sources/OversizeNetwork/NetworkService.swift create mode 100644 Sources/OversizeNetwork/Repositories/NetworkService.swift rename Sources/OversizeNetwork/{ => Repositories}/ServiceRegistering.swift (90%) rename Sources/{ => OversizeNetwork/Repositories}/openapi-generator-config.yaml (100%) create mode 100644 Sources/OversizeNetwork/Repositories/openapi.yaml delete mode 100644 Sources/main.swift delete mode 100644 Sources/openapi.yaml diff --git a/.gitignore b/.gitignore index 3b29812..3846059 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ DerivedData/ .swiftpm/config/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc +/Package.resolved diff --git a/Package.resolved b/Package.resolved index d88a90e..e93ed5a 100644 --- a/Package.resolved +++ b/Package.resolved @@ -5,8 +5,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/hmlongco/Factory.git", "state" : { - "revision" : "bf4e0ab7cfc45a921856d9ca196d834b7abf15a0", - "version" : "2.1.5" + "revision" : "061b3afe0358a0da7ce568f8272c847910be3dd7", + "version" : "2.2.0" } }, { @@ -14,8 +14,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/mattpolzin/OpenAPIKit.git", "state" : { - "revision" : "84f6702500fb00476d74420c1a0df64cddd3993e", - "version" : "3.0.0-alpha.7" + "revision" : "beca680cef9ea622d39b42187baa9ce77b89865a", + "version" : "3.0.0-beta.5" } }, { @@ -23,8 +23,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/oversizedev/OversizeCore.git", "state" : { - "revision" : "a248ebced6edae54b75d0e19d6f9d121be9e0810", - "version" : "1.4.0" + "revision" : "988deea848fb80612858b8a0036b51e919064abf", + "version" : "1.8.1" } }, { @@ -41,8 +41,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/oversizedev/OversizeServices.git", "state" : { - "revision" : "247aac3a109da879b1b57ab5661ca08998ba0e27", - "version" : "1.6.0" + "revision" : "2553d37f66d1e619a36410c3d63afc7ad3b99976", + "version" : "1.8.2" } }, { @@ -50,8 +50,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/oversizedev/OversizeUI.git", "state" : { - "revision" : "c7a305332c81c3be2ce34d7d928f79d43b50be74", - "version" : "3.0.6" + "revision" : "89a07072b2def7ba9948c532f0259f1dfb60f97e", + "version" : "3.2.0" } }, { @@ -68,8 +68,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-argument-parser.git", "state" : { - "revision" : "fee6933f37fde9a5e12a1e4aeaa93fe60116ff2a", - "version" : "1.2.2" + "revision" : "8f4d2753f0e4778c76d5f05ad16c74f707390531", + "version" : "1.2.3" } }, { @@ -95,8 +95,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-openapi-generator", "state" : { - "revision" : "1c07b5d805dec7f0e12d402d3da3413d59309bb6", - "version" : "0.1.3" + "revision" : "7da0f96bba4f40bec50273723e586c77f6323d6b", + "version" : "0.2.2" } }, { @@ -104,8 +104,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-openapi-runtime", "state" : { - "revision" : "e81f70f2a8b445224dc6b97d0c31ed0d190e04de", - "version" : "0.1.4" + "revision" : "5f7e7ee6862fcda793d16d8b5bf8d204503f161f", + "version" : "0.2.3" } }, { @@ -113,8 +113,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-openapi-urlsession", "state" : { - "revision" : "12cece2055fa3dbd20729eaf561a12af7b6fca7a", - "version" : "0.1.1" + "revision" : "8d34af5db44be4825749259dbb38e5e11a4c48da", + "version" : "0.2.2" } }, { @@ -131,8 +131,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-system.git", "state" : { - "revision" : "836bc4557b74fe6d2660218d56e3ce96aff76574", - "version" : "1.1.1" + "revision" : "025bcb1165deab2e20d4eaba79967ce73013f496", + "version" : "1.2.1" } }, { @@ -140,8 +140,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-tools-support-core.git", "state" : { - "revision" : "93784c59434dbca8e8a9e4b700d0d6d94551da6a", - "version" : "0.5.2" + "revision" : "3b13e439a341bbbfe0f710c7d1be37221745ef1a", + "version" : "0.6.1" } }, { @@ -149,8 +149,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/jpsim/Yams.git", "state" : { - "revision" : "9ff1cc9327586db4e0c8f46f064b6a82ec1566fa", - "version" : "4.0.6" + "revision" : "0d9ee7ea8c4ebd4a489ad7a73d5c6cad55d6fed3", + "version" : "5.0.6" } } ], diff --git a/Package.swift b/Package.swift index 4d86dfa..363c82b 100644 --- a/Package.swift +++ b/Package.swift @@ -1,6 +1,17 @@ // swift-tools-version: 5.8 // The swift-tools-version declares the minimum version of Swift required to build this package. +private extension PackageDescription.Target.Dependency { + static let openAPIRuntime: Self = .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime") + static let openAPIURLSession: Self = .product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession") + static let factory: Self = .product(name: "Factory", package: "Factory") + static let oversizeServices: Self = .product(name: "OversizeServices", package: "OversizeServices") +} + +private extension PackageDescription.Target.PluginUsage { + static let openAPIGenerator: Self = .plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator") +} + import PackageDescription let package = Package( @@ -9,43 +20,31 @@ let package = Package( .macOS(.v12), .iOS(.v15), .tvOS(.v15), .watchOS(.v8), ], products: [ - .executable(name: "OversizeNetwork", targets: ["OversizeNetwork"]), + .library( + name: "OversizeNetwork", + targets: ["OversizeNetwork"] + ), ], dependencies: [ - .package(url: "https://github.com/apple/swift-openapi-generator", .upToNextMinor(from: "0.1.0")), - .package(url: "https://github.com/apple/swift-openapi-runtime", .upToNextMinor(from: "0.1.0")), - .package(url: "https://github.com/apple/swift-openapi-urlsession", .upToNextMinor(from: "0.1.0")), + .package(url: "https://github.com/apple/swift-openapi-generator", .upToNextMinor(from: "0.2.0")), + .package(url: "https://github.com/apple/swift-openapi-runtime", .upToNextMinor(from: "0.2.0")), + .package(url: "https://github.com/apple/swift-openapi-urlsession", .upToNextMinor(from: "0.2.0")), .package(url: "https://github.com/hmlongco/Factory.git", .upToNextMajor(from: "2.1.3")), .package(url: "https://github.com/oversizedev/OversizeServices.git", .upToNextMajor(from: "1.4.0")), ], targets: [ - .executableTarget( + .target( name: "OversizeNetwork", dependencies: [ - .product( - name: "OpenAPIRuntime", - package: "swift-openapi-runtime" - ), - .product( - name: "OpenAPIURLSession", - package: "swift-openapi-urlsession" - ), - .product( - name: "Factory", - package: "Factory" - ), - .product( - name: "OversizeServices", - package: "OversizeServices" - ), + .openAPIRuntime, + .openAPIURLSession, + .factory, + .oversizeServices, ], - path: "Sources", + path: "./Sources/OversizeNetwork/Repositories", plugins: [ - .plugin( - name: "OpenAPIGenerator", - package: "swift-openapi-generator" - ) + .openAPIGenerator, ] - ) + ), ] ) diff --git a/Sources/OversizeNetwork/NetworkService.swift b/Sources/OversizeNetwork/NetworkService.swift deleted file mode 100644 index 6d7d724..0000000 --- a/Sources/OversizeNetwork/NetworkService.swift +++ /dev/null @@ -1,38 +0,0 @@ -// -// Copyright © 2023 Alexander Romanov -// File.swift, created on 30.06.2023 -// - -import OpenAPIRuntime -import OpenAPIURLSession -import OversizeServices -import OpenAPIRuntime -import OpenAPIURLSession - -public actor NetworkService { - let client: Client - - public init() { - self.client = Client( - serverURL: try! Servers.server1(), - transport: URLSessionTransport() - ) - } - - public func fetchAdsBanners() async -> Result<[Components.Schemas.AdBanner], AppError> { - do { - let response = try await client.getBanners(.init()) - switch response { - case .ok(let okResponse): - switch okResponse.body { - case .json(let banners): - return .success(banners) - } - case .undocumented: - return .failure(.network(type: .noResponse)) - } - } catch { - return .failure(.network(type: .unknown)) - } - } -} diff --git a/Sources/OversizeNetwork/Repositories/NetworkService.swift b/Sources/OversizeNetwork/Repositories/NetworkService.swift new file mode 100644 index 0000000..c96f7e0 --- /dev/null +++ b/Sources/OversizeNetwork/Repositories/NetworkService.swift @@ -0,0 +1,91 @@ +// +// Copyright © 2023 Alexander Romanov +// NetworkService.swift, created on 30.06.2023 +// +import OpenAPIURLSession +import OversizeServices + +public struct NetworkService { + let client = Client( + serverURL: try! Servers.server1(), + transport: URLSessionTransport() + ) + + public init() {} + + public func fetchApps() async -> Result<[Components.Schemas.AppShort], AppError> { + do { + let response = try await client.fetchApps(.init()) + switch response { + case let .ok(okResponse): + switch okResponse.body { + case let .json(apps): + return .success(apps) + } + case .undocumented: + return .failure(.network(type: .unexpectedStatusCode)) + case .notFound: + return .failure(.network(type: .invalidURL)) + } + } catch { + return .failure(.network(type: .unknown)) + } + } + + public func fetchInfo() async -> Result { + do { + let response = try await client.fetchInfo(.init()) + switch response { + case let .ok(okResponse): + switch okResponse.body { + case let .json(info): + return .success(info) + } + case .undocumented: + return .failure(.network(type: .unexpectedStatusCode)) + case .notFound: + return .failure(.network(type: .invalidURL)) + } + } catch { + return .failure(.network(type: .unknown)) + } + } + + public func fetchAppById(appId: String) async -> Result { + do { + let response = try await client.fetchAppById(.init(path: .init(appId: appId))) + switch response { + case let .ok(okResponse): + switch okResponse.body { + case let .json(info): + return .success(info) + } + case .undocumented: + return .failure(.network(type: .unexpectedStatusCode)) + case .notFound: + return .failure(.network(type: .invalidURL)) + } + } catch { + return .failure(.network(type: .unknown)) + } + } + + public func fetchAds() async -> Result<[Components.Schemas.Ad], AppError> { + do { + let response = try await client.fetchAds(.init()) + switch response { + case let .ok(okResponse): + switch okResponse.body { + case let .json(ads): + return .success(ads) + } + case .undocumented: + return .failure(.network(type: .unexpectedStatusCode)) + case .notFound: + return .failure(.network(type: .invalidURL)) + } + } catch { + return .failure(.network(type: .unknown)) + } + } +} diff --git a/Sources/OversizeNetwork/ServiceRegistering.swift b/Sources/OversizeNetwork/Repositories/ServiceRegistering.swift similarity index 90% rename from Sources/OversizeNetwork/ServiceRegistering.swift rename to Sources/OversizeNetwork/Repositories/ServiceRegistering.swift index 350e3f3..d1c45fb 100644 --- a/Sources/OversizeNetwork/ServiceRegistering.swift +++ b/Sources/OversizeNetwork/Repositories/ServiceRegistering.swift @@ -5,11 +5,9 @@ import Factory import Foundation -import OversizeServices public extension Container { var networkService: Factory { self { NetworkService() } } } - diff --git a/Sources/openapi-generator-config.yaml b/Sources/OversizeNetwork/Repositories/openapi-generator-config.yaml similarity index 100% rename from Sources/openapi-generator-config.yaml rename to Sources/OversizeNetwork/Repositories/openapi-generator-config.yaml diff --git a/Sources/OversizeNetwork/Repositories/openapi.yaml b/Sources/OversizeNetwork/Repositories/openapi.yaml new file mode 100644 index 0000000..d0eb4aa --- /dev/null +++ b/Sources/OversizeNetwork/Repositories/openapi.yaml @@ -0,0 +1,313 @@ +openapi: 3.0.3 +info: + title: OversizeStaticAPI + description: OversizeAPI + version: 1.0.0 +servers: + - url: https://cdn.oversize.design +paths: + /config/apps-short.json: + get: + tags: + - app + summary: Fetch apps + operationId: fetchApps + responses: + '200': + description: Successful operation + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/AppShort' + '404': + description: Apps not found + /config/info.json: + get: + tags: + - app + summary: Fetch info + operationId: fetchInfo + responses: + '200': + description: Successful operation + content: + application/json: + schema: + $ref: '#/components/schemas/Info' + '404': + description: Info not found + /config/apps/{appId}.json: + get: + tags: + - app + summary: Find app by ID + description: Returns a single app + operationId: fetchAppById + parameters: + - name: appId + in: path + description: ID of pet to return + required: true + schema: + type: string + responses: + '200': + description: successful operation + content: + application/json: + schema: + $ref: '#/components/schemas/AppDetail' + '404': + description: App not found + /config/ads.json: + get: + tags: + - app + summary: Fetch ads + operationId: fetchAds + responses: + '200': + description: Successful operation + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Ad' + '404': + description: Apps not found +components: + schemas: + Info: + required: + - "developer" + - "company" + properties: + developer: + required: + - "email" + - "name" + - "socialNetworks" + - "url" + properties: + email: + type: "string" + example: "alexander@oversize.design" + name: + type: "string" + example: "Alexander Romanov" + socialNetworks: + items: + items: + $ref: '#/components/schemas/SocialNetworkLink' + url: + type: "string" + example: "https://romanov.cc" + type: "object" + company: + required: + - "name" + - "url" + - "cdnUrl" + - "email" + - "socialNetworks" + - "supportEmail" + properties: + name: + type: "string" + example: "Oversize" + url: + type: "string" + example: "https://oversize.design" + cdnUrl: + type: "string" + example: "https://cdn.oversize.design" + email: + type: "string" + example: "support@oversize.design" + supportEmail: + type: "string" + example: "support@oversize.design" + appStoreId: + type: "string" + example: "1459928735" + socialNetworks: + items: + $ref: '#/components/schemas/SocialNetworkLink' + type: "object" + SocialNetworkLink: + type: "object" + required: + - id + - title + - url + - iconUrl + properties: + title: + type: "string" + example: "facebook" + id: + type: "string" + example: "aromanov91" + url: + type: "string" + example: "https://www.facebook.com/aromanov91" + iconUrl: + type: string + example: "https://cdn.oversize.design/assets/icons/brands/dribbble.svg" + AppShort: + required: + - name + - title + - address + - appStoreId + - termsDate + properties: + name: + type: string + example: Calendar Do + title: + type: string + example: Add tasks to meetings + subtitle: + type: string + example: All your meetings on a simple list + description: + type: string + example: >- + A simple and convenient viewing of events from the calendar in the + form of a list of tasks with the possibility of adding subtasks to + events. Convenient adding new events to the calendar as in the lists + of tasks + address: + type: string + example: calendardo + appStoreId: + type: string + example: '1661200390' + termsDate: + type: string + example: '2022-12-31' + AppDetail: + required: + - name + - title + - subtitle + - address + - appStoreId + - termsDate + - platforms + properties: + name: + type: string + example: Calendar Do + title: + type: string + example: Add tasks to meetings + subtitle: + type: string + example: All your meetings on a simple list + description: + type: string + example: >- + A simple and convenient viewing of events from the calendar in the + form of a list of tasks with the possibility of adding subtasks to + events. Convenient adding new events to the calendar as in the lists + of tasks + address: + type: string + example: calendardo + appStoreId: + type: string + example: '1661200390' + termsDate: + type: string + example: '2022-12-31' + platforms: + properties: + iOS: + $ref: '#/components/schemas/Platform' + iPadOS: + $ref: '#/components/schemas/Platform' + macOS: + $ref: '#/components/schemas/Platform' + tvOS: + $ref: '#/components/schemas/Platform' + watchOS: + $ref: '#/components/schemas/Platform' + visionOS: + $ref: '#/components/schemas/Platform' + Platform: + type: object + properties: + blocks: + type: "array" + items: + $ref: '#/components/schemas/AppDetailBlock' + AppDetailBlock: + type: object + required: + - title + - design + - items + properties: + design: + type: string + example: cards + title: + type: string + example: Features + subtitle: + type: string + example: Features + description: + type: string + example: All data is saved in the phone or is possible synchronization with iCloud + color: + type: string + example: F53C6E + grid: + type: string + example: grid1_2 + screenshotUrl: + type: string + example: https://cdn.oversize.design/assets/apps/dressweather/screenshots/1.png + items: + items: + $ref: '#/components/schemas/AppDetailFeature' + AppDetailFeature: + type: object + required: + - title + properties: + title: + type: string + example: View events from the calendar + description: + type: string + example: All data is saved in the phone or is possible synchronization with iCloud + alignment: + type: string + example: center + textSize: + type: string + example: small + screenshotUrl: + type: string + example: https://cdn.oversize.design/assets/apps/dressweather/screenshots/1.png + iconUrl: + type: string + example: https://cdn.oversize.design/assets/apps/dressweather/features/2.png + Ad: + required: + - id + - priority + properties: + id: + type: string + example: 1661200390 + priority: + type: integer + example: 1 diff --git a/Sources/main.swift b/Sources/main.swift deleted file mode 100644 index 6532350..0000000 --- a/Sources/main.swift +++ /dev/null @@ -1,23 +0,0 @@ -import OpenAPIRuntime -import OpenAPIURLSession -//import OversizeServices - -//public actor NetworkService { -// let client: Client -// -// public init() { -// self.client = Client( -// serverURL: try! Servers.server1(), -// transport: URLSessionTransport() -// ) -// } -// -// public func fetchAccountStatus() async -> Result { -// do { -// let result = try await client.getBanners(.init()) -// return .success(result) -// } catch { -// return .failure(error) -// } -// } -//} diff --git a/Sources/openapi.yaml b/Sources/openapi.yaml deleted file mode 100644 index 0cfa615..0000000 --- a/Sources/openapi.yaml +++ /dev/null @@ -1,50 +0,0 @@ -openapi: 3.0.3 -info: - title: OversizeNetwork - version: 1.0.0 -servers: - - url: https://cdn.oversize.design - description: CDN -paths: - /config/ads.json: - get: - tags: - - CDN - summary: Ads banners - operationId: getBanners - responses: - '200': - description: successful operation - content: - application/json: - schema: - type: array - items: - $ref: '#/components/schemas/AdBanner' - -components: - schemas: - AdBanner: - type: object - required: - - id - - name - - title - - subtitle - - path - properties: - id: - type: "integer" - example: 6443709281 - name: - type: "string" - example: "Scale Down" - title: - type: "string" - example: "Control weight easily, follow the successes of every month" - subtitle: - type: "string" - example: "It is very difficult to reset everything" - path: - type: "string" - example: "scaledown"