You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
6.0 KiB
125 lines
6.0 KiB
//
|
|
// NetworkFederalService.swift
|
|
// PadelClub
|
|
//
|
|
// Created by Razmig Sarkissian on 21/03/2024.
|
|
//
|
|
|
|
import Foundation
|
|
import CoreLocation
|
|
import PadelClubData
|
|
|
|
class NetworkFederalService {
|
|
struct HttpCommand: Decodable {
|
|
let command: String
|
|
let results: HttpResults?
|
|
let method : String?
|
|
let selector : String?
|
|
let data: String?
|
|
}
|
|
|
|
struct HttpResults: Decodable {
|
|
let nb_results : Int
|
|
let items: [FederalTournament]?
|
|
}
|
|
|
|
static let shared: NetworkFederalService = NetworkFederalService()
|
|
var formId = ""
|
|
|
|
var tenupJsonDecoder: JSONDecoder = {
|
|
let decoder = JSONDecoder()
|
|
let dateFormatter = DateFormatter()
|
|
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
|
|
decoder.dateDecodingStrategy = .formatted(dateFormatter)
|
|
return decoder
|
|
}()
|
|
|
|
func runTenupTask<T: Decodable>(request: URLRequest) async throws -> T {
|
|
let (data, response) = try await URLSession.shared.data(for: request)
|
|
|
|
// Print request info
|
|
print("Request: \(request.httpMethod ?? "GET") \(request.url?.absoluteString ?? "")")
|
|
|
|
// Print response status
|
|
if let httpResponse = response as? HTTPURLResponse {
|
|
print("Status code: \(httpResponse.statusCode)")
|
|
}
|
|
|
|
// Print JSON data before decoding
|
|
if let jsonObject = try? JSONSerialization.jsonObject(with: data) {
|
|
//print("Response JSON: \(jsonObject)")
|
|
} else {
|
|
print("Response is not a valid JSON")
|
|
// Try to print as string if not JSON
|
|
if let stringResponse = String(data: data, encoding: .utf8) {
|
|
print("Response as string: \(stringResponse)")
|
|
}
|
|
}
|
|
|
|
// Now try to decode
|
|
do {
|
|
return try tenupJsonDecoder.decode(T.self, from: data)
|
|
} catch {
|
|
print("Decoding error: \(error)")
|
|
throw error
|
|
}
|
|
}
|
|
|
|
func federalClubs(country: String = "fr", city: String, radius: Double, location: CLLocation? = nil) async throws -> FederalClubResponse {
|
|
|
|
return try await FederalDataService.shared.federalClubs(country: country, city: city, radius: radius, location: location)
|
|
/*
|
|
{
|
|
"geocoding[country]": "fr",
|
|
"geocoding[ville]": "Cayenne, 973, Guyane",
|
|
"geocoding[rayon]": "15",
|
|
"geocoding[userPosition][lng]": "-52.311583",
|
|
"geocoding[userPosition][lat]": "4.925248",
|
|
"geocoding[userPosition][showDistance]": "false",
|
|
"pratiqueOption[0]": "PADEL",
|
|
"nombreResultat": "6",
|
|
"diplomeEtatOption": "false",
|
|
"galaxieOption": "false",
|
|
"fauteuilOption": "false",
|
|
"tennisSanteOption": "false"
|
|
}
|
|
*/
|
|
|
|
var parameters = "geocoding[country]=\(country)&geocoding[ville]=\(city)&geocoding[rayon]=\(Int(radius))&pratiqueOption[0]=Padel"
|
|
|
|
if let location {
|
|
parameters = parameters + "&geocoding[userPosition][lat]=\(location.coordinate.latitude.formatted(.number.locale(Locale(identifier: "us"))))&geocoding[userPosition][lng]=\(location.coordinate.longitude.formatted(.number.locale(Locale(identifier: "us"))))&geocoding[userPosition][showDistance]=true"
|
|
}
|
|
//"geocoding%5Bcountry%5D=fr&geocoding%5Bville%5D=13%20Avenue%20Emile%20Bodin%2013260%20Cassis&geocoding%5Brayon%5D=15&geocoding%5BuserPosition%5D%5Blat%5D=43.22278594081477&geocoding%5BuserPosition%5D%5Blng%5D=5.556953900769194&geocoding%5BuserPosition%5D%5BshowDistance%5D=true&nombreResultat=0&diplomeEtatOption=false&galaxieOption=false&fauteuilOption=false&tennisSanteOption=false"
|
|
let postData = parameters.data(using: .utf8)
|
|
|
|
var request = URLRequest(url: URL(string: "https://tenup.fft.fr/recherche/clubs/ajax")!)
|
|
request.addValue("application/json, text/plain, */*", forHTTPHeaderField: "Accept")
|
|
request.addValue("fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3", forHTTPHeaderField: "Accept-Language")
|
|
request.addValue("gzip, deflate, br", forHTTPHeaderField: "Accept-Encoding")
|
|
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
|
request.addValue("https://tenup.fft.fr", forHTTPHeaderField: "Origin")
|
|
request.addValue("keep-alive", forHTTPHeaderField: "Connection")
|
|
request.addValue("https://tenup.fft.fr/recherche/clubs/list", forHTTPHeaderField: "Referer")
|
|
// request.addValue("a20ba3b563e5ce7ad731c2c1076b217f=a2de91fbefddf75ea4aa86297ed09bd5; visid_incap_2712217=TZgb6G1zTsiPtpJ4cCmOErtj8GQAAAAAQUIPAAAAAAC5nrgD+rm7QWCdUN5I8Y6T; nlbi_2712217=Ug01X5TrSizGkQw5qBb2twAAAAAOvBNMkIHMeRAJGDiOaFxs; incap_ses_391_2712217=E60LNJzW7B+BjW0qWx1tBbtj8GQAAAAAlw5keZVI9C7egwKQblAHeQ==; TCPID=1238411561211442193459; TCID=; incap_ses_391_2712217=MzHHL4jK9k4gpmkqWx1tBT9i8GQAAAAA1k2Eroyuow6SC5Zmf1WtVA==; visid_incap_2712217=lVlg9romTq6I9k4sVklsgr9F72QAAAAAQUIPAAAAAADw7ISp7aFXSsqidxqlj3Df", forHTTPHeaderField: "Cookie")
|
|
request.addValue("empty", forHTTPHeaderField: "Sec-Fetch-Dest")
|
|
request.addValue("cors", forHTTPHeaderField: "Sec-Fetch-Mode")
|
|
request.addValue("same-origin", forHTTPHeaderField: "Sec-Fetch-Site")
|
|
request.addValue("trailers", forHTTPHeaderField: "TE")
|
|
|
|
request.httpMethod = "POST"
|
|
request.httpBody = postData
|
|
|
|
return try await runTenupTask(request: request)
|
|
}
|
|
|
|
|
|
func getClubFederalTournaments(page: Int, tournaments: [FederalTournament], club: String, codeClub: String, startDate: Date? = nil, endDate: Date? = nil) async throws -> [FederalTournament] {
|
|
return try await FederalDataService.shared.getClubFederalTournaments(page: page, tournaments: tournaments, club: club, codeClub: codeClub, startDate: startDate, endDate: endDate).tournaments
|
|
}
|
|
|
|
func getUmpireData(idTournament: String) async throws -> (name: String?, email: String?, phone: String?) {
|
|
return try await FederalDataService.shared.getUmpireData(idTournament: idTournament)
|
|
}
|
|
|
|
}
|
|
|