Integration with gRPC
  • 31 Jul 2023
  • Dark
    Light

Integration with gRPC

  • Dark
    Light

Article Summary

v3.0

The SDK can be integrated into gRPC projects.

Start the SDK

The automatic interceptor is not supported in gRPC, so you should disable requests interception by setting the PXPolicy/urlRequestInterceptionType to PXPolicyUrlRequestInterceptionType/none.

policy.urlRequestInterceptionType = .none
policy.urlRequestInterceptionType = PXPolicyUrlRequestInterceptionTypeNone
Read about manual integration

You should read how to work with manual integration in your app.

Adding SDK's HTTP headers to your gRPC requests

In order to include the SDK's HTTP headers in your gRPC requests, you should add them to your request's metadata.

let headers = PerimeterX.headersForURLRequest()!
var callOption = CallOptions()
for (key, value) in headers {
	callOption.customMetadata.replaceOrAdd(name: key, value: value)
}
// send you gRPC request with `callOption`...

Now, use the call option instance when you call your send your gRPC request.

Handle block responses from your server

After receiving an error in the server's response, the gRPC library will throw an exception. pass the information to SDK with the PerimeterX/handleResponse(response:data:forAppId:callback:) function.

do {
	// send your gRPC request...
} catch {
	if let statusError = error as? GRPC.GRPCStatus, let data = statusError.message?.data(using: .utf8) {
		let response = HTTPURLResponse(url: URL(string: "https://your.server.domain")!, statusCode: 403, httpVersion: nil, headerFields: nil)!
		PerimeterX.handleResponse(response: response, data: data) { result in
			print("challenge result = \(result)")
		}
	}
}

Was this article helpful?