From e6f29e9a4e69eec9af83ba4803d61fc29e52bf8d Mon Sep 17 00:00:00 2001 From: Selim Mustafaev Date: Fri, 15 Aug 2025 18:09:01 +0300 Subject: [PATCH] Getting html report --- AutoCat.xcodeproj/project.pbxproj | 4 +++ .../Services/ApiService/ApiService.swift | 34 +++++++++++++++++++ .../ApiService/ApiServiceProtocol.swift | 2 ++ .../Services/ApiService/AvtocodResponse.swift | 14 ++++++++ .../VehicleService/VehicleService.swift | 7 ++++ AutoCatCore/Utils/Constants.swift | 3 ++ 6 files changed, 64 insertions(+) create mode 100644 AutoCatCore/Services/ApiService/AvtocodResponse.swift diff --git a/AutoCat.xcodeproj/project.pbxproj b/AutoCat.xcodeproj/project.pbxproj index 76fd61d..0e926d4 100644 --- a/AutoCat.xcodeproj/project.pbxproj +++ b/AutoCat.xcodeproj/project.pbxproj @@ -46,6 +46,7 @@ 7A45FB382C27073700618694 /* StorageService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A45FB372C27073700618694 /* StorageService.swift */; }; 7A4927D52CCE438600851C01 /* OptionalBinding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A4927D42CCE438600851C01 /* OptionalBinding.swift */; }; 7A4955822D58CCF900912E66 /* HistoryFilter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A4955812D58CCF900912E66 /* HistoryFilter.swift */; }; + 7A4BB8622E4F5EDC00AB22C9 /* AvtocodResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A4BB8612E4F5EDC00AB22C9 /* AvtocodResponse.swift */; }; 7A54BFD32D43B95E00176D6D /* DbUpdatePolicy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A54BFD22D43B95E00176D6D /* DbUpdatePolicy.swift */; }; 7A589E0F2D6B6E8E00EF3FBE /* NumberEditView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A589E0E2D6B6E8E00EF3FBE /* NumberEditView.swift */; }; 7A5911EE2D63226F00EC51BA /* SearchScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A5911ED2D63226F00EC51BA /* SearchScreen.swift */; }; @@ -303,6 +304,7 @@ 7A45FB372C27073700618694 /* StorageService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageService.swift; sourceTree = ""; }; 7A4927D42CCE438600851C01 /* OptionalBinding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OptionalBinding.swift; sourceTree = ""; }; 7A4955812D58CCF900912E66 /* HistoryFilter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoryFilter.swift; sourceTree = ""; }; + 7A4BB8612E4F5EDC00AB22C9 /* AvtocodResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AvtocodResponse.swift; sourceTree = ""; }; 7A52AB292580112E002CD910 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = ""; }; 7A530B7F2401803A00CBFE6E /* Vehicle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Vehicle.swift; sourceTree = ""; }; 7A54BFD22D43B95E00176D6D /* DbUpdatePolicy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DbUpdatePolicy.swift; sourceTree = ""; }; @@ -975,6 +977,7 @@ 7A599C352C18AC7F00D47C18 /* ApiError.swift */, 7A11474823FF2B2D00B424AF /* Response.swift */, 7AEAA2A02DAD9C00009954F0 /* TokenResponse.swift */, + 7A4BB8612E4F5EDC00AB22C9 /* AvtocodResponse.swift */, ); path = ApiService; sourceTree = ""; @@ -1492,6 +1495,7 @@ 7A6F096026DBF588003A965D /* VehicleNote.swift in Sources */, 7AD8572D2DF95F72009E4B72 /* SwiftDataStorageService.swift in Sources */, 7AF6D21E2677C1680086EA64 /* PlateNumber.swift in Sources */, + 7A4BB8622E4F5EDC00AB22C9 /* AvtocodResponse.swift in Sources */, 7A5D84C62C1AE72E00C2209B /* VehicleName.swift in Sources */, 7AD8572A2DF87928009E4B72 /* SDAudioRecord.swift in Sources */, 7A64A2122C19E2A100284124 /* VehicleModelDto.swift in Sources */, diff --git a/AutoCatCore/Services/ApiService/ApiService.swift b/AutoCatCore/Services/ApiService/ApiService.swift index 7b8b7c3..777bccf 100644 --- a/AutoCatCore/Services/ApiService/ApiService.swift +++ b/AutoCatCore/Services/ApiService/ApiService.swift @@ -252,6 +252,7 @@ public actor ApiService: ApiServiceProtocol { numberType: VehicleNumberType, notes: [VehicleNoteDto], events: [VehicleEventDto], + avtocodReport: String?, force: Bool = false ) async throws -> VehicleDto { @@ -356,4 +357,37 @@ public actor ApiService: ApiServiceProtocol { return try await makeBodyRequest(api: "vehicles/checkGbTg", body: body) } + + public func getAvtocodHtml(by number: String, numberType: VehicleNumberType) async throws -> String? { + guard var urlComponents = URLComponents(string: Constants.avtocodBaseURL + "auto/generate") else { + return nil + } + + urlComponents.queryItems = [ + URLQueryItem(name: "number", value: number.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)), + URLQueryItem(name: "type", value: numberType.value), + URLQueryItem(name: "device_token", value: UUID().uuidString) + ] + + guard let url = urlComponents.url else { + return nil + } + + var request = URLRequest(url: url) + request.addValue(Constants.avtocodUserAgent, forHTTPHeaderField: "User-Agent") + + let (data, _) = try await session.data(for: request) + let json = try JSONDecoder().decode(AvtocodResponse.self, from: data) + + guard let reportUrl = json.report_uri.flatMap(URL.init(string:)) else { + return nil + } + + request = URLRequest(url: reportUrl) + request.addValue(Constants.avtocodUserAgent, forHTTPHeaderField: "User-Agent") + let (reportData, _) = try await session.data(for: request) + let reportBase64 = reportData.base64EncodedString() + + return reportBase64 + } } diff --git a/AutoCatCore/Services/ApiService/ApiServiceProtocol.swift b/AutoCatCore/Services/ApiService/ApiServiceProtocol.swift index 433e976..745dce5 100644 --- a/AutoCatCore/Services/ApiService/ApiServiceProtocol.swift +++ b/AutoCatCore/Services/ApiService/ApiServiceProtocol.swift @@ -34,6 +34,7 @@ public protocol ApiServiceProtocol: Sendable { numberType: VehicleNumberType, notes: [VehicleNoteDto], events: [VehicleEventDto], + avtocodReport: String?, force: Bool ) async throws -> VehicleDto func checkVehicleGb(by number: String) async throws -> VehicleDto @@ -42,4 +43,5 @@ public protocol ApiServiceProtocol: Sendable { func fbVerifyAssertion(provider: String, idToken: String, accessToken: String?) async func getToken(code: String, codeVerifier: String) async throws -> TokenResponse func getReport(for number: String) async throws -> VehicleDto + func getAvtocodHtml(by number: String, numberType: VehicleNumberType) async throws -> String? } diff --git a/AutoCatCore/Services/ApiService/AvtocodResponse.swift b/AutoCatCore/Services/ApiService/AvtocodResponse.swift new file mode 100644 index 0000000..530c892 --- /dev/null +++ b/AutoCatCore/Services/ApiService/AvtocodResponse.swift @@ -0,0 +1,14 @@ +// +// AvtocodResponse.swift +// AutoCatCore +// +// Created by Selim Mustafaev on 15.08.2025. +// Copyright © 2025 Selim Mustafaev. All rights reserved. +// + +import Foundation + +struct AvtocodResponse: Decodable { + + let report_uri: String? +} diff --git a/AutoCatCore/Services/VehicleService/VehicleService.swift b/AutoCatCore/Services/VehicleService/VehicleService.swift index d4479f8..b5f6b0f 100644 --- a/AutoCatCore/Services/VehicleService/VehicleService.swift +++ b/AutoCatCore/Services/VehicleService/VehicleService.swift @@ -65,11 +65,18 @@ extension VehicleService: VehicleServiceProtocol { } async let locationTask = trackLocation ? locationService.getRecentLocation() : nil + + let avtocodReport = try? await apiService.getAvtocodHtml( + by: number, + numberType: numberType + ) + async let vehicleTask = apiService.checkVehicle( by: number, numberType: numberType, notes: notes, events: events, + avtocodReport: avtocodReport, force: forceUpdate ) diff --git a/AutoCatCore/Utils/Constants.swift b/AutoCatCore/Utils/Constants.swift index 92d23dd..8f623ed 100644 --- a/AutoCatCore/Utils/Constants.swift +++ b/AutoCatCore/Utils/Constants.swift @@ -55,4 +55,7 @@ public enum Constants { public static let audioRecordsFolder = "recordings" public static let userAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 18_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.4 Mobile/15E148 Safari/604.1" + + public static let avtocodBaseURL = "https://avtocod.ru/api/v4/" + public static let avtocodUserAgent = "avtocod/3 CFNetwork/3826.600.41 Darwin/24.6.0" }