Categories
developer documentation v0.0.27
mimik Developer Documentation
  • Tutorials
  • Working with edge microservices in an iOS project

Working with edge microservices in an iOS project

Objective

This tutorial demonstrates how to use the mimik Client Library when working with edge microservices in an iOS project.

Intended Readers

iOS developers who want to learn the basics of using the mimik Client Library to deploy, call, and manage edge microservices.

What You'll Learn

  • Deploying microservices
  • Referencing microservices
  • Calling microservices
  • Updating microservices
  • Undeploying microservices

Prerequisites

NOTE:

This tutorial does not work with the iOS Simulator. See the Simulator tutorial for details.

Deploying an Edge Microservice

Once the Client Library is integrated, the runtime started, and an Access Token generated, you can deploy an edge microservice.

Think of deployment as spinning up a small server inside your app.

1: // Pass the Access Token from the previous tutorial
2: func deployMicroservice(accessToken: String) async throws -> ClientLibrary.Microservice {
3:
4: guard let imageTarPath = Bundle.main.path(forResource: "randomnumber_v1", ofType: "tar") else {
5: throw NSError(domain: "Error", code: 404)
6: }
7:
8: let config = ClientLibrary.Microservice.Config(
9: imageName: "randomnumber-v1",
10: containerName: "randomnumber-v1",
11: basePath: "/randomnumber/v1",
12: envVariables: ["some-key": "some-value"]
13: )
14:
15: let microservice = try await ClientLibrary.deployMicroservice(
16: accessToken: accessToken,
17: config: config,
18: imageTarPath: imageTarPath
19: )
20:
21: print("✔️ Deployed:", microservice); return microservice
22: }

Listing 1: Deploying an edge microservice

  • imageTarPath: file path to the .tar archive in your app bundle.
  • config: describes deployment parameters.
  • edgeEngineAccessToken: the token from the previous tutorial.

If successful, you get a reference to the deployed microservice.

NOTE:
A microservice’s lifecycle is tied to the runtime—it starts and stops with it.

Referencing an Edge Microservice

After deployment, you’ll need a reference to the microservice in order to call its endpoints.

1: func deployedMicroservice(accessToken: String) async throws -> ClientLibrary.Microservice {
2:
3: let microservice = try await ClientLibrary.microservice(
4: containerName: "randomnumber-v1",
5: accessToken: accessToken
6: )
7:
8: guard let endpointUrl = microservice.urlComponents(withEndpoint: "/randomNumber")?.url else {
9: throw NSError(domain: "Microservice Error", code: 500)
10: }
11:
12: print("✔️ Endpoint URL:", endpointUrl)
13: return microservice
14: }

Listing 2: Referencing a deployed microservice

  • containerName: the microservice container name, e.g. randomnumber-v1.
  • edgeEngineAccessToken: the access token.

Calling an Edge Microservice

Once you have the endpoint URL, you can call it.

1: import Alamofire
2:
3: func callMicroservice(accessToken: String) async throws -> Int {
4:
5: let microservice = try await ClientLibrary.microservice(
6: containerName: "randomnumber-v1",
7: accessToken: accessToken
8: )
9:
10: guard let endpointUrl = microservice.urlComponents(withEndpoint: "/randomNumber")?.url else {
11: throw NSError(domain: "Microservice Error", code: 500)
12: }
13:
14: let value = try await AF.request(endpointUrl)
15: .serializingDecodable(Int.self)
16: .value
17:
18: print("✔️ Response:", value)
19: return value
20: }

Listing 3: Calling an edge microservice

The request uses Alamofire, which is included with the mimik Client Library dependencies.


Updating an Edge Microservice

Update a deployed microservice safely using the updateMicroserviceEnv method.

1: func updateMicroservice(accessToken: String) async throws -> ClientLibrary.Microservice {
2:
3: let microservice = try await ClientLibrary.microservice(
4: containerName: "randomnumber-v1",
5: accessToken: accessToken
6: )
7:
8: let updated = try await microservice.updateEnv(
9: accessToken: accessToken,
10: envVariables: ["some-new-key": "some-new-value"]
11: )
12:
13: print("✔️ Updated:", updated)
14: return updated
15: }

Listing 4: Updating an edge microservice

Always provide a complete set of environment variables each time you update.


Undeploying an Edge Microservice

To remove a microservice:

1: func undeployMicroservice(accessToken: String) async throws {
2:
3: let microservice = try await ClientLibrary.microservice(
4: containerName: "randomnumber-v1",
5: accessToken: accessToken
6: )
7:
8: try await ClientLibrary.undeployMicroservice(
9: accessToken: accessToken,
10: microservice: microservice
11: )
12:
13: print("✔️ Undeployed")
14: }

Listing 5: Undeploying an edge microservice

Once undeployed, the microservice is no longer functional.


Example Project

A complete Xcode project demonstrating all tutorials is available here:
GitHub: iOS devdocs example project

Run it on a real iOS device to see all features in action.


Additional Reading

Was this article helpful?

© mimik technology, Inc. all rights reserved