Skip to content

Commit cfd5e3e

Browse files
Mc/embedded doc (#349)
* Added java code samples to new module * Added kotlin code samples to new module Co-authored-by: Mateusz Wiktor <[email protected]>
1 parent a625f93 commit cfd5e3e

File tree

113 files changed

+8334
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+8334
-2
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
plugins {
2+
alias(libs.plugins.benmanes.versions)
3+
id("pubnub.kotlin-library")
4+
id("pubnub.dokka")
5+
id("pubnub.shared")
6+
}
7+
8+
dependencies {
9+
api(project(":pubnub-gson:pubnub-gson-api"))
10+
implementation(project(":pubnub-gson:pubnub-gson-impl"))
11+
12+
implementation(libs.hamcrest)
13+
implementation(libs.slf4j)
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.pubnub.docs.Entities;
2+
// https://www.pubnub.com/docs/sdks/java/entities/channel#basic-usage
3+
// snippet.channelApp
4+
5+
import com.pubnub.api.PubNubException;
6+
import com.pubnub.api.java.PubNub;
7+
import com.pubnub.api.java.v2.PNConfiguration;
8+
import com.pubnub.api.UserId;
9+
import com.pubnub.api.enums.PNLogVerbosity;
10+
import com.pubnub.api.java.v2.entities.Channel;
11+
12+
public class ChannelApp {
13+
public static void main(String[] args) throws PubNubException {
14+
// Configure PubNub instance
15+
PNConfiguration.Builder configBuilder = PNConfiguration.builder(new UserId("demoUserId"), "demo");
16+
configBuilder.publishKey("demo");
17+
configBuilder.logVerbosity(PNLogVerbosity.BODY);
18+
configBuilder.secure(true);
19+
20+
PubNub pubnub = PubNub.create(configBuilder.build());
21+
22+
// Create a Channel entity
23+
Channel singleChannel = pubnub.channel("myChannel");
24+
25+
System.out.println("Channel created: " + singleChannel.getName());
26+
}
27+
}
28+
// snippet.end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.pubnub.docs.Entities;
2+
// https://www.pubnub.com/docs/sdks/java/entities/channel-group#basic-usage
3+
4+
// snippet.channelGroupApp
5+
6+
import com.pubnub.api.PubNubException;
7+
import com.pubnub.api.java.PubNub;
8+
import com.pubnub.api.java.v2.PNConfiguration;
9+
import com.pubnub.api.UserId;
10+
import com.pubnub.api.enums.PNLogVerbosity;
11+
import com.pubnub.api.java.v2.entities.ChannelGroup;
12+
13+
public class ChannelGroupApp {
14+
public static void main(String[] args) throws PubNubException {
15+
// Configure PubNub instance
16+
PNConfiguration.Builder configBuilder = PNConfiguration.builder(new UserId("demoUserId"), "demo");
17+
configBuilder.publishKey("demo");
18+
configBuilder.logVerbosity(PNLogVerbosity.BODY);
19+
configBuilder.secure(true);
20+
21+
PubNub pubnub = PubNub.create(configBuilder.build());
22+
23+
// Create a ChannelGroup entity
24+
ChannelGroup myChannelGroup = pubnub.channelGroup("myGroup");
25+
26+
System.out.println("Channel Group created: " + myChannelGroup.getName());
27+
}
28+
}
29+
// snippet.end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.pubnub.docs.Entities;
2+
// https://www.pubnub.com/docs/sdks/java/entities/channel-metadata#basic-usage
3+
4+
// snippet.channelMetadataApp
5+
import com.pubnub.api.PubNubException;
6+
import com.pubnub.api.java.PubNub;
7+
import com.pubnub.api.java.v2.PNConfiguration;
8+
import com.pubnub.api.UserId;
9+
import com.pubnub.api.enums.PNLogVerbosity;
10+
import com.pubnub.api.java.v2.entities.ChannelMetadata;
11+
12+
public class ChannelMetadataApp {
13+
public static void main(String[] args) throws PubNubException {
14+
// Configure PubNub instance
15+
PNConfiguration.Builder configBuilder = PNConfiguration.builder(new UserId("demoUserId"), "demo");
16+
configBuilder.publishKey("demo");
17+
configBuilder.logVerbosity(PNLogVerbosity.BODY);
18+
configBuilder.secure(true);
19+
20+
PubNub pubnub = PubNub.create(configBuilder.build());
21+
22+
// Create a ChannelMetadata entity
23+
ChannelMetadata metadata = pubnub.channelMetadata("myChannel");
24+
25+
System.out.println("Channel Metadata created: " + metadata.getId());
26+
}
27+
}
28+
// snippet.end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.pubnub.docs.Entities;
2+
// https://www.pubnub.com/docs/sdks/java/entities/user-metadata#basic-usage
3+
4+
// snippet.userMetadataApp
5+
import com.pubnub.api.PubNubException;
6+
import com.pubnub.api.java.PubNub;
7+
import com.pubnub.api.java.v2.PNConfiguration;
8+
import com.pubnub.api.UserId;
9+
import com.pubnub.api.enums.PNLogVerbosity;
10+
import com.pubnub.api.java.v2.entities.UserMetadata;
11+
12+
public class UserMetadataApp {
13+
public static void main(String[] args) throws PubNubException {
14+
// Configure PubNub instance
15+
PNConfiguration.Builder configBuilder = PNConfiguration.builder(new UserId("demoUserId"), "demo");
16+
configBuilder.publishKey("demo");
17+
configBuilder.logVerbosity(PNLogVerbosity.BODY);
18+
configBuilder.secure(true);
19+
20+
PubNub pubnub = PubNub.create(configBuilder.build());
21+
22+
// Create a UserMetadata entity
23+
UserMetadata userMetadata = pubnub.userMetadata("userId1");
24+
25+
System.out.println("User Metadata created for user ID: " + userMetadata.getId());
26+
}
27+
}
28+
// snippet.end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.pubnub.docs;
2+
3+
import com.pubnub.api.PubNubException;
4+
import com.pubnub.api.UserId;
5+
import com.pubnub.api.java.PubNub;
6+
import com.pubnub.api.java.v2.PNConfiguration;
7+
8+
public abstract class SnippetBase {
9+
protected PubNub createPubNub() throws PubNubException {
10+
PNConfiguration.Builder configBuilder = PNConfiguration.builder(new UserId("yourUserId"), "demo");
11+
configBuilder.publishKey("demo");
12+
13+
PNConfiguration pnConfiguration = configBuilder.build();
14+
PubNub pubnub = PubNub.create(pnConfiguration);
15+
return pubnub;
16+
}
17+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.pubnub.docs.accessManager;
2+
// https://www.pubnub.com/docs/sdks/java/api-reference/access-manager#basic-usage
3+
4+
// snippet.basicGrantToken
5+
import com.pubnub.api.PubNubException;
6+
import com.pubnub.api.UserId;
7+
import com.pubnub.api.enums.PNLogVerbosity;
8+
import com.pubnub.api.java.PubNub;
9+
import com.pubnub.api.java.models.consumer.access_manager.v3.ChannelGrant;
10+
import com.pubnub.api.java.v2.PNConfiguration;
11+
import com.pubnub.api.models.consumer.access_manager.v3.PNGrantTokenResult;
12+
13+
import java.util.Arrays;
14+
15+
public class GrantTokenApp {
16+
public static void main(String[] args) throws PubNubException {
17+
PNConfiguration.Builder configBuilder = PNConfiguration.builder(new UserId("demoUserId"), "demo");
18+
configBuilder.publishKey("demo");
19+
configBuilder.secretKey("yourSecretKey"); // <-- a VALID secretKey is required
20+
PubNub pubnub = PubNub.create(configBuilder.build());
21+
22+
pubnub.grantToken(15)
23+
.authorizedUUID("my-authorized-uuid")
24+
.channels(Arrays.asList(
25+
ChannelGrant.name("channel-a").read(),
26+
ChannelGrant.name("channel-b").read().write()))
27+
.async(result -> {
28+
result.onSuccess((PNGrantTokenResult grantTokenResult) -> {
29+
System.out.println("Token: " + grantTokenResult.getToken());
30+
}).onFailure((PubNubException exception) -> {
31+
System.out.println("Error granting token: " + exception.getMessage());
32+
});
33+
});
34+
}
35+
}
36+
// snippet.end
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package com.pubnub.docs.accessManager;
2+
3+
import com.pubnub.api.PubNubException;
4+
import com.pubnub.api.UserId;
5+
import com.pubnub.api.enums.PNLogVerbosity;
6+
import com.pubnub.api.java.PubNub;
7+
import com.pubnub.api.java.SpaceId;
8+
import com.pubnub.api.java.models.consumer.access_manager.sum.SpacePermissions;
9+
import com.pubnub.api.java.models.consumer.access_manager.sum.UserPermissions;
10+
import com.pubnub.api.java.models.consumer.access_manager.v3.ChannelGrant;
11+
import com.pubnub.api.java.models.consumer.access_manager.v3.ChannelGroupGrant;
12+
import com.pubnub.api.java.models.consumer.access_manager.v3.UUIDGrant;
13+
import com.pubnub.api.java.v2.PNConfiguration;
14+
import com.pubnub.api.models.consumer.access_manager.v3.PNGrantTokenResult;
15+
import com.pubnub.docs.SnippetBase;
16+
17+
import java.util.Arrays;
18+
import java.util.Collections;
19+
20+
public class GrantTokenOther extends SnippetBase {
21+
private void grantTokenDifferentAccessLevels() throws PubNubException {
22+
// https://www.pubnub.com/docs/sdks/java/api-reference/access-manager#grant-an-authorized-client-different-levels-of-access-to-various-resources-in-a-single-call
23+
24+
PubNub pubNub = createPubNub();
25+
26+
// snippet.grantTokenDifferentAccessLevels
27+
PNGrantTokenResult pnGrantTokenResult = pubNub.grantToken(15)
28+
.authorizedUUID("my-authorized-uuid")
29+
.channels(Arrays.asList(
30+
ChannelGrant.name("channel-a").read(),
31+
ChannelGrant.name("channel-b").read().write(),
32+
ChannelGrant.name("channel-c").read().write(),
33+
ChannelGrant.name("channel-d").read().write()))
34+
.channelGroups(Arrays.asList(
35+
ChannelGroupGrant.id("channel-group-b").read()))
36+
.uuids(Arrays.asList(
37+
UUIDGrant.id("uuid-c").get(),
38+
UUIDGrant.id("uuid-d").get().update()))
39+
.sync();
40+
41+
String token = pnGrantTokenResult.getToken();
42+
// snippet.end
43+
}
44+
45+
private void grantTokenWithRegEx() throws PubNubException {
46+
// https://www.pubnub.com/docs/sdks/java/api-reference/access-manager#grant-an-authorized-client-read-access-to-multiple-channels-using-regex
47+
48+
PubNub pubNub = createPubNub();
49+
50+
// snippet.grantTokenWithRegEx
51+
pubNub.grantToken(15)
52+
.authorizedUUID("my-authorized-uuid")
53+
.channels(Arrays.asList(
54+
ChannelGrant.pattern("^channel-[A-Za-z0-9]*$").read()))
55+
.async(result -> { /* check result */ });
56+
// snippet.end
57+
}
58+
59+
private void grantTokenDifferentAccessLevelsWithRegEx() throws PubNubException {
60+
//https://www.pubnub.com/docs/sdks/java/api-reference/access-manager#grant-an-authorized-client-different-levels-of-access-to-various-resources-and-read-access-to-spaces-using-regex-in-a-single-call
61+
62+
PubNub pubNub = createPubNub();
63+
64+
// snippet.grantTokenDifferentAccessLevelsWithRegEx
65+
pubNub.grantToken(15)
66+
.authorizedUUID("my-authorized-uuid")
67+
.channels(Arrays.asList(
68+
ChannelGrant.name("channel-a").read(),
69+
ChannelGrant.name("channel-b").read().write(),
70+
ChannelGrant.name("channel-c").read().write(),
71+
ChannelGrant.name("channel-d").read().write(),
72+
ChannelGrant.pattern("^channel-[A-Za-z0-9]*$").read()))
73+
.channelGroups(Collections.singletonList(
74+
ChannelGroupGrant.id("channel-group-b").read()))
75+
.uuids(Arrays.asList(
76+
UUIDGrant.id("uuid-c").get(),
77+
UUIDGrant.id("uuid-d").get().update()))
78+
.async(result -> { /* check result */ });
79+
// snippet.end
80+
}
81+
82+
private void grantTokenUsersAndSpacesBasic() throws PubNubException {
83+
// https://www.pubnub.com/docs/sdks/java/api-reference/access-manager#basic-usage-1
84+
85+
PubNub pubnub = createPubNub();
86+
87+
// snippet.grantTokenUsersAndSpacesBasic
88+
PNGrantTokenResult pnGrantTokenResult = pubnub.grantToken(15)
89+
.authorizedUserId(new UserId("my-authorized-userId"))
90+
.spacesPermissions(Arrays.asList(SpacePermissions.id(new SpaceId("space-id")).read()))
91+
.sync();
92+
93+
String token = pnGrantTokenResult.getToken();
94+
// snippet.end
95+
}
96+
97+
private void grantTokenUsersAndSpacesDifferentLevels() throws PubNubException {
98+
// https://www.pubnub.com/docs/sdks/java/api-reference/access-manager#grant-an-authorized-client-different-levels-of-access-to-various-resources-in-a-single-call-1
99+
100+
PubNub pubnub = createPubNub();
101+
102+
// snippet.grantTokenUsersAndSpacesDifferentLevels
103+
pubnub.grantToken(15)
104+
.authorizedUserId(new UserId("my-authorized-userId"))
105+
.spacesPermissions(Arrays.asList(
106+
SpacePermissions.id(new SpaceId("space-a")).read(),
107+
SpacePermissions.id(new SpaceId("space-b")).read().write(),
108+
SpacePermissions.id(new SpaceId("space-c")).read().write(),
109+
SpacePermissions.id(new SpaceId("space-d")).read().write()))
110+
.usersPermissions(Arrays.asList(
111+
UserPermissions.id(new UserId("userId-c")).get(),
112+
UserPermissions.id(new UserId("userId-d")).get().update()))
113+
.async(result -> { /* check result */ });
114+
// snippet.end
115+
}
116+
117+
private void grantTokenUsersAndSpacesWithRegEx() throws PubNubException {
118+
// https://www.pubnub.com/docs/sdks/java/api-reference/access-manager#grant-an-authorized-client-read-access-to-multiple-channels-using-regex-1
119+
120+
PubNub pubnub = createPubNub();
121+
122+
// snippet.grantTokenUsersAndSpacesWithRegEx
123+
pubnub.grantToken(15)
124+
.authorizedUserId(new UserId("my-authorized-userId"))
125+
.spacesPermissions(Collections.singletonList(
126+
SpacePermissions.pattern("^space-[A-Za-z0-9]*$").read()))
127+
.async(result -> { /* check result */ });
128+
// snippet.end
129+
}
130+
131+
private void grantTokenUsersAndSpacesDifferentLevelsWithRegEx() throws PubNubException {
132+
// https://www.pubnub.com/docs/sdks/java/api-reference/access-manager#grant-an-authorized-client-different-levels-of-access-to-various-resources-and-read-access-to-channels-using-regex-in-a-single-call
133+
PubNub pubnub = createPubNub();
134+
135+
// snippet.grantTokenUsersAndSpacesDifferentLevelsWithRegEx
136+
PNGrantTokenResult pnGrantTokenResult = pubnub.grantToken(15)
137+
.authorizedUserId(new UserId("my-authorized-userId"))
138+
.spacesPermissions(Arrays.asList(
139+
SpacePermissions.id(new SpaceId("space-a")).read(),
140+
SpacePermissions.id(new SpaceId("space-b")).read().write(),
141+
SpacePermissions.id(new SpaceId("space-c")).read().write(),
142+
SpacePermissions.id(new SpaceId("space-d")).read().write(),
143+
SpacePermissions.pattern("^space-[A-Za-z0-9]*$").read()))
144+
.usersPermissions(Arrays.asList(
145+
UserPermissions.id(new UserId("userId-c")).get(),
146+
UserPermissions.id(new UserId("userId-d")).get().update()))
147+
.sync();
148+
149+
String token = pnGrantTokenResult.getToken();
150+
151+
// snippet.end
152+
}
153+
}

0 commit comments

Comments
 (0)