Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture and display response headers #16

Merged
merged 2 commits into from
Jul 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Capture response headers
  • Loading branch information
davedelong committed Jul 25, 2018
commit 590dfd5aa0a531486e3a96f97da4301bcfee6561
2 changes: 2 additions & 0 deletions Sources/Models/RequestModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class RequestModel: Codable {
let headers: [String: String]?
var httpBody: Data?
var code: Int
var responseHeaders: [String: String]?
var dataResponse: Data?
var errorClientDescription: String?
var duration: Double?
Expand All @@ -33,6 +34,7 @@ class RequestModel: Codable {
func initResponse(response: URLResponse) {
guard let responseHttp = response as? HTTPURLResponse else {return}
code = responseHttp.statusCode
responseHeaders = responseHttp.allHeaderFields as? [String: String]
}

}
3 changes: 2 additions & 1 deletion Sources/Models/Section.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ struct Section {

enum SectionType {
case overview
case header
case requestHeader
case requestBody
case responseHeader
case responseBody
}
13 changes: 9 additions & 4 deletions Sources/UI/RequestDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ class RequestDetailViewController: WHBaseViewController {

var request: RequestModel?
var sections: [Section] = [
Section(name: "Overview", type: .overview),
Section(name: "Header", type: .header),
Section(name: "Request", type: .overview),
Section(name: "Request Header", type: .requestHeader),
Section(name: "Request Body", type: .requestBody),
Section(name: "Response Header", type: .responseHeader),
Section(name: "Response Body", type: .responseBody)
]

Expand Down Expand Up @@ -81,14 +82,18 @@ extension RequestDetailViewController: UITableViewDataSource{
let cell = tableView.dequeueReusableCell(withIdentifier: "TextTableViewCell", for: indexPath) as! TextTableViewCell
cell.textView.attributedText = RequestModelBeautifier.overview(request: req)
return cell
case .header:
case .requestHeader:
let cell = tableView.dequeueReusableCell(withIdentifier: "TextTableViewCell", for: indexPath) as! TextTableViewCell
cell.textView.attributedText = RequestModelBeautifier.header(request: req)
cell.textView.attributedText = RequestModelBeautifier.header(req.headers)
return cell
case .requestBody:
let cell = tableView.dequeueReusableCell(withIdentifier: "ActionableTableViewCell", for: indexPath) as! ActionableTableViewCell
cell.labelAction?.text = "View body"
return cell
case .responseHeader:
let cell = tableView.dequeueReusableCell(withIdentifier: "TextTableViewCell", for: indexPath) as! TextTableViewCell
cell.textView.attributedText = RequestModelBeautifier.header(req.responseHeaders)
return cell
case .responseBody:
let cell = tableView.dequeueReusableCell(withIdentifier: "ActionableTableViewCell", for: indexPath) as! ActionableTableViewCell
cell.labelAction?.text = "View body"
Expand Down
8 changes: 4 additions & 4 deletions Sources/Utils/RequestModelBeautifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ class RequestModelBeautifier: NSObject {
return final
}

static func header(request: RequestModel) -> NSMutableAttributedString{
guard request.headers != nil else {
static func header(_ headers: [String: String]?) -> NSMutableAttributedString{
guard let headerDictionary = headers else {
return NSMutableAttributedString(string: "-")
}
let final = NSMutableAttributedString()
for key in request.headers!.keys {
final.append(NSMutableAttributedString().bold(key).normal(" " + (request.headers![key] ?? "-") + "\n"))
for (key, value) in headerDictionary {
final.append(NSMutableAttributedString().bold(key).normal(" " + value + "\n"))
}
return final
}
Expand Down