-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForMyPhoachServer.swift
151 lines (115 loc) · 4.92 KB
/
ForMyPhoachServer.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// ForMyPhoachServer.swift
// PhoachingWithDB
//
// Created by Artem Vekshin on 14.07.2023.
import Foundation
// MARK: - PhoachresultsForMyElement
struct PhoachresultsForMyElement: Codable, Hashable, Identifiable {
let id: Int
let created, updated: String
let status: Status
let userID: Int
let accessgroup, title: String
let description: String?
let author, duration: String
let cost: JSONNull?
let dayWithTitleInJSON, quantityFailTasksForFailSeminar: String?
let checklistid: Int?
enum CodingKeys: String, CodingKey {
case id, created, updated, status
case userID = "userId"
case accessgroup, title, description, author, duration, cost
case dayWithTitleInJSON = "day_with_title_in_json"
case quantityFailTasksForFailSeminar = "quantity_fail_tasks_for_fail_seminar"
case checklistid
}
}
enum Status: String, Codable {
case active = "ACTIVE"
}
typealias PhoachresultsForMy = [PhoachresultsForMyElement]
// MARK: - Encode/decode helpers
class JSONNull: Codable, Hashable {
public static func == (lhs: JSONNull, rhs: JSONNull) -> Bool {
return true
}
public var hashValue: Int {
return 0
}
public init() {}
public required init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if !container.decodeNil() {
throw DecodingError.typeMismatch(JSONNull.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for JSONNull"))
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encodeNil()
}
}
class MyphoachServer: ObservableObject{
static let shared = MyphoachServer()
func sendRequestWithAuthorization(completion: @escaping ([PhoachresultsForMyElement]?) -> Void) {
let url = URL(string: "http://90.156.205.157:8090/api/v1/users/seminarofuser")!
let session = URLSession.shared
var request = URLRequest(url: url)
// Устанавливаем метод запроса (GET, POST, PUT, DELETE и т.д.)
request.httpMethod = "GET"
// Устанавливаем заголовок Authorization
let token = "Bearer_eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJuaWtlcjY4QHlhbmRleC5ydSIsInJvbGVzIjpbIlJPTEVfVVNFUiJdLCJpYXQiOjE2ODc2NzI5NjgsImV4cCI6MTcyMzY3Mjk2OH0.g4mpGK5-xS2SA2pLBy_4GekmX-D_bU_1UdIB-wtNHJo"
request.setValue(token, forHTTPHeaderField: "Authorization")
let task = session.dataTask(with: request) { (data, response, error) in
if let error = error {
print("Ошибка: \(error.localizedDescription)")
return
}
//Acces to HTTP Server
guard response is HTTPURLResponse else { return }
// Обрабатываем ответ сервера
if let data = data {
if let phoachDataforme = try? JSONDecoder().decode([PhoachresultsForMyElement]?.self, from: data) {
completion(phoachDataforme)
} else {
print("FAILwithMy")}
}
}
task.resume()
}
// func deleteItem(itemID: Int, completion: @escaping (Bool) -> Void) {
//
// let url = URL(string: "http://90.156.205.157:8090/api/v1/users/seminarofuser/\(itemID)")!
// let session = URLSession.shared
// var request = URLRequest(url: url)
//
// // Устанавливаем метод запроса (GET, POST, PUT, DELETE и т.д.)
// request.httpMethod = "DELETE"
//
// // Устанавливаем заголовок Authorization
// let token = "Bearer_eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJuaWtlcjY4QHlhbmRleC5ydSIsInJvbGVzIjpbIlJPTEVfVVNFUiJdLCJpYXQiOjE2ODc2NzI5NjgsImV4cCI6MTcyMzY3Mjk2OH0.g4mpGK5-xS2SA2pLBy_4GekmX-D_bU_1UdIB-wtNHJo"
// request.setValue(token, forHTTPHeaderField: "Authorization")
//
// let task = session.dataTask(with: request) { (data, response, error) in
// if let error = error {
// print("Ошибка: \(error.localizedDescription)")
// return
// }
// //Acces to HTTP Server
// guard response is HTTPURLResponse else { return }
//
// // Обрабатываем ответ сервера
// if let data = data {
// if let phoachDataforme = try? JSONDecoder().decode([PhoachresultsForMyElement]?.self, from: data) {
// completion(phoachDataforme)
//
//
// } else {
// print("FAILwithMy")}
// }
//
//
// }
// task.resume()
// }
}