yoloabdo / rebootingnetwork Goto Github PK
View Code? Open in Web Editor NEWA tutorial project for the network layer with swift protocols/generics in #SwiftCairo talk
License: MIT License
A tutorial project for the network layer with swift protocols/generics in #SwiftCairo talk
License: MIT License
method : get
parameters: nil
path: ""
mainURL: URL(string: "https://api.xxx.getValue")!
why return Response status code was unacceptable: 404.
use blow code is ok
request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers).responseSwiftCairoUser { [weak self] (response) in // }
I was sending the Bearer token in headers but it wasn't working. So, I made some changes in urlRequest and now it is working.
var urlRequest: URLRequest {
var request = URLRequest(url: requestURL)
request.httpMethod = method.rawValue
headers.forEach { (header) in
print(header.name, header.value)
request.addValue(header.value, forHTTPHeaderField: header.name)
}
return request
}
Full code
import Foundation
import Alamofire
protocol URLRequestBuilder: URLRequestConvertible, APIRequestHandler {
var mainURL: URL { get }
var requestURL: URL { get }
// MARK: - Path
var path: String { get }
// MARK: - Parameters
var parameters: Parameters? { get }
var headers: HTTPHeaders { get }
// MARK: - Methods
var method: HTTPMethod { get }
var encoding: ParameterEncoding { get }
var urlRequest: URLRequest { get }
}
extension URLRequestBuilder {
var encoding: ParameterEncoding {
switch method {
case .get:
return URLEncoding.default
default:
return JSONEncoding.default
}
}
var mainURL: URL {
return URL(string: "https://www.test.com")!
}
var requestURL: URL {
return mainURL.appendingPathComponent(path)
}
var urlRequest: URLRequest {
var request = URLRequest(url: requestURL)
request.httpMethod = method.rawValue
headers.forEach { (header) in
print(header.name, header.value)
request.addValue(header.value, forHTTPHeaderField: header.name)
}
return request
}
// MARK: - URLRequestConvertible
func asURLRequest() throws -> URLRequest {
return try encoding.encode(urlRequest, with: parameters)
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.