-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/ProjectInTheClass/Noto
- Loading branch information
Showing
23 changed files
with
2,520 additions
and
514 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// Delete_Api.swift | ||
// noto-App | ||
// | ||
// Created by 박상현 on 12/10/24. | ||
// | ||
import SwiftUI | ||
|
||
// DELETE 요청을 처리하는 함수 | ||
func delete(url: String) async throws -> ApiModel<String?> { | ||
// URL을 유효한 형식으로 만들기 | ||
guard let url = URL(string: url) else { | ||
throw NSError(domain: "Invalid URL", code: 0, userInfo: nil) // URL이 유효하지 않으면 에러 던지기 | ||
} | ||
|
||
// URLRequest 객체 생성 | ||
var request = URLRequest(url: url) | ||
request.httpMethod = "DELETE" // DELETE 요청 메소드 설정 | ||
|
||
// 비동기적으로 DELETE 요청 보내기 | ||
let (data, _) = try await URLSession.shared.data(for: request) | ||
|
||
// 응답 데이터 출력 (디버깅용) | ||
if let jsonString = String(data: data, encoding: .utf8) { | ||
print("Raw JSON Response: \(jsonString)") | ||
} | ||
|
||
// JSONDecoder 생성 | ||
let decoder = JSONDecoder() | ||
|
||
// Custom DateFormatter를 사용하여 특정 날짜 형식 처리 | ||
let formatter = DateFormatter() | ||
formatter.dateFormat = "yyyy-MM-dd" // 필요한 날짜 형식으로 설정 | ||
|
||
// 커스텀 포맷터를 디코더에 설정 | ||
decoder.dateDecodingStrategy = .formatted(formatter) | ||
|
||
// API 응답을 지정된 모델로 디코딩 | ||
let apiResponse = try decoder.decode(ApiModel<String?>.self, from: data) | ||
return apiResponse | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// Get_Api.swift | ||
// noto-App | ||
// | ||
// Created by 박상현 on 12/6/24. | ||
// | ||
import SwiftUI | ||
|
||
// GET 요청을 처리하는 함수 | ||
func get<T: Codable>(url: String, responseType: T.Type) async throws -> ApiModel<T> { | ||
// Try to create a URL from the string | ||
guard let url = URL(string: url) else { | ||
throw NSError(domain: "Invalid URL", code: 0, userInfo: nil) // Throw error if URL is invalid | ||
} | ||
|
||
// Now `url` is of type URL, so we can proceed with the network request | ||
let (data, _) = try await URLSession.shared.data(from: url) | ||
|
||
// Print the raw data for debugging | ||
if let jsonString = String(data: data, encoding: .utf8) { | ||
print("Raw JSON Response: \(jsonString)") | ||
} | ||
|
||
// Create a JSON decoder | ||
let decoder = JSONDecoder() | ||
|
||
// Custom DateFormatter for the expected date format | ||
let formatter = DateFormatter() | ||
formatter.dateFormat = "yyyy-MM-dd" // Specify your custom date format here | ||
|
||
// Set the custom formatter to the decoder | ||
decoder.dateDecodingStrategy = .formatted(formatter) | ||
|
||
// Decode the API response | ||
let apiResponse = try decoder.decode(ApiModel<T>.self, from: data) | ||
return apiResponse | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// Model_Api.swift | ||
// noto-App | ||
// | ||
// Created by 박상현 on 12/6/24. | ||
// | ||
import SwiftUI | ||
|
||
struct ApiModel<T: Codable>: Codable { | ||
let code: String | ||
let message: String | ||
let data: T | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// Patch_Api.swift | ||
// noto-App | ||
// | ||
// Created by 박상현 on 12/12/24. | ||
// | ||
import SwiftUI | ||
|
||
// PATCH 요청을 처리하는 함수 | ||
func patch<U: Codable>(url: String, body: U) async throws -> ApiModel<String?> { | ||
// Try to create a URL from the string | ||
guard let url = URL(string: url) else { | ||
throw NSError(domain: "Invalid URL", code: 0, userInfo: nil) // Throw error if URL is invalid | ||
} | ||
|
||
// Create the URLRequest for the PUT request | ||
var request = URLRequest(url: url) | ||
request.httpMethod = "PATCH" // HTTP method 변경 (PUT) | ||
|
||
// Set the request's content-type header (application/json for JSON data) | ||
request.setValue("application/json", forHTTPHeaderField: "Content-Type") | ||
|
||
// Encode the body into JSON | ||
let encoder = JSONEncoder() | ||
|
||
// 날짜 포맷 설정 | ||
let formatter = DateFormatter() | ||
formatter.dateFormat = "yyyy-MM-dd" // 서버에서 요구하는 날짜 형식으로 설정 | ||
|
||
// JSONEncoder에 날짜 인코딩 전략 설정 | ||
encoder.dateEncodingStrategy = .formatted(formatter) | ||
|
||
let bodyData = try encoder.encode(body) | ||
request.httpBody = bodyData | ||
|
||
// Perform the network request and get the response data | ||
let (data, _) = try await URLSession.shared.data(for: request) | ||
|
||
// Print the raw data for debugging | ||
if let jsonString = String(data: data, encoding: .utf8) { | ||
print("Raw JSON Response: \(jsonString)") | ||
} | ||
|
||
// Create a JSON decoder | ||
let decoder = JSONDecoder() | ||
|
||
// Set the custom formatter to the decoder | ||
decoder.dateDecodingStrategy = .formatted(formatter) | ||
|
||
// Decode the API response | ||
let apiResponse = try decoder.decode(ApiModel<String?>.self, from: data) | ||
return apiResponse | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// Post_Api.swift | ||
// noto-App | ||
// | ||
// Created by 박상현 on 12/10/24. | ||
// | ||
import SwiftUI | ||
|
||
// POST 요청을 처리하는 함수 | ||
func post<T: Codable, U: Codable>(url: String, body: U, responseType: T.Type) async throws -> ApiModel<T> { | ||
// Try to create a URL from the string | ||
guard let url = URL(string: url) else { | ||
throw NSError(domain: "Invalid URL", code: 0, userInfo: nil) // Throw error if URL is invalid | ||
} | ||
|
||
// Create the URLRequest for the POST request | ||
var request = URLRequest(url: url) | ||
request.httpMethod = "POST" | ||
|
||
// Set the request's content-type header (application/json for JSON data) | ||
request.setValue("application/json", forHTTPHeaderField: "Content-Type") | ||
|
||
// Encode the body into JSON | ||
let encoder = JSONEncoder() | ||
|
||
// 날짜 포맷 설정 | ||
let formatter0 = DateFormatter() | ||
formatter0.dateFormat = "yyyy-MM-dd" // 서버에서 요구하는 날짜 형식으로 설정 | ||
|
||
// JSONEncoder에 날짜 인코딩 전략 설정 | ||
encoder.dateEncodingStrategy = .formatted(formatter0) | ||
|
||
let bodyData = try encoder.encode(body) | ||
request.httpBody = bodyData | ||
|
||
// Perform the network request and get the response data | ||
let (data, _) = try await URLSession.shared.data(for: request) | ||
|
||
// Print the raw data for debugging | ||
if let jsonString = String(data: data, encoding: .utf8) { | ||
print("Raw JSON Response: \(jsonString)") | ||
} | ||
|
||
// Create a JSON decoder | ||
let decoder = JSONDecoder() | ||
|
||
// Custom DateFormatter for the expected date format | ||
let formatter1 = DateFormatter() | ||
formatter1.dateFormat = "yyyy-MM-dd" // Specify your custom date format here | ||
|
||
// Set the custom formatter to the decoder | ||
decoder.dateDecodingStrategy = .formatted(formatter1) | ||
|
||
// Decode the API response | ||
let apiResponse = try decoder.decode(ApiModel<T>.self, from: data) | ||
return apiResponse | ||
} |
Oops, something went wrong.