Skip to content

Conversation

@bghoward
Copy link

No description provided.

ThomasWDev added 2 commits March 9, 2021 20:29

class func httpParametersString(parameters: [String: AnyHashable]) -> String {
var parametersString = ""
let parameterKeys = Array(parameters.keys)
Copy link
Author

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 {
Copy link
Author

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)))
Copy link
Author

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 {
Copy link
Author

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 {
Copy link
Author

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?
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants