Latest 1.9
Homepage https://github.com/kozlek/Condulet
License MIT
Platforms ios 10.0
Authors

Condulet

Build Status
Language
License

Condulet is flexible and convenient API client construction framework built on top of URLSession and URLSessionTask. It can handle plenty of data types including multipart form data generation, sending and receiving JSON encoded Protobuf messages. Of course, you can use your custom response handlers and data parsers with Coundulet. Condulet provides most of the features needed to build robust client for your backend services. The main purpose of it — make your job done. And my job too :) 

Installation

CocoaPods

pod 'Condulet'

Add Protobufs supporting extensions:

pod 'Condulet/Protobuf'

And don’t forget to import the framework:

import Condulet

Manually

Just put the files from Core and Protobuf directories somethere in your project. To use Protobuf extensions you need additionally integrate SwiftProtobuf framework into your project.

Usage

The core class which handles network task is ServiceTask. ServiceTaskBuilder provides factory methods helping to create and configure instance of ServiceTask. If you need more control over the process of making request and handling the response, you can use delegation and implement ServiceTaskRetrofitting protocol and modify task behavior via retrofitter. Also you can subclass ServiceTask, it is built for that.

Make a GET request expecting json response

ServiceTaskBuilder()
    .url("https://host.com/path/to/endpoint")
    .method(.GET)
    .query(["param": value])
    // Expecting valid JSON response
    .json { (object, response) in
        print("JSON response received: (object)")
    }
    .error { (error, response) in
        print("Error occured: (error)")
    }
    .perform()

Sending and receiving data

Send and receive data using objects conforming to Codable protocol:


struct NameRequest: Encodable {
    let name: String
}

struct NameResponse: Decodable {
    let isValid: Bool
}

ServiceTaskBuilder()
    // Set base url and HTTP method
    .endpoint(.POST, "https://host.com")
    // Add path to resource
    .path("/path/to/resource")
    // Serialize our Codable struct and set body
    .body(codable: NameRequest("some"))
    // Expect response with the object of 'NameResponse' type
    .codable { (object: NameResponse, response) in
        print("Name valid: (object.isValid)")
    }
    // Otherwise will fail with error
    .error { (error, response) in
        print("Error occured: (error)")
    }
    .perform()

Just download some file:


ServiceTaskBuilder()
    .headers(["Authorization": "Bearer (token)"])
    .method(.PUT)
    .url("https://host.com/file/12345")
    .body(text: "123456789")
    .file { (url, response) in
        print("Downloaded: (url)")
        // Remove temp file
        try? FileManager.default.removeItem(at: url)
    }
    .error { (error, response) in
        print("Error occured: (error)")
    }
    // When download destination not provided, content will be downloaded and saved to temp file
    .download()

Upload multipart form data encoded content:


// Create new form data builder
var formDataBuilder = FormDataBuilder()

do {

    // Filename and MIME type will be obtained automatically from URL. It can be provided explicitly too
    try formDataBuilder.append(.file(name: "image", url: *url*))

    // Generate form data in memory. It also can be written directly to disk or stream using encode(to:) method 
    let formData = try formDataBuilder.encode()

    ServiceTaskBuilder()
            .endpoint(.POST, "https://host.com/upload")
            .body(data: formData, contentType: formDataBuilder.contentType)
            .response(content: { (response) in
                switch response {
                case .success:
                    print("Done!")
                case .failure(let error):
                    print("Failed to upload: (error)")
                }
            })
            .perform()
}
catch {
    print("(error))
    return
}

Send and receive Protobuf messages:


ServiceTaskBuilder()
    .endpoint(.POST, "https://host.com")
    // Create and configure request message in place
    .body { (message: inout Google_Protobuf_SourceContext) in
        message.fileName = "file.name"
    }
    // Expecting Google_Protobuf_Empty message response
    .proto{ (message: Google_Protobuf_Empty, response) in
        print("Done!")
    }
    .error { (error, response) in
        print("Error occured: (error)")
    }
    .perform()

// Or another version of the code above with explicitly provided types
ServiceTaskBuilder()
    .endpoint(.POST, "https://host.com")
    // Create and configure request message in place
    .body(proto: Google_Protobuf_SourceContext.self) { (message) in
        message.fileName = "file.name"
    }
    // Expecting Google_Protobuf_Empty message response
    .response(proto: Google_Protobuf_Empty.self) { (response) in
        switch response {
        case .success(let message):
            print("Done!")
        case .failure(let error):
            print("Error occured: (error)")
        }
    }
    .perform()

Author

Natan Zalkin [email protected]

License

Condulet is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "Condulet",
    "version": "1.9",
    "summary": "Condulet makes it simple to send requests to web services",
    "description": "Condulet is flexible and convenient API client construction framework built on top of URLSession and URLSessionTask. It can handle plenty of data types including multipart form data generation, sending and receiving JSON encoded Protobuf messages.",
    "homepage": "https://github.com/kozlek/Condulet",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Natan Zalkin": "[email protected]"
    },
    "source": {
        "git": "https://github.com/kozlek/Condulet.git",
        "tag": "1.9"
    },
    "module_name": "Condulet",
    "swift_version": "4.1",
    "platforms": {
        "ios": "10.0"
    },
    "default_subspecs": "Core",
    "subspecs": [
        {
            "name": "Core",
            "source_files": "Condulet/Core/*.swift"
        },
        {
            "name": "Protobuf",
            "dependencies": {
                "Condulet/Core": [],
                "SwiftProtobuf": []
            },
            "source_files": "Condulet/Protobuf/*.swift"
        }
    ]
}

Pin It on Pinterest

Share This