Skip to content

Instantly share code, notes, and snippets.

@rvashishth
Created October 14, 2020 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rvashishth/d74543afd28a2a11d16aa01d5b5b4572 to your computer and use it in GitHub Desktop.
Save rvashishth/d74543afd28a2a11d16aa01d5b5b4572 to your computer and use it in GitHub Desktop.
Integrate with pulsar admin api using spring webclient
import groovy.transform.CompileStatic
import groovy.transform.ToString
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.util.LinkedMultiValueMap
import org.springframework.util.MultiValueMap
import org.springframework.web.reactive.function.BodyInserters
import org.springframework.web.reactive.function.client.WebClient
import reactor.core.publisher.Mono
@CompileStatic
class TestMe2 {
static void main(String[] args) {
String token = "jwt_access_token"
new TestMe2().createNamespace(token)
}
String createNamespace(String token) {
WebClient webClient = WebClient.builder()
.baseUrl('https://domain:4443').build()
ResponseEntity<Void> response = webClient
.method(HttpMethod.PUT)
.uri("/admin/v2/namespaces/streamnative/ns5")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header(HttpHeaders.AUTHORIZATION, "Bearer $token")
.bodyValue("{}")
.retrieve()
.toBodilessEntity().block()
println response.statusCode
}
String getTopic(String token) {
WebClient webClient = WebClient.builder()
.baseUrl('https://domain:4443').build()
List<String> topics = webClient
.method(HttpMethod.GET)
.uri("/admin/v2/persistent/streamnative/ns1")
.header(HttpHeaders.AUTHORIZATION, "Bearer $token")
.retrieve()
.bodyToMono(List<String>.class).block()
println topics
}
String createPartitionedTopic(String token) {
WebClient webClient = WebClient.builder()
.baseUrl('https://domain:4443').build()
ResponseEntity<Void> response = webClient
.method(HttpMethod.PUT)
.uri("/admin/v2/persistent/streamnative/ns1/topic7/partitions")
.header(HttpHeaders.AUTHORIZATION, "Bearer $token")
.bodyValue(2)
.retrieve()
.toBodilessEntity().block()
println response.statusCode
}
String createTenant(String token) {
WebClient webClient = WebClient.builder()
.baseUrl('https://domain:4443').build()
String requestBody = "{\"allowedClusters\": [\"pulsar\"]}"
ResponseEntity<Void> response = webClient
.method(HttpMethod.PUT)
.uri("admin/v2/tenants/streamlio3")
.header(HttpHeaders.AUTHORIZATION, "Bearer $token")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.bodyValue(requestBody)
.retrieve()
.toBodilessEntity().block()
println response.statusCode
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment