Wednesday, December 7, 2022
HomeiOS Developmentios - Ship Publish parameter as customized class object not working

ios – Ship Publish parameter as customized class object not working


I’ve to ship parameter in submit api, utilizing ParameterEncoding however its not working.

struct NewRelayEndpoint: Encodable {
        var listenerServiceId: String
        var controlProtocol: String
        var ipAddress: String
        var identify: String
        var username: String
        var password: String

    enum CodingKeys: String, CodingKey {
        case controlProtocol = "controlProtocol"
        case ipAddress = "ipAddress"
        case listenerServiceId = "listenerServiceId"
        case identify = "identify"
        case username = "username"
        case password = "password"
    }
    
    init(identify: String, listenerServiceId: String, controlProtocol: String, ipAddress: String,
         username: String, password: String) {
        self.controlProtocol = controlProtocol
        self.ipAddress = ipAddress
        self.listenerServiceId = listenerServiceId
        self.identify = identify
        self.username = username
        self.password = password
    }
       
    func encode(to encoder: Encoder) throws {
            var container = encoder.container(keyedBy: CodingKeys.self)
            attempt container.encode(identify, forKey: .identify)
            attempt container.encode(listenerServiceId, forKey: .listenerServiceId)
            attempt container.encode(controlProtocol, forKey: .controlProtocol)
            attempt container.encode(ipAddress, forKey: .ipAddress)
            attempt container.encode(username, forKey: .username)
            attempt container.encode(password, forKey: .password)
        }
    }

enum ApiError: Error {
    case forbidden
    case notFound
    case battle
    case internalServerError
}

enum RelayEndPointsAPI: URLRequestConvertible {
    
    case createNewEndpoint(_ endpoint: NewRelayEndpoint)
    
    //MARK: - URLRequestConvertible
    func asURLRequest() throws -> URLRequest {
        let url = attempt RelayServerURL.baseurl.stage.asURL()
        
        var urlRequest = URLRequest(url: url.appendingPathComponent(path))
         
        
        urlRequest.httpMethod = methodology.rawValue
        
        urlRequest.setValue(Constants.ContentType.json.rawValue, forHTTPHeaderField: Constants.HttpHeaderField.contentType.rawValue)

        
        swap self {
        default:
            urlRequest.setValue(getAccessToken(), forHTTPHeaderField: Constants.HttpHeaderField.accessToken.rawValue)
        }
        
       let encoding: ParameterEncoding = {
           swap methodology {
           case .get:
               return URLEncoding.default
           case .submit:
               return JSONEncoding.default
           default:
               return JSONEncoding.default
           }
       }()
        return attempt encoding.encode(urlRequest, with: parameters!)
    }
    
     func getAccessToken() -> String {
         return UserDefaults.customary.string(forKey: "AccessToken")!
     }
    
    //MARK: - HttpMethod
      personal var methodology: HTTPMethod {
          swap self {
          case .getListOfRelayEndpoints, .getListOfListenerServices :
              return .get
          case .createNewEndpoint(_):
              return .submit
          }
      }
      
      //MARK: - Path
      personal var path: String {
          swap self {
          case .createNewEndpoint(_):
              return "api/factors
          }
      }
      
      //MARK: - Parameters
      personal var parameters: Parameters? {
          swap self {
          case .createNewEndpoint(let endPoint):
              return ["endpoint": endPoint]
          }
      }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments