Created
November 28, 2022 18:23
-
-
Save kevinzhow/ecbe1f686b427c89c7c57c47a2dd1910 to your computer and use it in GitHub Desktop.
Makes the alamofire can encode protobuf easily
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Alamofire | |
open class ProtobufParameterEncoder: ParameterEncoder { | |
/// Returns an encoder with default parameters. | |
public static var `default`: ProtobufParameterEncoder { ProtobufParameterEncoder() } | |
public init() {} | |
open func encode<Parameters>(_ parameters: Parameters?, | |
into request: URLRequest) throws -> URLRequest { | |
guard let parameters = parameters else { return request } | |
var request = request | |
request.httpBody = parameters as? Data | |
if request.headers["Content-Type"] == nil { | |
request.headers.update(.contentType("application/x-protobuf")) | |
} | |
return request | |
} | |
} | |
extension ParameterEncoder where Self == ProtobufParameterEncoder { | |
/// Provides a default `ProtobufParameterEncoder` instance. | |
public static var protobuf: ProtobufParameterEncoder { ProtobufParameterEncoder() } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage