Wednesday, June 26, 2024
HomeiOS Developmentios - Easy methods to submit a Multi-Part Google Type utilizing Swift...

ios – Easy methods to submit a Multi-Part Google Type utilizing Swift and Moya


The google type decides what part needs to be opened based mostly on the present reply.
I am making an attempt to programmatically submit a Google Type that’s divided into a number of sections utilizing Swift and the Moya framework.

My present implementation includes sending a POST request on to the formResponse endpoint, which works for easy varieties however fails to deal with multi-section varieties appropriately. The preliminary responses, akin to the e-mail and the reply to the primary query, are recorded efficiently, however Google doesn’t obtain every other solutions.

That is what I take advantage of to ship a request:

protocol SurveyTargetType: TargetType {
    var baseURL: URL { get }
    var sampleData: Information { get }
    var timeoutLevel: RequestTimeoutLevel { get }
}

protocol SurveyAPI {
    func createSurvey(params: [String: String]) -> AnyPublisher<Bool, MoyaError>
}

closing class DefaultSurveyAPI: SurveyAPI {
    non-public let api: APIType
    
    init(api: APIType) {
        self.api = api
    }
    
    func createSurvey(params: [String: String]) -> AnyPublisher<Bool, MoyaError> {
        let queryItems = params.map { URLQueryItem(title: $0.key, worth: $0.worth) }
        let goal = SurveyTargets.SubmitSurvey(queryItems: queryItems)

        return api.request(goal: goal)
            .map { response in
                return response.statusCode == 200
            }
            .eraseToAnyPublisher()
    }
}

extension SurveyTargetType {
    var baseURL: URL {
        guard let url = URL(string: SurveyConfiguration.baseSurveyLink) else {
            fatalError("Invalid base URL")
        }
        return url
    }

    var sampleData: Information { return Information() }
    var timeoutLevel: RequestTimeoutLevel { .default }
}

enum SurveyTargets {
    struct SubmitSurvey: SurveyTargetType {
        let queryItems: [URLQueryItem]
        
        var path: String { "" }
        var methodology: Moya.Methodology { .submit }
        
        var parameters: [String: String] {
            var param: [String: String] = [:]
            for queryItem in queryItems {
                param[queryItem.name] = queryItem.worth
            }
            return param
        }
        
        var job: Activity {
            .requestParameters(parameters: parameters, encoding: URLEncoding.default)
        }
        
        var headers: [String : String]? {
            return ["Content-Type": "application/x-www-form-urlencoded"]
        }
    }
}

The url of the shape (SurveyConfiguration.baseSurveyLink):
https://docs.google.com/varieties/d/e/FORM_ID/formResponse



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments