Is there any way to log my request and response in Moya 14 without using verbose?
container.register(NetworkLoggerPlugin.self) { r in
NetworkLoggerPlugin(verbose: true)
}.inObjectScope(.container)
Thank you in advance.
The initial guidance has been given elsewhere to create a custom plugin for Moya, but here's a working example of a verbose plugin that will display both request and response data.
Add the following code to wherever you are calling Moya from:
struct VerbosePlugin: PluginType {
let verbose: Bool
func prepare(_ request: URLRequest, target: TargetType) -> URLRequest {
#if DEBUG
if let body = request.httpBody,
let str = String(data: body, encoding: .utf8) {
if verbose {
print("request to send: \(str))")
}
}
#endif
return request
}
func didReceive(_ result: Result<Response, MoyaError>, target: TargetType) {
#if DEBUG
switch result {
case .success(let body):
if verbose {
print("Response:")
if let json = try? JSONSerialization.jsonObject(with: body.data, options: .mutableContainers) {
print(json)
} else {
let response = String(data: body.data, encoding: .utf8)!
print(response)
}
}
case .failure( _):
break
}
#endif
}
}
In your set up, add the new plugin:
let APIManager = MoyaProvider<API>( plugins: [
VerbosePlugin(verbose: true)
])
This will output both the request being made and the response returned. If the response is JSON encoded, it will pretty-print the JSON, otherwise it will attempt to print out the raw response data.
MoyaProvider(plugins: [NetworkLoggerPlugin()])
Related
In Chrome browser, I input this address
https://jsonplaceholder.typicode.com/todos/1
Then I see output on Chrome window:
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
Now my question is that, can i do this in Swift?
Accessing an http address then get the output?
Now on this webpage, i find the below code.
import SwiftUI
// Create URL
let url = URL(string: "https://jsonplaceholder.typicode.com/todos/1")
guard let requestUrl = url else { fatalError() }
// Create URL Request
var request = URLRequest(url: requestUrl)
// Specify HTTP Method to use
request.httpMethod = "GET"
// Send HTTP Request
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
// Check if Error took place
if let error = error {
print("Error took place \(error)")
return
}
// Read HTTP Response Status code
if let response = response as? HTTPURLResponse {
print("Response HTTP Status code: \(response.statusCode)")
}
// Convert HTTP Response Data to a simple String
if let data = data, let dataString = String(data: data, encoding: .utf8) {
print("Response data string:\n \(dataString)")
}
}
task.resume()
I run this code in Playground in XCode. As it says on the webpage I got the right output:
Response data string:
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
But it prints on the console of XCode. I cannot find a way to pass this output to a variable. So I cannot go further.
I tried add lines like:
return dataString
all got error.
Or put it in a Func(), then call the function, also got error.
I am not a programmer and very new to Swift, hope people here can help.
You need to decode from JSON and store the result in an object.
Create an object that conforms to Codable and can store the result - it need to have all the properties of the JSON:
struct MyObject: Codable {
let userId: Int
let id: Int
let title: String
let completed: Bool
}
Create a function to decode:
func decode(data: Data) -> MyObject? {
do {
let result = try JSONDecoder().decode(MyObject.self, from: data)
return result
} catch {
print("\n-->> Error decoding JSON: \(error), \(error)")
return nil
}
}
Use the function:
if let data = data {
// downloaded will store the result from the JSON
let downloaded = decode(data: data)
print(downloaded) // Optional(Test_Swift.MyObject(userId: 1, id: 1, title: "delectus aut autem", completed: false))
print(downloaded?.title ?? "Could not decode") // delectus aut autem
}
I'm working on a project where I need to upload files to channels in a slack workspace using slack's API. This is how I'm reading the file contents:
func readFile(localFilePath: String) -> Data? {
var contents: Data
let url = URL(fileURLWithPath: localFilePath)
do {
contents = try Data(contentsOf: url)
} catch {
return nil
}
return contents
}
However, this is what I get on slack:
I have also tried reading the file as a String, like this:
contents = try String(contentsOf: url, encoding: .ascii)
This way slack appears to recognize it's a PDF, but it's blank (see image below).
If I add an explicit Content-Type header and set it to multipart/form-data;, I get an invalid_form_data error in response.
Here's the request code:
let reqHeaders: HTTPHeaders = [
"Authorization": "",
"Accept": "application/json",
"Content-Type": "multipart/form-data;",
]
let params: [String:Any] = [
"channels": channelSelections.joined(separator: ","),
"content": fileData,
"initial_comment": messageText,
"filename": filename
]
AF.request(url, method: .post, parameters: params, headers: reqHeaders).responseJSON { response in
switch response.result {
case .success:
if let data = response.data {
do {
let json = try JSON(data: data)
if json["ok"].boolValue {
// some code
} else {
print("\n\n\n\(json)\n\n\n")
}
}
catch{
print("JSON Error")
}
}
case let .failure(error):
print(error)
}
}
What's the correct way to read the file and send as payload to slack's API?
I'm trying to migrate my project from Alamofire 4.9 to 5.3 and I'm having a hard time with error handling. I would like to use Decodable as much as possible, but my API endpoints return one JSON structure when everything goes well, and a different JSON structure when there is an error, the same for all errors across all endpoints. The corresponding Codable in my code is ApiError.
I would like to create a custom response serializer that can give me a Result<T, ApiError> instead of the default Result<T, AFError>. I found this article that seems to explain the general process but the code in there does not compile.
How can I create such a custom ResponseSerializer?
I ended up making it work with the following ResponseSerializer:
struct APIError: Error, Decodable {
let message: String
let code: String
let args: [String]
}
final class TwoDecodableResponseSerializer<T: Decodable>: ResponseSerializer {
lazy var decoder: JSONDecoder = {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
return decoder
}()
private lazy var successSerializer = DecodableResponseSerializer<T>(decoder: decoder)
private lazy var errorSerializer = DecodableResponseSerializer<APIError>(decoder: decoder)
public func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Error?) throws -> Result<T, APIError> {
guard error == nil else { return .failure(APIError(message: "Unknown error", code: "unknown", args: [])) }
guard let response = response else { return .failure(APIError(message: "Empty response", code: "empty_response", args: [])) }
do {
if response.statusCode < 200 || response.statusCode >= 300 {
let result = try errorSerializer.serialize(request: request, response: response, data: data, error: nil)
return .failure(result)
} else {
let result = try successSerializer.serialize(request: request, response: response, data: data, error: nil)
return .success(result)
}
} catch(let err) {
return .failure(APIError(message: "Could not serialize body", code: "unserializable_body", args: [String(data: data!, encoding: .utf8)!, err.localizedDescription]))
}
}
}
extension DataRequest {
#discardableResult func responseTwoDecodable<T: Decodable>(queue: DispatchQueue = DispatchQueue.global(qos: .userInitiated), of t: T.Type, completionHandler: #escaping (Result<T, APIError>) -> Void) -> Self {
return response(queue: .main, responseSerializer: TwoDecodableResponseSerializer<T>()) { response in
switch response.result {
case .success(let result):
completionHandler(result)
case .failure(let error):
completionHandler(.failure(APIError(message: "Other error", code: "other", args: [error.localizedDescription])))
}
}
}
}
And with that, I can call my API like so:
AF.request(request).validate().responseTwoDecodable(of: [Item].self) { response in
switch response {
case .success(let items):
completion(.success(items))
case .failure(let error): //error is an APIError
log.error("Error while loading items: \(String(describing: error))")
completion(.failure(.couldNotLoad(underlyingError: error)))
}
}
I simply consider that any status code outside of the 200-299 range corresponds to an error.
ResponseSerializers have a single requirement. Largely you can just copy the existing serializers. For example, if you wanted to parse a CSV (with no response checking):
struct CommaDelimitedSerializer: ResponseSerializer {
func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Error?) throws -> [String] {
// Call the existing StringResponseSerializer to get many behaviors automatically.
let string = try StringResponseSerializer().serialize(request: request,
response: response,
data: data,
error: error)
return Array(string.split(separator: ","))
}
}
You can read more in Alamofire's documentation.
I work with iOS app that use Alamofire, I want to write a generic function(s) which used to send and retrieve data from server to a decodable objects, my function was as below :
func pop <T : Codable> (_ Url: inout String, _ popedList: inout [T]) {
let url = URL(string:Url)
Alamofire.request(url!, method: .post).responseJSON { response in
let result = response.data
do {
let data = try JSONDecoder().decode(popedList, from: result!)// get error here
print(data[0])
let jsonEncoder = JSONEncoder()
let jsonData = try! jsonEncoder.encode(data[0])
let jsonString = String(data: jsonData, encoding: .utf8)
print("jsonString: \(String(describing: jsonString))")
} catch let e as NSError {
print("error : \(e)")
}
}
}
and a function to send an object to server as below:
func push <T : Codable> (_ Url: inout String, _ pushObject: inout T) {
let jsonData = try! JSONEncoder().encode(pushObject)
let jsonString = String(data: jsonData, encoding: .utf8)
print("jsonString: \(String(describing: jsonString))")
let url = URL(string:Url)
Alamofire.request(url!,
method: .post,
parameters:jsonString)//it's need to creat a Dictionary instate of String
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.response { response in
// response handling code
let result = response.data
print(response.data)
}
}
I get an error in first function,
"Cannot invoke 'decode' with an argument list of type '([T], from: Data)'"
and
"Escaping closures can only capture inout parameters explicitly by value"
What is the best way to write these to function as generic?
After a few searches and trying to edit my functions I capable to rewrite my two functions in such away that I get what I need:
func pop<T: Decodable>(from: URL, decodable: T.Type, completion:#escaping (_ details: [T]) -> Void)
{
Alamofire.request(from, method: .post).responseJSON { response in
let result_ = response.data
do {
let data = try JSONDecoder().decode([T].self, from: result_!)
//let data = try JSONDecoder().decode(decodable, from: result_!)// get error here
//print(data[0])
print("data[0] : \(data[0])")
completion(data)
} catch let e as NSError {
print("error : \(e)")
}
}
}
func push <T : Codable> (_ Url: String, _ pushObject: T)
{
let jsonData = try! JSONEncoder().encode(pushObject)
let jsonString = String(data: jsonData, encoding: .utf8)
print("jsonString: \(String(describing: jsonString))")
let url = URL(string:Url)
Alamofire.request(url!,
method: .post,
parameters:convertToDictionary(text: jsonString!))//it's need to creat a Dictionary instate of String
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.response { response in
// response handling code
print(response.data!)
if let jsonData = response.data {
let jsonString = String(data: jsonData, encoding: .utf8)
print("response.data: \(String(describing: jsonString))")
}
}
}
func convertToDictionary(text: String) -> [String: Any]? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
}
}
return nil
}
For the first function, JSONDecoder.decode() wants 2 parameters:
Type to decode to: the class/struct you want it to decode to. This is not an instantiated object, just the type.
Data to decode from: the generic Data object that will be converted to the type you specified.
So, in order to be able to write your function so that it has a generic URL and result object, you would need to pass it the object type and a callback to pass the result to, since network operations are asynchronous.
func dec<T: Decodable>(from: URL, decodable: T.Type, result: (T) -> Void) {
// your Alamofire logic
let data = try JSONDecoder().decode(popedList, from: result!)
result(data)
}
You can apply the same logic to the second function.
Note that this is not the best way to take care of eventual errors, just an example of how you can handle encoding with a generic function.
JSONDecoder().decode method takes type and data parameter. Pass type not popedList.
let data = try JSONDecoder().decode([T].self, from: result!)
Inout Paramaters
Function parameters are constants by default. Trying to change the value of a function parameter from within the body of that function results in a compile-time error. This means that you can’t change the value of a parameter by mistake. If you want a function to modify a parameter’s value, and you want those changes to persist after the function call has ended, define that parameter as an in-out parameter instead.
You are not changing value of popedList in both functions, so using inout is meaningless.
I've been fighting this for over 10 hours and done my best before coming here. Many tutorials I've looked out ended up being outdated by a version or two and/or missing some key knowledge in the explanation.
I'm trying to upload a PDF to a server using a WCF Rest Service. When I debug on the WCF service, the variable named document is a null stream. I've searched the web many hours trying many different things and I've exhausted MANY attempts to get such a little thing to work! What is really annoying is that this isn't the first time someone has needed to do this and yet I haven't found an answer anywhere.
If there is a better way to post the PDF other than what I've posted, I'm open to suggestions but I don't want to use third-party frameworks.
Requirements:
- Using the WCF service is required
- I'm not saving to a file system or UNC
I need to get a valid memory stream passed to the service and I can take care of the function code from there. I've tried using a Base64DataString before this and I could get that to work either. If you wanted to provide that as an option, I'm open to it.
Please help!
Swift 4 code:
import PDFKit
class FileUpload {
static func generateBoundaryString() -> String {
return "Boundary-\(UUID().uuidString)"
}
static func dataUploadBodyWithParameters(_ parameters: [String: Any]?, filename: String, mimetype: String, dataKey: String, data: Data, boundary: String) -> Data {
var body = Data()
// encode parameters first
if parameters != nil {
for (key, value) in parameters! {
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString("\(value)\r\n")
}
}
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"\(dataKey)\"; filename=\"\(filename)\"\r\n")
body.appendString("Content-Type: \(mimetype)\r\n\r\n")
body.append(data)
body.appendString("\r\n")
body.appendString("--\(boundary)--\r\n")
print(body)
return body
}
static func uploadData(_ data: Data, toURL urlString: String, withFileKey fileKey: String, completion: ((_ success: Bool, _ result: Any?) -> Void)?) {
if let url = URL(string: urlString) {
// build request
let boundary = generateBoundaryString()
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
// build body
let body = dataUploadBodyWithParameters(nil, filename: "iOSUpload.pdf", mimetype: "application/pdf", dataKey: fileKey, data: data, boundary: boundary)
request.httpBody = body
//UIApplication.shared.isNetworkActivityIndicatorVisible = true
URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) -> Void in
if data != nil && error == nil {
do {
let result = try JSONSerialization.jsonObject(with: data!, options: [])
print(result)
DispatchQueue.main.async(execute: { completion?(true, result) })
} catch {
DispatchQueue.main.async(execute: { completion?(false, nil) })
}
} else { DispatchQueue.main.async(execute: { completion?(false, nil) }) }
//UIApplication.shared.isNetworkActivityIndicatorVisible = false
}).resume()
} else { DispatchQueue.main.async(execute: { completion?(false, nil) }) }
}
}
extension Data {
mutating func appendString(_ string: String) {
let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
append(data!)
}
}
}
WCF Relevant Code:
<ServiceContract>
Public Interface IWebService
<OperationContract>
<WebInvoke(Method:="POST", UriTemplate:="UploadFile/{fileName}")>
Function UploadFile(fileName As String, document As Stream) As String
End Interface
<ServiceBehavior(InstanceContextMode:=InstanceContextMode.Single)>
Public Class MyWebService : Implements IWebService
Public Function UploadFile(fileName As String, document As Stream) As String Implements IWebService.UploadFile
'document is null
End Function