-
Notifications
You must be signed in to change notification settings - Fork 0
Implementation of Code Challenge #1
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
base: master
Are you sure you want to change the base?
Conversation
- Added UI, Network, Pod file
|
|
||
| class func httpParametersString(parameters: [String: AnyHashable]) -> String { | ||
| var parametersString = "" | ||
| let parameterKeys = Array(parameters.keys) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to create a temporary array here. I think this can be simplified.
var parametersString = ""
parameters.forEach { (key, val) in
let encodedParameterKey = urlEncode(string: key)
let encodedParameterValue = urlEncode(string: "\(val)")
parametersString += "&=\(encodedParameterKey)=\(encodedParameterValue)"
}
return parametersString.dropFirst().description
| guard let parameters = parameters else { | ||
| return request | ||
| } | ||
| if request.httpMethod == RequestConfiguration.HTTPMethod.get.rawValue { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use a switch here?
guard let m = request.httpMethod, let method = RequestConfiguration.HTTPMethod.init(rawValue: m) else {
return request
}
switch method {
case .get:
var parametersString = httpParametersString(parameters: parameters)
if !parametersString.isEmpty {
parametersString = "?" + parametersString
}
if let url = request.url {
let urlStringWithParameters = url.absoluteString + parametersString
if let urlWithParameters = URL(string: urlStringWithParameters) {
request.url = urlWithParameters
}
}
case .post, .put:
let parametersString = httpParametersString(parameters: parameters)
request.httpBody = parametersString.data(using: .utf8)
default:
break
}
return request
| modelRequestCompletion?(.success(dataResponse)) | ||
| return | ||
| } catch { | ||
| modelRequestCompletion?(.failure(NetworkError.decodeModelFailure(error: error))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing a return here. If an error occurs then the modelRequestCompletion will get called twice, with the last one being a success.
| func fetchPhotos() { | ||
| self.setState(state: .loading) | ||
| photoRepository!.fetchPhotos(completion: { (result) in | ||
| DispatchQueue.main.async { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be better to put the main dispatch in the PhotoRepository
| func fetchPhotos(completion: PhotoDataSourceCompletion?) { | ||
| let requestConfiguration = RequestConfiguration(endpoint: "https://jsonplaceholder.typicode.com/photos", httpMethod: .get, parameters: nil) | ||
| networkManager.executeRequest(requestConfiguration: requestConfiguration, responseModel: [Photo].self) { result in | ||
| switch result { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move back to the main thread before calling the completion handler
| private var selectedPhoto: Photo? | ||
|
|
||
| private let networkManager = NetworkManager() | ||
| private var photoRepository : PhotoRepository? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should be able to make this non-optional
No description provided.