-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from novoda/feature/rm-77-api-service
Feature [RM77] Api Service
- Loading branch information
Showing
4 changed files
with
86 additions
and
0 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
44 changes: 44 additions & 0 deletions
44
Rick-and-Morty/Rick And Morty/Services/RickAndMortyService.swift
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,44 @@ | ||
// | ||
// RickAndMortyService.swift | ||
// Rick And Morty | ||
// | ||
// Created by Scottie Gray on 2021-08-10. | ||
// Copyright © 2021 Novoda. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
final class RickAndMortyService: RickAndMortyServiceProtocol { | ||
static let baseURL = URL(string: "https://rickandmortyapi.com/api")! | ||
|
||
func fetchData<T:Decodable>(url: URL, success: @escaping (T) -> (), error: @escaping (Error?) -> ()) { | ||
let request = URLRequest(url: url) | ||
|
||
URLSession.shared.dataTask(with: request) { data, response, requestError in | ||
if requestError != nil { | ||
DispatchQueue.main.async { | ||
error(requestError) | ||
} | ||
} | ||
|
||
if let data = data { | ||
let decoder = JSONDecoder() | ||
|
||
do { | ||
let decodedData = try decoder.decode(T.self, from: data) | ||
DispatchQueue.main.async { | ||
success(decodedData) | ||
} | ||
} catch let decodingError { | ||
DispatchQueue.main.async { | ||
error(decodingError) | ||
} | ||
} | ||
} else { | ||
DispatchQueue.main.async { | ||
error(requestError) | ||
} | ||
} | ||
}.resume() | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
Rick-and-Morty/Rick And Morty/Services/RickAndMortyServiceProtocol.swift
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 @@ | ||
// | ||
// RickAndMortyServiceProtocol.swift | ||
// Rick And Morty | ||
// | ||
// Created by Scottie Gray on 2021-08-11. | ||
// Copyright © 2021 Novoda. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol RickAndMortyServiceProtocol { | ||
func fetchData<T:Decodable>(url: URL, success: @escaping (T) -> (), error: @escaping (Error?) -> ()) | ||
} |