I make network layer using Moya.I have added bearer access to Header and showing me in log "Personal access client not found. Please create one"
here is my code :
For Service API
enum Service {
case addProperty(property_type_id:Int,property_category_id:Int,
user_id:Int,property_name:String,address: String,description:
String,price:Int,city_id:Int,area_id:Int)
}
extension Service:TargetType,AccessTokenAuthorizable {
var baseURL: URL {
return URL(string: Constant.Url.baseUrl)!
}
var path: String {
switch self {
case .addProperty(_, _, _, _, _, _, _, _, _):
return "/add"
}
}
var method:Moya.Method {
switch self {
case .addProperty(_, _, _, _, _, _, _, _, _):
return .post
}
}
var sampleData: Data {
switch self {
case .addProperty(_, _, _, _, _, _, _, _, _):
return "{'phone':'page'}".data(using: .utf8)!
}
}
var task: Task {
switch self {
case .addProperty(let property_type_id, let property_category_id, let user_id, let property_name, let address, let description, let price, let city_id, let area_id):
var params = [String:Any]()
params["property_type_id"] = "\(property_type_id)"
params["property_category_id"] = "\(property_category_id)"
params["user_id"] = "\(user_id)"
params["property_name"] = "\(property_name)"
params["address"] = "\(address)"
params["price"] = "\(price)"
params["city_id"] = "\(city_id)"
params["area_id"] = "\(area_id)"
var formData = [MultipartFormData]()
for (key, value) in params {
if let imgData = value as? Data {
formData.append(MultipartFormData(provider: .data(imgData), name: key, fileName: "testImage.jpg", mimeType: "image/jpeg"))
} else {
formData.append(MultipartFormData(provider: .data("\(value)".data(using: .utf8)!), name: key))
}
}
return .uploadMultipart(formData
}
}
var headers: [String : String]? {
switch self {
case .addProperty(_, _, _, _, _, _, _, _, _):
return ["Content-Type": "multipart/form-data",
"Authorization": "Bearer+\(Constant.accesstoken.token)"]
}
}
var authorizationType: AuthorizationType? {
switch self {
case .addProperty(_, _, _, _, _, _, _, _, _):
return .bearer
}
}
}
here is network manager :
protocol Networkable {
var provider: MoyaProvider<Service> { get }
func addProperty(property_type_id:Int,property_category_id:Int,user_id:Int,property_name:String,address: String,description:String,price:Int,city_id:Int,area_id:Int,completion:@escaping (Data?)->Void)
}
class NetworkManager:Networkable {
static let shared = NetworkManager()
private init() {}
var provider = MoyaProvider<Service>()
func addProperty(property_type_id: Int, property_category_id: Int, user_id: Int, property_name: String, address: String, description: String, price: Int, city_id: Int, area_id: Int, completion: @escaping (Data?) -> Void) {
provider.request(.addProperty(property_type_id: property_type_id, property_category_id: property_category_id, user_id: user_id, property_name: property_name, address: address, description: description, price: price, city_id: city_id, area_id: area_id)) { (result) in
self.requestCompletion(result: result) { (data) in
completion(data)
}
}
}
//MARK: For The Completion
func requestCompletion(result:Result<Moya.Response, MoyaError>,completion:@escaping (Data?)->Void){
switch result {
case let .success(response):
do {
completion(response.data)
} catch let err {
print(err.localizedDescription)
completion(nil)
}
case let .failure(error):
print("error is : \(error.errorDescription)")
completion(nil)
}
}
}
Still I am getting Error "message": Personal access client not found. Please create one."
How can i add Authorization token or is there anything need to add in header .backend Php laravel.
I've stored the
OAuthToken
inUserDefaults
. Now alterprovider
withHope this works.!