Skip to content

Commit ea00832

Browse files
Mc/embedded doc kotlin (#351)
* Added snippets kotlin --------- Co-authored-by: Mateusz Wiktor <[email protected]>
1 parent b665d18 commit ea00832

File tree

52 files changed

+1933
-214
lines changed

Some content is hidden

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

52 files changed

+1933
-214
lines changed

.github/CODEOWNERS

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
* @marcin-cebo @jguz-pubnub @kleewho
2-
.github/* @parfeon @marcin-cebo @jguz-pubnub @kleewho
1+
* @kleewho @marcin-cebo @jguz-pubnub @parfeon
2+
.github/* @kleewho @marcin-cebo @jguz-pubnub @parfeon

pubnub-gson/pubnub-gson-docs/src/main/java/com/pubnub/docs/Entities/ChannelApp.java renamed to pubnub-gson/pubnub-gson-docs/src/main/java/com/pubnub/docs/entities/ChannelApp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.pubnub.docs.Entities;
1+
package com.pubnub.docs.entities;
22
// https://www.pubnub.com/docs/sdks/java/entities/channel#basic-usage
33
// snippet.channelApp
44

pubnub-gson/pubnub-gson-docs/src/main/java/com/pubnub/docs/Entities/ChannelGroupApp.java renamed to pubnub-gson/pubnub-gson-docs/src/main/java/com/pubnub/docs/entities/ChannelGroupApp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.pubnub.docs.Entities;
1+
package com.pubnub.docs.entities;
22
// https://www.pubnub.com/docs/sdks/java/entities/channel-group#basic-usage
33

44
// snippet.channelGroupApp

pubnub-gson/pubnub-gson-docs/src/main/java/com/pubnub/docs/Entities/ChannelMetadataApp.java renamed to pubnub-gson/pubnub-gson-docs/src/main/java/com/pubnub/docs/entities/ChannelMetadataApp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.pubnub.docs.Entities;
1+
package com.pubnub.docs.entities;
22
// https://www.pubnub.com/docs/sdks/java/entities/channel-metadata#basic-usage
33

44
// snippet.channelMetadataApp

pubnub-gson/pubnub-gson-docs/src/main/java/com/pubnub/docs/Entities/UserMetadataApp.java renamed to pubnub-gson/pubnub-gson-docs/src/main/java/com/pubnub/docs/entities/UserMetadataApp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.pubnub.docs.Entities;
1+
package com.pubnub.docs.entities;
22
// https://www.pubnub.com/docs/sdks/java/entities/user-metadata#basic-usage
33

44
// snippet.userMetadataApp

pubnub-gson/pubnub-gson-docs/src/main/java/com/pubnub/docs/miscellaneous/MiscellaneousOthers.java

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package com.pubnub.docs.miscellaneous;
22

3+
import com.google.gson.JsonObject;
4+
import com.google.gson.JsonParser;
35
import com.pubnub.api.PubNubException;
6+
import com.pubnub.api.UserId;
47
import com.pubnub.api.crypto.CryptoModule;
5-
import com.pubnub.api.models.consumer.push.payload.PushPayloadHelper;
6-
import com.pubnub.docs.SnippetBase;
78
import com.pubnub.api.java.PubNub;
89
import com.pubnub.api.java.util.TimetokenUtil;
10+
import com.pubnub.api.java.v2.PNConfiguration;
11+
import com.pubnub.api.models.consumer.push.payload.PushPayloadHelper;
12+
import com.pubnub.docs.SnippetBase;
913

1014
import java.io.InputStream;
15+
import java.nio.charset.StandardCharsets;
1116
import java.time.Instant;
1217
import java.time.LocalDateTime;
1318
import java.util.HashMap;
@@ -180,4 +185,50 @@ private void timetokenToUnixTimeBasic() {
180185
System.out.println("PubNub timetoken: " + timetoken);
181186
// snippet.end
182187
}
188+
189+
private void createCryptoModuleBasic() throws PubNubException {
190+
// https://www.pubnub.com/docs/general/setup/data-security#encrypting-messages
191+
192+
// snippet.createCryptoModuleBasic
193+
PNConfiguration.Builder configBuilder = PNConfiguration.builder(new UserId("yourUserId"), "yourSubscribeKey");
194+
// publishKey from Admin Portal (only required if publishing)
195+
configBuilder.publishKey("PublishKey");
196+
configBuilder.cryptoModule(CryptoModule.createAesCbcCryptoModule("enigma", true));
197+
// all necessary config options
198+
PubNub pubNub = PubNub.create(configBuilder.build());
199+
// snippet.end
200+
}
201+
202+
private void encryptApnsBasic() {
203+
// https://www.pubnub.com/docs/general/setup/data-security#apns-example
204+
205+
// snippet.encryptApnsBasic
206+
JsonObject clearData = new JsonObject();
207+
clearData.addProperty("test_name", "pregnancy");
208+
clearData.addProperty("results", "positive");
209+
clearData.addProperty("notes", "You are having twins!");
210+
byte[] clearBytes = clearData.toString().getBytes(StandardCharsets.UTF_8);
211+
212+
CryptoModule aesCbcCryptoModule = CryptoModule.createAesCbcCryptoModule("myCipherKey01", true);
213+
214+
byte[] encryptedData = aesCbcCryptoModule.encrypt(clearBytes);
215+
// [80, 78, 69, 68, 1, 65, 67, 82, 72, 16, 83, 67, -54, -1, 98, -51, 91, 120, 31, 121, 100, 95, 75, 54, -95, 60, -74, 32, 26, 108, 77, 107, -47, 50, -45, -8, 86, -67, 72, 30, -106, -9, 45, -92, -111, 118, 50, -55, -48, -103, -90, -115, -70, 120, -47, -107, 41, 7, -61, 52, -37, -61, 83, -20, 34, -30, -64, 61, 104, 24, 3, 25, 41, -122, -36, 60, 98, -16, 34, 81, -41, 46, 102, 7, -97, 64, -37, -10, 124, 67, -41, 101, 35, -80, -103, -27, -26, -34, -50, -86, -2, -84, 105, 16, -84, 4]
216+
// snippet.end
217+
}
218+
219+
private void decryptingMessage() {
220+
// https://www.pubnub.com/docs/general/setup/data-security#decrypting-messages
221+
222+
// snippet.decryptingMessage
223+
// parse the received message and pass the encrypted parts to decrypt API.
224+
CryptoModule aesCbcCryptoModule = CryptoModule.createAesCbcCryptoModule("myCipherKey01", true);
225+
byte[] encryptedData = new byte[]{80, 78, 69, 68, 1, 65, 67, 82, 72, 16, 83, 67, -54, -1, 98, -51, 91, 120, 31, 121, 100, 95, 75, 54, -95, 60, -74, 32, 26, 108, 77, 107, -47, 50, -45, -8, 86, -67, 72, 30, -106, -9, 45, -92, -111, 118, 50, -55, -48, -103, -90, -115, -70, 120, -47, -107, 41, 7, -61, 52, -37, -61, 83, -20, 34, -30, -64, 61, 104, 24, 3, 25, 41, -122, -36, 60, 98, -16, 34, 81, -41, 46, 102, 7, -97, 64, -37, -10, 124, 67, -41, 101, 35, -80, -103, -27, -26, -34, -50, -86, -2, -84, 105, 16, -84, 4};
226+
227+
byte[] decryptedBytes = aesCbcCryptoModule.decrypt(encryptedData);
228+
JsonObject decryptedJson = JsonParser.parseString(new String(decryptedBytes, StandardCharsets.UTF_8)).getAsJsonObject();
229+
assert decryptedJson.get("test_name").getAsString().equals("pregnancy");
230+
assert decryptedJson.get("results").getAsString().equals("positive");
231+
assert decryptedJson.get("notes").getAsString().equals("You are having twins!");
232+
// snippet.end
233+
}
183234
}

pubnub-kotlin/pubnub-kotlin-docs/build.gradle.kts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,14 @@ dependencies {
1010
implementation(project(":pubnub-kotlin:pubnub-kotlin-impl"))
1111

1212
implementation(kotlin("test"))
13+
implementation(libs.slf4j)
14+
15+
// ↓ LOG4J 2 ↓
16+
val log4j2Version = "2.24.3"
17+
implementation("org.apache.logging.log4j:log4j-api:$log4j2Version")
18+
implementation("org.apache.logging.log4j:log4j-core:$log4j2Version")
19+
implementation("org.apache.logging.log4j:log4j-slf4j2-impl:$log4j2Version")
20+
21+
// ↓ LOGBACK ↓ If you want to use Logback instead of Log4j2, comment log4j dependencies above and uncomment the following line:
22+
// implementation("ch.qos.logback:logback-classic:1.3.15")
1323
}

pubnub-kotlin/pubnub-kotlin-docs/config/ktlint/baseline.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,16 @@
1616
<error line="139" column="31" source="standard:no-multi-spaces" />
1717
<error line="140" column="29" source="standard:no-multi-spaces" />
1818
</file>
19+
<file name="src/main/kotlin/com/pubnub/docs/fileSharing/FileSharingMain.kt">
20+
<error line="53" column="44" source="standard:no-multi-spaces" />
21+
<error line="54" column="45" source="standard:no-multi-spaces" />
22+
<error line="55" column="40" source="standard:no-multi-spaces" />
23+
<error line="56" column="30" source="standard:no-multi-spaces" />
24+
<error line="61" column="27" source="standard:no-multi-spaces" />
25+
</file>
26+
<file name="src/main/kotlin/com/pubnub/docs/messageReactions/MessageReactionsMain.kt">
27+
<error line="101" column="28" source="standard:no-multi-spaces" />
28+
<error line="102" column="32" source="standard:no-multi-spaces" />
29+
<error line="103" column="45" source="standard:no-multi-spaces" />
30+
</file>
1931
</baseline>

pubnub-kotlin/pubnub-kotlin-docs/src/main/kotlin/com/pubnub/docs/accessManager/GrantTokenOther.kt

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
package com.pubnub.docs.accessManager
22

3+
import com.pubnub.api.PubNub
34
import com.pubnub.api.SpaceId
45
import com.pubnub.api.UserId
56
import com.pubnub.api.models.consumer.access_manager.sum.SpacePermissions
67
import com.pubnub.api.models.consumer.access_manager.sum.UserPermissions
78
import com.pubnub.api.models.consumer.access_manager.v3.ChannelGrant
89
import com.pubnub.api.models.consumer.access_manager.v3.ChannelGroupGrant
910
import com.pubnub.api.models.consumer.access_manager.v3.UUIDGrant
10-
import com.pubnub.docs.SnippetBase
1111

12-
class GrantTokenOther : SnippetBase() {
13-
private fun grantTokenDifferentAccessLevels() {
12+
class GrantTokenOther {
13+
private fun grantTokenDifferentAccessLevels(pubnub: PubNub) {
1414
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/access-manager#grant-an-authorized-client-different-levels-of-access-to-various-resources-in-a-single-call
1515

16-
val pubnub = createPubNub()
17-
1816
// snippet.grantTokenDifferentAccessLevels
1917
pubnub.grantToken(
2018
ttl = 15,
@@ -42,11 +40,9 @@ class GrantTokenOther : SnippetBase() {
4240
// snippet.end
4341
}
4442

45-
private fun grantTokenWithPatternBasedChannelAccess() {
43+
private fun grantTokenWithPatternBasedChannelAccess(pubnub: PubNub) {
4644
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/access-manager#grant-an-authorized-client-read-access-to-multiple-channels-using-regex
4745

48-
val pubnub = createPubNub()
49-
5046
// snippet.grantTokenWithPatternBasedChannelAccess
5147
pubnub.grantToken(
5248
ttl = 15,
@@ -65,11 +61,9 @@ class GrantTokenOther : SnippetBase() {
6561
// snippet.end
6662
}
6763

68-
private fun grantTokenWithPatternBasedChannelGroupAccess() {
64+
private fun grantTokenWithPatternBasedChannelGroupAccess(pubnub: PubNub) {
6965
// https://www.pubnub.com/docs/sdks/kotlin/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
7066

71-
val pubnub = createPubNub()
72-
7367
// snippet.grantTokenWithPatternBasedChannelGroupAccess
7468
pubnub.grantToken(
7569
ttl = 15,
@@ -99,11 +93,9 @@ class GrantTokenOther : SnippetBase() {
9993
// snippet.end
10094
}
10195

102-
private fun grantTokenWithSpacePermission() {
96+
private fun grantTokenWithSpacePermission(pubnub: PubNub) {
10397
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/access-manager#basic-usage-1
10498

105-
val pubnub = createPubNub()
106-
10799
// snippet.grantTokenWithSpacePermission
108100
pubnub.grantToken(
109101
ttl = 15,
@@ -120,11 +112,9 @@ class GrantTokenOther : SnippetBase() {
120112
// snippet.end
121113
}
122114

123-
private fun grantTokenWithSpaceAndUserPermissionDifferentLevels() {
115+
private fun grantTokenWithSpaceAndUserPermissionDifferentLevels(pubnub: PubNub) {
124116
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/access-manager#grant-an-authorized-client-different-levels-of-access-to-various-resources-in-a-single-call-1
125117

126-
val pubnub = createPubNub()
127-
128118
// snippet.grantTokenWithSpaceAndUserPermissionDifferentLevels
129119
pubnub.grantToken(
130120
ttl = 15,
@@ -150,11 +140,9 @@ class GrantTokenOther : SnippetBase() {
150140
// snippet.end
151141
}
152142

153-
private fun grantTokenMultipleSpacesUsingRegEx() {
143+
private fun grantTokenMultipleSpacesUsingRegEx(pubnub: PubNub) {
154144
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/access-manager#grant-an-authorized-client-read-access-to-multiple-spaces-using-regex
155145

156-
val pubnub = createPubNub()
157-
158146
// snippet.grantTokenMultipleSpacesUsingRegEx
159147
pubnub.grantToken(
160148
ttl = 15,
@@ -173,11 +161,9 @@ class GrantTokenOther : SnippetBase() {
173161
// snippet.end
174162
}
175163

176-
private fun grantTokenVariousResourcesUsingRegEx() {
164+
private fun grantTokenVariousResourcesUsingRegEx(pubnub: PubNub) {
177165
// https://www.pubnub.com/docs/sdks/kotlin/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
178166

179-
val pubnub = createPubNub()
180-
181167
// snippet.grantTokenMultipleSpacesUsingRegEx
182168
pubnub.grantToken(
183169
ttl = 15,

pubnub-kotlin/pubnub-kotlin-docs/src/main/kotlin/com/pubnub/docs/appContext/ChannelMembersOthers.kt

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
package com.pubnub.docs.appContext
22

3+
import com.pubnub.api.PubNub
34
import com.pubnub.api.models.consumer.objects.PNMemberKey
45
import com.pubnub.api.models.consumer.objects.PNSortKey
56
import com.pubnub.api.models.consumer.objects.member.MemberInclude
67
import com.pubnub.api.models.consumer.objects.member.PNMember
78
import com.pubnub.api.models.consumer.objects.member.PNMemberArrayResult
8-
import com.pubnub.docs.SnippetBase
99

10-
class ChannelMembersOthers : SnippetBase() {
11-
private fun getChannelMembersBasic() {
10+
class ChannelMembersOthers {
11+
private fun getChannelMembersBasic(pubnub: PubNub) {
1212
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/objects#basic-usage-12
1313

14-
val pubnub = createPubNub()
15-
1614
// snippet.getChannelMembersBasic
1715
pubnub.getChannelMembers("myChannel")
1816
.async { result ->
@@ -25,11 +23,9 @@ class ChannelMembersOthers : SnippetBase() {
2523
// snippet.end
2624
}
2725

28-
private fun setChannelMembersBasic() {
26+
private fun setChannelMembersBasic(pubnub: PubNub) {
2927
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/objects#basic-usage-13
3028

31-
val pubnub = createPubNub()
32-
3329
// snippet.setChannelMembersBasic
3430
pubnub.setChannelMembers(
3531
channel = "myChannel",
@@ -53,11 +49,9 @@ class ChannelMembersOthers : SnippetBase() {
5349
// snippet.end
5450
}
5551

56-
private fun removeChannelMembersBasic() {
52+
private fun removeChannelMembersBasic(pubnub: PubNub) {
5753
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/objects#basic-usage-14
5854

59-
val pubnub = createPubNub()
60-
6155
// snippet.removeChannelMembersBasic
6256
pubnub.removeChannelMembers(
6357
channel = "myChannel",
@@ -73,11 +67,9 @@ class ChannelMembersOthers : SnippetBase() {
7367
// snippet.end
7468
}
7569

76-
private fun manageChannelMembersBasic() {
70+
private fun manageChannelMembersBasic(pubnub: PubNub) {
7771
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/objects#basic-usage-15
7872

79-
val pubnub = createPubNub()
80-
8173
// snippet.manageChannelMembersBasic
8274
val pnMemberArrayResult: PNMemberArrayResult = pubnub.manageChannelMembers(
8375
channel = "myChannel",

pubnub-kotlin/pubnub-kotlin-docs/src/main/kotlin/com/pubnub/docs/appContext/ChannelMembershipsOthers.kt

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package com.pubnub.docs.appContext
22

3+
import com.pubnub.api.PubNub
34
import com.pubnub.api.models.consumer.objects.PNMembershipKey
45
import com.pubnub.api.models.consumer.objects.PNSortKey
56
import com.pubnub.api.models.consumer.objects.membership.MembershipInclude
67
import com.pubnub.api.models.consumer.objects.membership.PNChannelMembership
7-
import com.pubnub.docs.SnippetBase
88

9-
class ChannelMembershipsOthers : SnippetBase() {
10-
private fun getMembershipsBasic() {
9+
class ChannelMembershipsOthers {
10+
private fun getMembershipsBasic(pubnub: PubNub) {
1111
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/objects#basic-usage-8
12-
val pubnub = createPubNub()
1312

1413
// snippet.getMembershipsBasic
1514
pubnub.getMemberships(
@@ -34,11 +33,9 @@ class ChannelMembershipsOthers : SnippetBase() {
3433
// snippet.end
3534
}
3635

37-
private fun setMembershipsBasic() {
36+
private fun setMembershipsBasic(pubnub: PubNub) {
3837
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/objects#basic-usage-9
3938

40-
val pubnub = createPubNub()
41-
4239
// snippet.setMembershipsBasic
4340
pubnub.setMemberships(
4441
channels = listOf(
@@ -69,11 +66,9 @@ class ChannelMembershipsOthers : SnippetBase() {
6966
// snippet.end
7067
}
7168

72-
private fun removeMembershipsBasic() {
69+
private fun removeMembershipsBasic(pubnub: PubNub) {
7370
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/objects#basic-usage-10
7471

75-
val pubnub = createPubNub()
76-
7772
// snippet.removeMembershipsBasic
7873
pubnub.removeMemberships(listOf("myChannel"))
7974
.async { result ->
@@ -86,11 +81,9 @@ class ChannelMembershipsOthers : SnippetBase() {
8681
// snippet.end
8782
}
8883

89-
private fun manageMembershipsBasic() {
84+
private fun manageMembershipsBasic(pubnub: PubNub) {
9085
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/objects#basic-usage-11
9186

92-
val pubnub = createPubNub()
93-
9487
// snippet.manageMembershipsBasic
9588
pubnub.manageMemberships(
9689
channelsToSet = listOf(PNChannelMembership.Partial(channelId = "myChannelToSet")),

0 commit comments

Comments
 (0)