Skip to content
Snippets Groups Projects
RestlikeMessage.swift 948 B
Newer Older
import Foundation

public struct RestlikeMessage: Equatable {
Dariusz Rybicki's avatar
Dariusz Rybicki committed
  public init(
    version: Int,
    headers: Data,
    content: Data,
    method: Int,
    uri: String,
    error: String
  ) {
    self.version = version
    self.headers = headers
    self.content = content
    self.method = method
    self.uri = uri
    self.error = error
  }

  public var version: Int?
  public var headers: Data?
  public var content: Data?
  public var method: Int?
  public var uri: String?
  public var error: String?
}

extension RestlikeMessage: Codable {
Dariusz Rybicki's avatar
Dariusz Rybicki committed
  enum CodingKeys: String, CodingKey {
    case version = "Version"
    case headers = "Headers"
    case content = "Content"
    case method = "Method"
    case uri = "URI"
    case error = "Error"
  }
Dariusz Rybicki's avatar
Dariusz Rybicki committed
  public static func decode(_ data: Data) throws -> Self {
    try JSONDecoder().decode(Self.self, from: data)
  }

  public func encode() throws -> Data {
    try JSONEncoder().encode(self)
  }