Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Week8,9-이현희] API 통신 미션과 #43

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
626 changes: 626 additions & 0 deletions 06_hyunhee/week08/APITest/APITest.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
17 changes: 17 additions & 0 deletions 06_hyunhee/week08/APITest/APITest/APITestApp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// APITestApp.swift
// APITest
//
// Created by lee hyunhee on 5/24/24.
//

import SwiftUI

@main
struct APITestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"images" : [
{
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
48 changes: 48 additions & 0 deletions 06_hyunhee/week08/APITest/APITest/ContentView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// ContentView.swift
// APITest
//
// Created by lee hyunhee on 5/24/24.
//

import SwiftUI

struct ContentView: View {
@State var users: [User] = []
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
VStack {
ForEach(users, id:\.self.id) { user in
Text(user.name)
}
}

Button("add user") {
MyAPI().addUser { u in
MyAPI().getUser() { u1 in
users = u1
}
}
}
Button("delete user") {
MyAPI().deleteUserById(id: 5) { u in
MyAPI().getUser() { u1 in
users = u1
}
}
}
}
.onAppear {
MyAPI().getUser { u in
users = u
}
}
}
}

#Preview {
ContentView()
}
63 changes: 63 additions & 0 deletions 06_hyunhee/week08/APITest/APITest/MyAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// MyAPI.swift
// APITest
//
// Created by lee hyunhee on 5/24/24.
//

import Foundation
import Alamofire

struct User: Codable {
let id: String
let name: String
let email: String
let age: Int
}

class MyAPI {
func getUser(completion: @escaping (_ users: [User]) -> Void) {
let url = "https://665006e1ec9b4a4a60306fd2.mockapi.io/User"

let dataRequest = AF.request(url, method: .get, encoding: JSONEncoding.default)
dataRequest.responseDecodable(of: [User].self) { response in
switch response.result {
case .success(let response):
print("success: \(response)")
completion(response)
case .failure(let error):
print("debug error: \(error)")
}
}
}

func addUser(completion: @escaping (_ users: User) -> Void) {
let url = "https://665006e1ec9b4a4a60306fd2.mockapi.io/User/"

let dataRequest = AF.request(url, method: .post, encoding: JSONEncoding.default)
dataRequest.responseDecodable(of: User.self) { response in
switch response.result {
case .success(let response):
print("success: \(response)")
completion(response)
case .failure(let error):
print("debug error: \(error)")
}
}
}

func deleteUserById(id: Int, completion: @escaping (_ users: User) -> Void) {
let url = "https://665006e1ec9b4a4a60306fd2.mockapi.io/User/\(id)"

let dataRequest = AF.request(url, method: .delete, encoding: JSONEncoding.default)
dataRequest.responseDecodable(of: User.self) { response in
switch response.result {
case .success(let response):
print("success: \(response)")
completion(response)
case .failure(let error):
print("debug error: \(error)")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
36 changes: 36 additions & 0 deletions 06_hyunhee/week08/APITest/APITestTests/APITestTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// APITestTests.swift
// APITestTests
//
// Created by lee hyunhee on 5/24/24.
//

import XCTest
@testable import APITest

final class APITestTests: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
// Any test you write for XCTest can be annotated as throws and async.
// Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
// Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
}

func testPerformanceExample() throws {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}

}
41 changes: 41 additions & 0 deletions 06_hyunhee/week08/APITest/APITestUITests/APITestUITests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// APITestUITests.swift
// APITestUITests
//
// Created by lee hyunhee on 5/24/24.
//

import XCTest

final class APITestUITests: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.

// In UI tests it is usually best to stop immediately when a failure occurs.
continueAfterFailure = false

// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}

override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testExample() throws {
// UI tests must launch the application that they test.
let app = XCUIApplication()
app.launch()

// Use XCTAssert and related functions to verify your tests produce the correct results.
}

func testLaunchPerformance() throws {
if #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 7.0, *) {
// This measures how long it takes to launch your application.
measure(metrics: [XCTApplicationLaunchMetric()]) {
XCUIApplication().launch()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// APITestUITestsLaunchTests.swift
// APITestUITests
//
// Created by lee hyunhee on 5/24/24.
//

import XCTest

final class APITestUITestsLaunchTests: XCTestCase {

override class var runsForEachTargetApplicationUIConfiguration: Bool {
true
}

override func setUpWithError() throws {
continueAfterFailure = false
}

func testLaunch() throws {
let app = XCUIApplication()
app.launch()

// Insert steps here to perform after app launch but before taking a screenshot,
// such as logging into a test account or navigating somewhere in the app

let attachment = XCTAttachment(screenshot: app.screenshot())
attachment.name = "Launch Screen"
attachment.lifetime = .keepAlways
add(attachment)
}
}
Loading