calling existing AWS Lambda functions using the API Gateway URL directly with Swift?

I had a very basic Flask server while developing my apis locally and now it’s time to port them over to AWS Lambda which I’ve done so using serverless to get everything deployed through configuration files without any problems.

Is this the only route I can go? https://docs.amplify.aws/swift/build-a-backend/auth/use-existing-cognito-resources/#use-auth-resources-without-an-amplify-backend ?

Is it possible to not rely on 3rd party libraries and directly call the API Gateway url in Swift, somehow? Something like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class APIGatewayManager {
static let shared = APIGatewayManager()
private let baseURL: String
private init() {
self.baseURL = "https://abc123def.execute-api.us-west-2.amazonaws.com/dev"
}
func makeAPICall<T: Codable>(path: String, method: String, body: [String: Any]? = nil) async throws -> T {
guard let url = URL(string: baseURL + path) else {
throw APIError.invalidURL
}
var request = URLRequest(url: url)
request.httpMethod = method
if let body = body {
request.httpBody = try? JSONSerialization.data(withJSONObject: body)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
}
let (data, response) = try await URLSession.shared.data(for: request)
guard let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
throw APIError.invalidResponse
}
let decoder = JSONDecoder()
return try decoder.decode(T.self, from: data)
}
}
enum APIError: Error {
case invalidURL
case invalidResponse
case decodingError
}
class ProfilesViewModel: ObservableObject {
@MainActor
func fetchProfiles() async {
do {
let profiles: [DecodableProfile] = try await APIGatewayManager.shared.makeAPICall(path: "/profiles", method: "GET")
self.profiles = profiles.map { $0.toProfile() }
self.showToastMessage("Fetched profiles successfully", isSuccess: true)
} catch {
self.showToastMessage("Error fetching profiles: (error.localizedDescription)", isSuccess: false)
}
}
@MainActor
func updateProfile(_ profile: Profile) async {
do {
let updatedProfile: DecodableProfile = try await APIGatewayManager.shared.makeAPICall(
path: "/profiles/(profile.id)",
method: "PUT",
body: profile.toDecodableProfile().dictionary
)
if let index = self.profiles.firstIndex(where: { $0.id == updatedProfile.id }) {
self.profiles[index] = updatedProfile.toProfile()
}
self.showToastMessage("Profile updated successfully", isSuccess: true)
} catch {
self.showToastMessage("Error updating profile: (error.localizedDescription)", isSuccess: false)
}
}
}
extension Encodable {
var dictionary: [String: Any]? {
guard let data = try? JSONEncoder().encode(self) else { return nil }
return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)) as? [String: Any]
}
}
</code>
<code>class APIGatewayManager { static let shared = APIGatewayManager() private let baseURL: String private init() { self.baseURL = "https://abc123def.execute-api.us-west-2.amazonaws.com/dev" } func makeAPICall<T: Codable>(path: String, method: String, body: [String: Any]? = nil) async throws -> T { guard let url = URL(string: baseURL + path) else { throw APIError.invalidURL } var request = URLRequest(url: url) request.httpMethod = method if let body = body { request.httpBody = try? JSONSerialization.data(withJSONObject: body) request.setValue("application/json", forHTTPHeaderField: "Content-Type") } let (data, response) = try await URLSession.shared.data(for: request) guard let httpResponse = response as? HTTPURLResponse, (200...299).contains(httpResponse.statusCode) else { throw APIError.invalidResponse } let decoder = JSONDecoder() return try decoder.decode(T.self, from: data) } } enum APIError: Error { case invalidURL case invalidResponse case decodingError } class ProfilesViewModel: ObservableObject { @MainActor func fetchProfiles() async { do { let profiles: [DecodableProfile] = try await APIGatewayManager.shared.makeAPICall(path: "/profiles", method: "GET") self.profiles = profiles.map { $0.toProfile() } self.showToastMessage("Fetched profiles successfully", isSuccess: true) } catch { self.showToastMessage("Error fetching profiles: (error.localizedDescription)", isSuccess: false) } } @MainActor func updateProfile(_ profile: Profile) async { do { let updatedProfile: DecodableProfile = try await APIGatewayManager.shared.makeAPICall( path: "/profiles/(profile.id)", method: "PUT", body: profile.toDecodableProfile().dictionary ) if let index = self.profiles.firstIndex(where: { $0.id == updatedProfile.id }) { self.profiles[index] = updatedProfile.toProfile() } self.showToastMessage("Profile updated successfully", isSuccess: true) } catch { self.showToastMessage("Error updating profile: (error.localizedDescription)", isSuccess: false) } } } extension Encodable { var dictionary: [String: Any]? { guard let data = try? JSONEncoder().encode(self) else { return nil } return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)) as? [String: Any] } } </code>
class APIGatewayManager {
    static let shared = APIGatewayManager()
    
    private let baseURL: String
    
    private init() {
        self.baseURL = "https://abc123def.execute-api.us-west-2.amazonaws.com/dev"
    }
    
    func makeAPICall<T: Codable>(path: String, method: String, body: [String: Any]? = nil) async throws -> T {
        guard let url = URL(string: baseURL + path) else {
            throw APIError.invalidURL
        }
        
        var request = URLRequest(url: url)
        request.httpMethod = method
        
        if let body = body {
            request.httpBody = try? JSONSerialization.data(withJSONObject: body)
            request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        }
        
        let (data, response) = try await URLSession.shared.data(for: request)
        
        guard let httpResponse = response as? HTTPURLResponse,
              (200...299).contains(httpResponse.statusCode) else {
            throw APIError.invalidResponse
        }
        
        let decoder = JSONDecoder()
        return try decoder.decode(T.self, from: data)
    }
}

enum APIError: Error {
    case invalidURL
    case invalidResponse
    case decodingError
}

class ProfilesViewModel: ObservableObject {
    
    @MainActor
    func fetchProfiles() async {
        do {
            let profiles: [DecodableProfile] = try await APIGatewayManager.shared.makeAPICall(path: "/profiles", method: "GET")
            self.profiles = profiles.map { $0.toProfile() }
            self.showToastMessage("Fetched profiles successfully", isSuccess: true)
        } catch {
            self.showToastMessage("Error fetching profiles: (error.localizedDescription)", isSuccess: false)
        }
    }
    
    @MainActor
    func updateProfile(_ profile: Profile) async {
        do {
            let updatedProfile: DecodableProfile = try await APIGatewayManager.shared.makeAPICall(
                path: "/profiles/(profile.id)",
                method: "PUT",
                body: profile.toDecodableProfile().dictionary
            )
            if let index = self.profiles.firstIndex(where: { $0.id == updatedProfile.id }) {
                self.profiles[index] = updatedProfile.toProfile()
            }
            self.showToastMessage("Profile updated successfully", isSuccess: true)
        } catch {
            self.showToastMessage("Error updating profile: (error.localizedDescription)", isSuccess: false)
        }
    }
}

extension Encodable {
    var dictionary: [String: Any]? {
        guard let data = try? JSONEncoder().encode(self) else { return nil }
        return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)) as? [String: Any]
    }
}

Tried setting up Amplify to work with existing Lambda S3 service through API Gateway/Serverless but got lost very quickly.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật