1
\$\begingroup\$

I am creating a user request model to pass-params for web service call

  • toRegisterParams() is used call register web service request
  • toLoginParams() is used call register web service request

  • so my question is this correct way or I need to Optimise my code or I should make different models for the same. As most-of parameters are the same.

  • for example, if I need add param in login service there are more probability to add the same in register service param.

     struct UserResquest
    {
        var phone: String = ""
        var countryPhoneCode: String = ""
        var referralCode: String = ""
        var firstName : String = ""
        var lastName : String = ""
        var email: String = ""
        var country: String = ""
        var loginBy: String = CONSTANT.MANUAL
        var socialUniqueID: String = ""
        var password: String = ""
    
        /**
         * Instantiate the instance using the passed dictionary values to set the properties values
         */
        func toRegisterParams() -> [String:Any] {
            var dictParam: [String:Any] = [:]
            dictParam[PARAMS.PHONE] = phone
            dictParam[PARAMS.COUNTRY_PHONE_CODE] = countryPhoneCode
            dictParam[PARAMS.APP_VERSION] = (Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String)
            dictParam[PARAMS.EMAIL] = email
            dictParam[PARAMS.LAST_NAME] = firstName
            dictParam[PARAMS.FIRST_NAME] = lastName
            dictParam[PARAMS.DEVICE_TOKEN] = preferenceHelper.getDeviceToken()
            dictParam[PARAMS.DEVICE_TYPE] = CONSTANT.IOS
            dictParam[PARAMS.DEVICE_TIMEZONE] = TimeZone.current.identifier
            dictParam[PARAMS.COUNTRY] = country
            dictParam[PARAMS.LOGIN_BY] = loginBy
            if loginBy != CONSTANT.MANUAL {
                dictParam[PARAMS.SOCIAL_UNIQUE_ID] = socialUniqueID
                dictParam[PARAMS.PASSWORD] = ""
            } else {
                dictParam[PARAMS.SOCIAL_UNIQUE_ID] = ""
                dictParam[PARAMS.PASSWORD] = password
            }
            return dictParam
        }
    
        func toLoginParams() {
            var dictParam: [String:Any] = [:]
            dictParam[PARAMS.APP_VERSION] = (Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String)
            dictParam[PARAMS.EMAIL] = phone
            dictParam[PARAMS.COUNTRY_PHONE_CODE] = countryPhoneCode
            dictParam[PARAMS.DEVICE_TIMEZONE] = TimeZone.current.identifier
            dictParam[PARAMS.DEVICE_TYPE] = CONSTANT.IOS
            dictParam[PARAMS.SOCIAL_UNIQUE_ID] = ""
            dictParam[PARAMS.DEVICE_TOKEN] = preferenceHelper.getDeviceToken()
            dictParam[PARAMS.LOGIN_BY] = CONSTANT.MANUAL
            dictParam[PARAMS.PASSWORD] = password
            dictParam[PARAMS.PHONE] = phone
            return dictParam
        }
    }
    

let me explain the scenario

in my app user can login throught phone number - in first screen user inputs the mobile number and place submit button (which call api and check number exist or not)

  • if yes then password screen will display when user enter password login api call with above params

  • If number is not registered register screen will display with entered mobile number and then user can't edit number as it verified by otp. after fill form register api call with above param.

\$\endgroup\$
  • \$\begingroup\$ Both toRegisterParams() and toLoginParams() are described as “used call register web service request” – copy/paste error? \$\endgroup\$ – Martin R Sep 26 at 6:51
  • \$\begingroup\$ Is dictParam[PARAMS.EMAIL] = phone a typo? \$\endgroup\$ – Martin R Sep 26 at 7:25
  • \$\begingroup\$ yes, its a typo \$\endgroup\$ – Jaydeep Vyas Sep 26 at 11:25
  • \$\begingroup\$ toRegisterParams() used to pass register params while toLoginParams() to pass login params there is no error everything work perfect, but i want to know is it correct way or not. \$\endgroup\$ – Jaydeep Vyas Sep 26 at 11:26
  • \$\begingroup\$ Did you fill it once one-time and then call the services? When they are on different places filled then I would do 2 different structs \$\endgroup\$ – muescha Sep 27 at 1:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.