Giter Site home page Giter Site logo

codablerpc's Introduction

CodableRPC

CodableRPC is a general purpose RPC client & server implemented in Swift that uses Codable for serialization, enabling you to write idiomatic and type-safe procedure calls.

While a general purpose RPC implementation, Reddit uses CodableRPC to support ad hoc communication between XCTest UI tests and the iOS app.

Usage

// An RPCMethod enum defines the methods that can be performed by the server.
enum ExampleRPCMethod: RPCMethod {
  typealias Result = ExampleMethodResult

  case ping
}

// The method result enum defines the responses returned by the server.
enum ExampleMethodResult: Codable {
  case pong(TimeInterval)
}

// Construct external dependencies needed by the RPC methods. These objects would likely be
// provided by your dependency injection system.
struct MethodDependencies {
  let timeProvider = TimeProvider()

  struct TimeProvider {
    var currentTime: TimeInterval {
      Date().timeIntervalSinceReferenceDate
    }
  }
}

// Extend the 'RPCMethod' method type in your server target with 'RPCMethodPerformable' to
// implement the method that performs the methods.
extension ExampleRPCMethod: RPCMethodPerformable {
  func perform(dependencyProvider: MethodDependencies) async throws -> ExampleMethodResult {
    switch self {
    case .ping:
      return .pong(dependencyProvider.timeProvider.currentTime)
    }
  }
}

// Start the server.
let server = RPCServer<ExampleRPCMethod>(dependencyProvider: MethodDependencies())
try server.start(host: "127.0.0.1", port: 1234)

// Connect the client.
let client = RPCClient<ExampleRPCMethod>()
try client.connect(host: "127.0.0.1", port: 1234)

// Perform the 'ping' method.
let result = try client.call(.ping)

switch result {
case .pong(let currentTime):
  print(currentTime)
}

The included example project demonstrates using CodableRPC to communicate between a UI test and the iOS app under test. Notice that the example project declares the ExampleRPCMethod enum in a target that is imported by both the app target and the test target, whereas the RPCMethodPerformable conformance is declared in a target that is only imported by the app target. This setup is necessary to decouple your test target from the dependencies that implement the RPC methods.

Configuration

Both RPCServer and RPCClient can be initialized with custom configurations, though the defaults should be reasonable for simple workloads. See the documentation on RPCServerConfig and RPCClientConfig for details.

codablerpc's People

Contributors

ileitch avatar areitz avatar

Stargazers

Xin Xu avatar Muhammad Hanif Sugiyanto avatar Lawrence Gimenez avatar Mattia Valzelli avatar Eli Perkins avatar Serj Agopian avatar André avatar Matteo Crespi avatar Chris avatar Jake Prickett avatar  avatar Markus Lanxinger avatar Kabir Khan avatar Devansh Mohata avatar Marc Maguire avatar An Tran avatar James Sedlacek avatar Matt Robinson avatar adamz avatar  avatar Gonzalo avatar Kotaro Suto avatar

Watchers

 avatar Jason Harvey avatar Kyle Lemons avatar Jayme Howard avatar Natalie Swan avatar Mae S avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.