api-swift / Tests / IrdAPITests / Helpers / Spec.swift
155 words
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import Foundation
import Testing
@testable import IrdAPI

/// The vendored OpenAPI spec, located from this file (no second copy in the repo).
enum Spec {
    static let url: URL = {
        var u = URL(fileURLWithPath: #filePath)
        for _ in 0..<4 { u.deleteLastPathComponent() }   // Spec.swift → Helpers → IrdAPITests → Tests → repo root
        return u.appendingPathComponent("spec/openapi.json")
    }()

    static let json: JSONValue = { try! JSONValue.parse(try! Data(contentsOf: url)) }()

    static let methods: Set<String> = ["get", "post", "put", "patch", "delete"]

    /// Every `"METHOD /path"` the spec declares.
    static var operations: Set<String> {
        var ops = Set<String>()
        for (path, item) in json["paths"]?.objectValue ?? [:] {
            for (method, _) in item.objectValue ?? [:] where methods.contains(method) {
                ops.insert("\(method.uppercased()) \(path)")
            }
        }
        return ops
    }

    /// The operation object for `"METHOD /path"`.
    static func operation(_ key: String) -> JSONValue? {
        let parts = key.split(separator: " ", maxSplits: 1).map(String.init)
        guard parts.count == 2 else { return nil }
        return json["paths"]?[parts[1]]?[parts[0].lowercased()]
    }
}

static mirror of HEAD · about · clone: git clone https://ird.cod-sursa.ro/api-swift.git