Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Last minute fixes #9

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,7 @@ public void serviceBusSessionProcessor() throws InterruptedException {
.processError(ServiceBusSessionProcessor::processError)
.buildProcessorClient();

if (processorClient.equals(null)) {
throw new RuntimeException("Sample was not successful: processorClient equals null");
}
assertNotNull(processorClient);

Log.i(PROCESSOR_TAG, "Starting the processor");
processorClient.start();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.android.appconfiguration;


import android.util.Log;

import com.azure.data.appconfiguration.ConfigurationAsyncClient;
import com.azure.data.appconfiguration.ConfigurationClientBuilder;
import com.azure.data.appconfiguration.models.ConfigurationSetting;
import com.azure.identity.ClientSecretCredential;


/**
* Sample demonstrates how to add, get, and delete a configuration setting by conditional request asynchronously.
*/
public class ConditionalRequestAsync {

private static final String TAG = "ConditionRequestAsyncOutput";

/**
* Runs the sample algorithm and demonstrates how to add, get, and delete a configuration setting by conditional
* request asynchronously
* @throws InterruptedException when a thread is waiting, sleeping, or otherwise occupied,
* and the thread is interrupted, either before or during the activity.
*/
public static void main(String endpoint, ClientSecretCredential credential) throws InterruptedException {

// Instantiate a client that will be used to call the service.
final ConfigurationAsyncClient client = new ConfigurationClientBuilder()
.credential(credential)
.endpoint(endpoint)
.buildAsyncClient();

ConfigurationSetting setting = new ConfigurationSetting().setKey("key").setLabel("label").setValue("value");

// If you want to conditionally update the setting, set `ifUnchanged` to true. If the ETag of the
// given setting matches the one in the service, then the setting is updated. Otherwise, it is
// not updated.
// If the given setting is not exist in the service, the setting will be added to the service.
client.setConfigurationSettingWithResponse(setting, true).subscribe(
result -> {
final ConfigurationSetting output = result.getValue();
final int statusCode = result.getStatusCode();
Log.i(TAG, String.format("Set config with status code: %s, Key: %s, Value: %s", statusCode, output.getKey(),
output.getValue()));
},
error -> Log.e(TAG, "There was an error while setting the setting: " + error));




// If you want to conditionally retrieve the setting, set `ifChanged` to true. If the ETag of the
// given setting matches the one in the service, then 304 status code with null value returned in the response.
// Otherwise, a setting with new ETag returned, which is the latest setting retrieved from the service.
client.getConfigurationSettingWithResponse(setting, null, true).subscribe(
result -> {
final ConfigurationSetting output = result.getValue();
final int statusCode = result.getStatusCode();
Log.i(TAG, String.format("Get config with status code: %s, Key: %s, Value: %s", statusCode, output.getKey(),
output.getValue()));
},
error -> Log.e(TAG, "There was an error while getting the setting: " + error));


// If you want to conditionally delete the setting, set `ifUnchanged` to true. If the ETag of the
// given setting matches the one in the service, then the setting is deleted. Otherwise, it is
// not deleted.
client.deleteConfigurationSettingWithResponse(setting, true).subscribe(
result -> {
final ConfigurationSetting output = result.getValue();
final int statusCode = result.getStatusCode();
Log.i(TAG, String.format("Deleted config with status code: %s, Key: %s, Value: %s", statusCode, output.getKey(),
output.getValue()));
},
error -> Log.e(TAG, "There was an error while deleting the setting: " + error));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.android.appconfiguration;

import android.util.Log;

import com.azure.data.appconfiguration.ConfigurationClient;
import com.azure.data.appconfiguration.ConfigurationClientBuilder;
import com.azure.data.appconfiguration.models.ConfigurationSetting;
import com.azure.identity.ClientSecretCredential;

/**
* Sample demonstrates how to add, get, and delete a configuration setting.
*/
public class HelloWorld {
/**
* Runs the sample algorithm and demonstrates how to add, get, and delete a configuration setting.
*/

private static final String TAG = "AppconfigHelloWorldOutput";
public static void main(String endpoint, ClientSecretCredential credential) {
final ConfigurationClient client = new ConfigurationClientBuilder()
.credential(credential)
.endpoint(endpoint)
.buildClient();

// Name of the key to add to the configuration service.
final String key = "hello";
final String value = "world";

// Sometimes the client can be set to read only from previous tests.
client.setReadOnly(key, null, false);

Log.i(TAG, "Beginning of synchronous sample...");

ConfigurationSetting setting = client.setConfigurationSetting(key, null, value);
Log.i(TAG, String.format("[SetConfigurationSetting] Key: %s, Value: %s", setting.getKey(), setting.getValue()));


setting = client.getConfigurationSetting(key, null, null);
Log.i(TAG, String.format("[GetConfigurationSetting] Key: %s, Value: %s", setting.getKey(), setting.getValue()));

setting = client.deleteConfigurationSetting(key, null);
Log.i(TAG, String.format("[DeleteConfigurationSetting] Key: %s, Value: %s", setting.getKey(), setting.getValue()));

Log.i(TAG, "End of synchronous sample.");

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.android.appconfiguration;

import android.util.Log;

import com.azure.data.appconfiguration.ConfigurationClient;
import com.azure.data.appconfiguration.ConfigurationClientBuilder;
import com.azure.data.appconfiguration.models.ConfigurationSetting;
import com.azure.identity.ClientSecretCredential;

/**
* Sample demonstrates how to add, get, and delete a configuration setting.
*/
public class ReadOnly {
/**
* Runs the sample algorithm and demonstrates how to add, get, and delete a configuration setting.
*/

private static final String TAG = "AppconfigReadOnlyOutput";

public static void main(String endpoint, ClientSecretCredential credential) {

final ConfigurationClient client = new ConfigurationClientBuilder()
.credential(credential)
.endpoint(endpoint)
.buildClient();

// Name of the key to add to the configuration service.
final String key = "hello";
final String value = "world";

final ConfigurationSetting setting = client.setConfigurationSetting(key, null, value);
// Read-Only
final ConfigurationSetting readOnlySetting = client.setReadOnly(setting.getKey(), setting.getLabel(), true);
Log.i(TAG, String.format("Setting is read-only now, Key: %s, Value: %s",
readOnlySetting.getKey(), readOnlySetting.getValue()));

// Clear Read-Only
final ConfigurationSetting clearedReadOnlySetting = client.setReadOnly(setting.getKey(), setting.getLabel(), false);
Log.i(TAG, String.format("Setting is no longer read-only, Key: %s, Value: %s",
clearedReadOnlySetting.getKey(), clearedReadOnlySetting.getValue()));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,64 @@
import android.util.Log;


import com.azure.core.http.rest.PagedIterable;
import com.azure.data.appconfiguration.ConfigurationClient;
import com.azure.data.appconfiguration.ConfigurationClientBuilder;
import com.azure.data.appconfiguration.models.ConfigurationSetting;
import com.azure.data.appconfiguration.models.SecretReferenceConfigurationSetting;

import com.azure.data.appconfiguration.models.SettingSelector;
import com.azure.identity.ClientSecretCredential;

public class SecretReferenceConfigurationSettingSample {

private static final String TAG = "SecretReferenceConfigurationSettingOutput";

public static void main(String endPoint, ClientSecretCredential credential) {

final ConfigurationClient client = new ConfigurationClientBuilder()
.credential(credential)
.endpoint(endPoint)
.buildClient();

// Name of the key to add to the configuration service.
final String key = "hello";
final String secretIdValue = "{the-keyVault-secret-id-uri}";

Log.i(TAG, "Beginning of synchronous sample...");

SecretReferenceConfigurationSetting referenceConfigurationSetting =
new SecretReferenceConfigurationSetting(key, secretIdValue);

// setConfigurationSetting adds or updates a setting to Azure App Configuration store. Alternatively, you can
// call addConfigurationSetting which only succeeds if the setting does not exist in the store. Or,
// you can call setConfigurationSetting to update a setting that is already present in the store.
Log.i(TAG, "[Set-SecretReferenceConfigurationSetting]");
SecretReferenceConfigurationSetting setting = (SecretReferenceConfigurationSetting) client.setConfigurationSetting(referenceConfigurationSetting);
printSecretReferenceConfigurationSetting(setting);

Log.i(TAG, "[Get-SecretReferenceConfigurationSetting]");
setting = (SecretReferenceConfigurationSetting) client.getConfigurationSetting(setting);
printSecretReferenceConfigurationSetting(setting);

Log.i(TAG, "[List-SecretReferenceConfigurationSetting]");
final PagedIterable<ConfigurationSetting> configurationSettings = client.listConfigurationSettings(new SettingSelector());
for (ConfigurationSetting configurationSetting : configurationSettings) {
if (configurationSetting instanceof SecretReferenceConfigurationSetting) {
Log.i(TAG, "-Listing-SecretReferenceConfigurationSetting");
printSecretReferenceConfigurationSetting((SecretReferenceConfigurationSetting) configurationSetting);
} else {
Log.i(TAG, "-Listing-non-SecretReferenceConfigurationSetting");
Log.i(TAG, String.format("Key: %s, Value: %s%n", configurationSetting.getKey(), configurationSetting.getValue()));
}
}

Log.i(TAG, "[Delete-SecretReferenceConfigurationSetting");
setting = (SecretReferenceConfigurationSetting) client.deleteConfigurationSetting(setting);
printSecretReferenceConfigurationSetting(setting);

Log.i(TAG, "End of synchronous sample.");
}

public static void printSecretReferenceConfigurationSetting(SecretReferenceConfigurationSetting setting) {
Log.i(TAG, String.format("Key: %s, Secret ID: %s, Content Type: %s, Value: %s%n", setting.getKey(),
setting.getSecretId(), setting.getContentType(), setting.getValue()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import android.util.Log;

import com.azure.data.appconfiguration.ConfigurationClient;
import com.azure.data.appconfiguration.ConfigurationClientBuilder;
import com.azure.data.appconfiguration.models.ConfigurationSetting;
import com.azure.identity.ClientSecretCredential;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -18,6 +21,55 @@ public class WatchFeature {

private static final String TAG = "WatchFeatureOutput";

public static void main(String endPoint, ClientSecretCredential credential) {

// Instantiate a client that will be used to call the service.
ConfigurationClient client = new ConfigurationClientBuilder()
.credential(credential)
.endpoint(endPoint)
.buildClient();


// Prepare a list of watching settings and update one same setting value to the service.
String prodDBConnectionKey = "prodDBConnection";
String prodDBConnectionLabel = "prodLabel";

// Assume we have a list of watching setting that stored somewhere.
List<ConfigurationSetting> watchingSettings = Arrays.asList(
client.addConfigurationSetting(prodDBConnectionKey, prodDBConnectionLabel, "prodValue"),
client.addConfigurationSetting("stageDBConnection", "stageLabel", "stageValue")
);

Log.i(TAG, "Watching settings:");
for (ConfigurationSetting setting : watchingSettings) {
Log.i(TAG, String.format("\tkey=%s, label=%s, value=%s, ETag=%s.%n",
setting.getKey(), setting.getLabel(), setting.getValue(), setting.getETag()));
}

// One of the watching settings is been updated by someone in other place.
ConfigurationSetting updatedSetting = client.setConfigurationSetting(
prodDBConnectionKey, prodDBConnectionLabel, "updatedProdValue");
Log.i(TAG, "Updated settings:");
Log.i(TAG, String.format("\tkey=%s, label=%s, value=%s, ETag=%s.%n",
updatedSetting.getKey(), updatedSetting.getLabel(), updatedSetting.getValue(), updatedSetting.getETag()));

// Updates the watching settings if needed, and only returns a list of updated settings.
List<ConfigurationSetting> refreshedSettings = refresh(client, watchingSettings);

Log.i(TAG, "Refreshed settings:");
for (ConfigurationSetting setting : refreshedSettings) {
Log.i(TAG, String.format("\tkey=%s, label=%s, value=%s, ETag=%s.%n",
setting.getKey(), setting.getLabel(), setting.getValue(), setting.getETag()));
}

// Cleaning up after ourselves by deleting the values.
Log.i(TAG, "Deleting settings:");
watchingSettings.forEach(setting -> {
client.deleteConfigurationSetting(setting.getKey(), setting.getLabel());
Log.i(TAG, String.format("\tkey: %s, value: %s.%n", setting.getKey(), setting.getValue()));
});
}

/**
* A refresh method that runs every day to update settings and returns a updated settings.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.android.servicebus;

import com.azure.identity.ClientSecretCredential;
import com.azure.messaging.servicebus.ServiceBusClientBuilder;
import com.azure.messaging.servicebus.ServiceBusReceiverAsyncClient;

import android.util.Log;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* Sample example showing how peek would work.
*/
public class PeekMessageAsync {

private static final String TAG = "ServiceBusPeekMessageAsyncOutput";


public static void main(String queueName, ClientSecretCredential credential) throws InterruptedException {
AtomicBoolean sampleSuccessful = new AtomicBoolean(false);
CountDownLatch countdownLatch = new CountDownLatch(1);

ServiceBusReceiverAsyncClient receiver = new ServiceBusClientBuilder()
.fullyQualifiedNamespace("android-service-bus.servicebus.windows.net")
.credential(credential)
.receiver()
.queueName(queueName)
.buildAsyncClient();


receiver.peekMessage().subscribe(
message -> {
Log.i(TAG, "Received Message Id: " + message.getMessageId());
Log.i(TAG, "Received Message: " + message.getBody());
},
error -> Log.e(TAG, "Error occurred while receiving message: " + error),
() -> {
Log.i(TAG, "Receiving complete.");
sampleSuccessful.set(true);
});

// Subscribe is not a blocking call so we wait here so the program does not end while finishing
// the peek operation.
countdownLatch.await(10, TimeUnit.SECONDS);

// Close the receiver.
receiver.close();


}
}
Loading