@@ -11,15 +11,174 @@ import Alamofire
12class Model{
12class Model{
13 static var url = URL(string:"https://valera-denis.herokuapp.com/authenticate")!
13 static var url = URL(string:"https://valera-denis.herokuapp.com/authenticate")!
14+ static var url1 = URL(string:"https://valera-denis.herokuapp.com/posts/last")!
15+ static var urlForDrafts = URL(string:"https://valera-denis.herokuapp.com/drafts/create")!
16+ static var urlForPublish = URL(string:"https://valera-denis.herokuapp.com/drafts/publish")!
17+ static var urlForUpdate = URL(string:"https://valera-denis.herokuapp.com/drafts/update")!
19+ struct Posts: Codable {
20+ var body: [Attachments]
24+ init(body: [Attachments], authorId: Int, id: Int) {
26+ self.authorId = authorId
31+ struct Attachments: Codable {
34+ init(markdown: String) {
35+ self.markdown = markdown
15 static func authenticate(){
39 static func authenticate(){
17− let params: Parameters = ["authenticationId" : 9]
41+ let json: [String:Any] = ["authenticationId" : 9]
18− AF.request(url, method: .post, parameters: params).response{
42+ let jsonData = try? JSONSerialization.data(withJSONObject: json)
44+ var request = URLRequest(url: url)
45+ request.httpMethod = "POST"
47+ // insert json data to the request
48+ request.httpBody = jsonData
49+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
51+ AF.request(request).response { (responce) in
53+ guard let realResponse = responce.response, realResponse.statusCode == 204 else {
54+ print("Not a 204 response")
58+ let fields = realResponse.allHeaderFields as? [String :String]
60+ let cookies = HTTPCookie.cookies(withResponseHeaderFields: fields!, for: realResponse.url!)
63+ for cookie in cookies {
64+ HTTPCookieStorage.shared.setCookie(cookie)
65+ DataStorage.standard.cookie = cookie
71+ static func getLast(completion: @escaping ((Channel)->())){
72+ let jsonString = "100"
73+ var request = URLRequest(url: url1)
75+ request.httpMethod = "POST"
77+ // insert json data to the request
78+ request.httpBody = jsonString.data(using: .utf8)
79+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
81+ AF.request(request).responseJSON {
84+ if response.response?.statusCode == 200 {
88+ let decoder = JSONDecoder()
89+ guard let json = response.data else { return }
91+ guard let newPost = try? decoder.decode([Posts].self, from: json) else { return }
21− print(responce.response?.allHeaderFields)
93+ completion(Channel(title: "# General", subtitle: "No subtitle", hashtags: ["ПИ"], people: ["No"], posts: newPost))
97+ static func createDraft(completion: @escaping ((Int)->())){
98+ let jsonString = "[{ \"markdown\": \"# This is a markdown title\nThis is body.\" }]"
99+ var request = URLRequest(url: urlForDrafts)
101+ request.httpMethod = "POST"
103+ // insert json data to the request
104+ request.httpBody = jsonString.data(using: .utf8)
105+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
107+ AF.request(request).responseJSON {
110+ if response.response?.statusCode == 200 {
111+ print("success in creating draft")
113+ print("\(response.response?.statusCode ?? 0)")
116+ //let decoder = JSONDecoder()
117+ guard let json = response.data else { return }
119+ guard let id = String(data: json, encoding: String.Encoding.utf8) else {return}
121+ print("with id \(id)")
122+ completion(Int(id) ?? 0)
126+ static var seeMe: Int = 0 {
132+ static func publish(){
133+ let jsonUpd = "\(seeMe)"
134+ var request = URLRequest(url: urlForPublish)
136+ request.httpMethod = "POST"
138+ // insert json data to the request
139+ request.httpBody = jsonUpd.data(using: .utf8)
140+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
141+ AF.request(request).response {
144+ if response.response?.statusCode == 204 {
145+ print("successfuly published \(jsonUpd)")
150+ static func update(post: Posts){
151+ let transformed = post.body.reduce("") { (res, att) -> String in
159+ "markdown": "\(transformed)"
166+ var request = URLRequest(url: urlForUpdate)
168+ request.httpMethod = "POST"
170+ // insert json data to the request
171+ request.httpBody = jsonUpd.data(using: .utf8)
172+ request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
173+ AF.request(request).response {
176+ if response.response?.statusCode == 204 {
177+ print("successfuly updated \(post.id) with \(jsonUpd)")
180+ print(response.response?.statusCode)