Skip to content

Latest commit

 

History

History
64 lines (50 loc) · 1.93 KB

README.md

File metadata and controls

64 lines (50 loc) · 1.93 KB

GoogleCloudPubSubAPI

Getting Started

If you only need to use the Google Cloud PubSub API, then this guide will help you get started.

In your Package.swift file, make sure you have the following dependencies and targets

dependencies: [
        //...
        .package(url: "https://github.com/vapor-community/google-cloud.git", from: "1.0.0"),
    ],
    targets: [
        .target(name: "MyAppName", dependencies: [
            //...
            .product(name: "CloudPubSub", package: "google-cloud"),
        ]),
    ]

Now you can setup the configuration for any GCP API globally via Application.

In configure.swift

import CloudPubSub
 
 app.googleCloud.credentials = try GoogleCloudCredentialsConfiguration(projectId: "myprojectid-12345",
 credentialsFile: "~/path/to/service-account.json")

Next we setup the CloudlPubSub API configuration (specific to this API).

app.googleCloud.pubsub.configuration = .default()

Now we can start using the GoogleCloudPubSub API There's a handy extension on Request that you can use to get access to a pubsub client via a property named gcPubsub.

func publishMessage(_ req: Request) throws -> EventLoopFuture<[String]> {
    guard let topicId = req.parameters.get("topicId") else {
        throw Abort(.badRequest, reason:"Missing Topic ID from the request")
    }
    
    try PubSubMessage.validate(content: req)
    let message = try req.content.decode(PubSubMessage.self)
    
    return req.gcPubSub.pubSubTopic.publish(topicId: topicId,
                                            data: message.data,
                                            attributes: nil,
                                            orderingKey: nil)
        .map { response in
            return response.messageIds
    }
}

struct PubSubMessage: Content {
    let data: String
    let attributes: [String: String]?
    let orderingKey: String?
}