123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696 |
- // ----------------------------------------------------------
- // <copyright file="KeyValueService.cs" company="Microsoft">
- // Copyright (c) Microsoft Corporation. All rights reserved.
- // </copyright>
- // ----------------------------------------------------------
- namespace Microsoft.Hawaii.KeyValue.Client
- {
- using System;
- using System.Security;
- /// <summary>
- /// Helper class that provides access to the KeyValue service.
- /// </summary>
- public class KeyValueService
- {
- /// <summary>
- /// Specifies the default service host name. This will be used to create the service url.
- /// </summary>
- public const string DefaultHostName = "http://api.hawaii-services.net/keyvalue/v1";
- /// <summary>
- /// Specifies a signature for the REST methods that manage KeyValue item.
- /// </summary>
- public const string KeyValueSignature = "values";
- /// <summary>
- /// Specifies a query string key of prefix.
- /// </summary>
- public const string PrefixKey = "prefix";
- /// <summary>
- /// Specifies a query string key of size.
- /// </summary>
- public const string SizeKey = "size";
- /// <summary>
- /// Specifies a query string key of continuationToken.
- /// </summary>
- public const string ContinuationTokenKey = "continuationToken";
- /// <summary>
- /// Specifies a query string key of batch operation.
- /// </summary>
- public const string GetBatchKey = "$batch";
- /// <summary>
- /// Specifies a query string key of timestamp before.
- /// </summary>
- public const string BeforeKey = "before";
- /// <summary>
- /// The name of the config file that indicates where the KVS staging service is located. Used only as a test hook.
- /// </summary>
- private static readonly string StagingConfigFileName = @"C:\AzureStagingDeploymentConfig\KeyValueStagingHostConfig.ini";
- /// <summary>
- /// The service host name. This is the private variable that is initialized on first
- /// access via the GetHostName() method. If a config file is present to point to a
- /// staging server, that host will be stored. Otherwise, the default is used.
- /// </summary>
- private static string hostName;
- /// <summary>
- /// The service scope. This is the private variable that is initialized on first
- /// access via the ServiceScope get accessor. If a config file is present to point to a
- /// staging server, that host will be stored. Otherwise, the default is used.
- /// </summary>
- private static string serviceScope;
- /// <summary>
- /// Gets the Host Name to be used when accessing the service.
- /// </summary>
- public static string HostName
- {
- get
- {
- return KeyValueService.GetHostName();
- }
- }
- /// <summary>
- /// Gets the service scope to be used when accessing the adm OAuth service.
- /// </summary>
- private static string ServiceScope
- {
- get
- {
- if (string.IsNullOrEmpty(KeyValueService.serviceScope))
- {
- KeyValueService.serviceScope = AdmAuthClientIdentity.GetServiceScope(KeyValueService.HostName);
- }
- return KeyValueService.serviceScope;
- }
- }
- /// <summary>
- /// Helper method to initiate the call that gets KeyValue items by key.
- /// </summary>
- /// <param name="hawaiiAppId">Specifies the Hawaii Application Id.</param>
- /// <param name="key">Specifies a key to search.</param>
- /// <param name="onComplete">Specifies an "on complete" delegate callback.</param>
- /// <param name="stateObject">Specifies a user-defined object.</param>
- public static void GetByKeyAsync(
- string hawaiiAppId,
- string key,
- ServiceAgent<GetByKeyResult>.OnCompleteDelegate onComplete,
- object stateObject)
- {
- if (string.IsNullOrEmpty(hawaiiAppId))
- {
- throw new ArgumentNullException("hawaiiAppId");
- }
- GetByKeyAsync(
- new GuidAuthClientIdentity(hawaiiAppId),
- key,
- onComplete,
- stateObject);
- }
- /// <summary>
- /// Helper method to initiate the call that gets KeyValue items by key.
- /// </summary>
- /// <param name="clientId">The adm client Id.</param>
- /// <param name="clientSecret">The adm client secret.</param>
- /// <param name="key">Specifies a key to search.</param>
- /// <param name="onComplete">Specifies an "on complete" delegate callback.</param>
- /// <param name="stateObject">Specifies a user-defined object.</param>
- public static void GetByKeyAsync(
- string clientId,
- string clientSecret,
- string key,
- ServiceAgent<GetByKeyResult>.OnCompleteDelegate onComplete,
- object stateObject)
- {
- if (string.IsNullOrEmpty(clientId))
- {
- throw new ArgumentNullException("clientId");
- }
- if (string.IsNullOrEmpty(clientSecret))
- {
- throw new ArgumentNullException("clientSecret");
- }
- GetByKeyAsync(
- new AdmAuthClientIdentity(clientId, clientSecret, KeyValueService.ServiceScope),
- key,
- onComplete,
- stateObject);
- }
- /// <summary>
- /// Helper method to initiate the call that gets KeyValue items by keys.
- /// </summary>
- /// <param name="hawaiiAppId">Specifies the Hawaii Application Id.</param>
- /// <param name="keys">Specifies the keys to search.</param>
- /// <param name="onComplete">Specifies an "on complete" delegate callback.</param>
- /// <param name="stateObject">Specifies a user-defined object.</param>
- public static void GetByKeysAsync(
- string hawaiiAppId,
- string[] keys,
- ServiceAgent<GetByKeysResult>.OnCompleteDelegate onComplete,
- object stateObject)
- {
- if (string.IsNullOrEmpty(hawaiiAppId))
- {
- throw new ArgumentNullException("hawaiiAppId");
- }
- GetByKeysAsync(
- new GuidAuthClientIdentity(hawaiiAppId),
- keys,
- onComplete,
- stateObject);
- }
- /// <summary>
- /// Helper method to initiate the call that gets KeyValue items by keys.
- /// </summary>
- /// <param name="clientId">The adm client Id.</param>
- /// <param name="clientSecret">The adm client secret.</param>
- /// <param name="keys">Specifies the keys to search.</param>
- /// <param name="onComplete">Specifies an "on complete" delegate callback.</param>
- /// <param name="stateObject">Specifies a user-defined object.</param>
- public static void GetByKeysAsync(
- string clientId,
- string clientSecret,
- string[] keys,
- ServiceAgent<GetByKeysResult>.OnCompleteDelegate onComplete,
- object stateObject)
- {
- if (string.IsNullOrEmpty(clientId))
- {
- throw new ArgumentNullException("clientId");
- }
- if (string.IsNullOrEmpty(clientSecret))
- {
- throw new ArgumentNullException("clientSecret");
- }
- GetByKeysAsync(
- new AdmAuthClientIdentity(clientId, clientSecret, KeyValueService.ServiceScope),
- keys,
- onComplete,
- stateObject);
- }
- /// <summary>
- /// Helper method to initiate the call that gets KeyValue items.
- /// </summary>
- /// <param name="hawaiiAppId">Specifies the Hawaii Application Id.</param>
- /// <param name="prefix">Specifies the search prefix keyword.</param>
- /// <param name="size">Specifies the size of result.</param>
- /// <param name="continuationToken">Specifies the continuation token.</param>
- /// <param name="onComplete">Specifies an "on complete" delegate callback.</param>
- /// <param name="stateObject">Specifies a user-defined object.</param>
- public static void GetAsync(
- string hawaiiAppId,
- string prefix,
- int size,
- string continuationToken,
- ServiceAgent<GetResult>.OnCompleteDelegate onComplete,
- object stateObject)
- {
- if (string.IsNullOrEmpty(hawaiiAppId))
- {
- throw new ArgumentNullException("hawaiiAppId");
- }
- GetAsync(
- new GuidAuthClientIdentity(hawaiiAppId),
- prefix,
- size,
- continuationToken,
- onComplete,
- stateObject);
- }
- /// <summary>
- /// Helper method to initiate the call that gets KeyValue items.
- /// </summary>
- /// <param name="clientId">The adm client Id.</param>
- /// <param name="clientSecret">The adm client secret.</param>
- /// <param name="prefix">Specifies the search prefix keyword.</param>
- /// <param name="size">Specifies the size of result.</param>
- /// <param name="continuationToken">Specifies the continuation token.</param>
- /// <param name="onComplete">Specifies an "on complete" delegate callback.</param>
- /// <param name="stateObject">Specifies a user-defined object.</param>
- public static void GetAsync(
- string clientId,
- string clientSecret,
- string prefix,
- int size,
- string continuationToken,
- ServiceAgent<GetResult>.OnCompleteDelegate onComplete,
- object stateObject)
- {
- if (string.IsNullOrEmpty(clientId))
- {
- throw new ArgumentNullException("clientId");
- }
- if (string.IsNullOrEmpty(clientSecret))
- {
- throw new ArgumentNullException("clientSecret");
- }
- GetAsync(
- new AdmAuthClientIdentity(clientId, clientSecret, KeyValueService.ServiceScope),
- prefix,
- size,
- continuationToken,
- onComplete,
- stateObject);
- }
- /// <summary>
- /// Helper method to initiate the call that creates KeyValue item.
- /// </summary>
- /// <param name="hawaiiAppId">Specifies the Hawaii Application Id.</param>
- /// <param name="items">Specifies the KeyValue items to create.</param>
- /// <param name="onComplete">Specifies an "on complete" delegate callback.</param>
- /// <param name="stateObject">Specifies a user-defined object.</param>
- public static void CreateAsync(
- string hawaiiAppId,
- KeyValueItem[] items,
- ServiceAgent<SetResult>.OnCompleteDelegate onComplete,
- object stateObject)
- {
- if (string.IsNullOrEmpty(hawaiiAppId))
- {
- throw new ArgumentNullException("hawaiiAppId");
- }
- CreateAsync(
- new GuidAuthClientIdentity(hawaiiAppId),
- items,
- onComplete,
- stateObject);
- }
- /// <summary>
- /// Helper method to initiate the call that creates KeyValue item.
- /// </summary>
- /// <param name="clientId">The adm client Id.</param>
- /// <param name="clientSecret">The adm client secret.</param>
- /// <param name="items">Specifies the KeyValue items to create.</param>
- /// <param name="onComplete">Specifies an "on complete" delegate callback.</param>
- /// <param name="stateObject">Specifies a user-defined object.</param>
- public static void CreateAsync(
- string clientId,
- string clientSecret,
- KeyValueItem[] items,
- ServiceAgent<SetResult>.OnCompleteDelegate onComplete,
- object stateObject)
- {
- if (string.IsNullOrEmpty(clientId))
- {
- throw new ArgumentNullException("clientId");
- }
- if (string.IsNullOrEmpty(clientSecret))
- {
- throw new ArgumentNullException("clientSecret");
- }
- CreateAsync(
- new AdmAuthClientIdentity(clientId, clientSecret, KeyValueService.ServiceScope),
- items,
- onComplete,
- stateObject);
- }
- /// <summary>
- /// Helper method to initiate the call that sets KeyValue item.
- /// </summary>
- /// <param name="hawaiiAppId">Specifies the Hawaii Application Id.</param>
- /// <param name="items">Specifies the KeyValue items to create.</param>
- /// <param name="onComplete">Specifies an "on complete" delegate callback.</param>
- /// <param name="stateObject">Specifies a user-defined object.</param>
- public static void SetAsync(
- string hawaiiAppId,
- KeyValueItem[] items,
- ServiceAgent<SetResult>.OnCompleteDelegate onComplete,
- object stateObject)
- {
- if (string.IsNullOrEmpty(hawaiiAppId))
- {
- throw new ArgumentNullException("hawaiiAppId");
- }
- SetAsync(
- new GuidAuthClientIdentity(hawaiiAppId),
- items,
- onComplete,
- stateObject);
- }
- /// <summary>
- /// Helper method to initiate the call that sets KeyValue item.
- /// </summary>
- /// <param name="clientId">The adm client Id.</param>
- /// <param name="clientSecret">The adm client secret.</param>
- /// <param name="items">Specifies the KeyValue items to create.</param>
- /// <param name="onComplete">Specifies an "on complete" delegate callback.</param>
- /// <param name="stateObject">Specifies a user-defined object.</param>
- public static void SetAsync(
- string clientId,
- string clientSecret,
- KeyValueItem[] items,
- ServiceAgent<SetResult>.OnCompleteDelegate onComplete,
- object stateObject)
- {
- if (string.IsNullOrEmpty(clientId))
- {
- throw new ArgumentNullException("clientId");
- }
- if (string.IsNullOrEmpty(clientSecret))
- {
- throw new ArgumentNullException("clientSecret");
- }
- SetAsync(
- new AdmAuthClientIdentity(clientId, clientSecret, KeyValueService.ServiceScope),
- items,
- onComplete,
- stateObject);
- }
- /// <summary>
- /// Helper method to initiate the call that deletes KeyValue items.
- /// </summary>
- /// <param name="hawaiiAppId">Specifies the Hawaii Application Id.</param>
- /// <param name="prefix">Specifies the search prefix keyword.</param>
- /// <param name="before">Specifies the timpstamp for delete operation.</param>
- /// <param name="onComplete">Specifies an "on complete" delegate callback.</param>
- /// <param name="stateObject">Specifies a user-defined object.</param>
- public static void DeleteAsync(
- string hawaiiAppId,
- string prefix,
- DateTime before,
- ServiceAgent<DeleteResult>.OnCompleteDelegate onComplete,
- object stateObject)
- {
- if (string.IsNullOrEmpty(hawaiiAppId))
- {
- throw new ArgumentNullException("hawaiiAppId");
- }
- DeleteAsync(
- new GuidAuthClientIdentity(hawaiiAppId),
- prefix,
- before,
- onComplete,
- stateObject);
- }
- /// <summary>
- /// Helper method to initiate the call that deletes KeyValue items.
- /// </summary>
- /// <param name="clientId">The adm client Id.</param>
- /// <param name="clientSecret">The adm client secret.</param>
- /// <param name="prefix">Specifies the search prefix keyword.</param>
- /// <param name="before">Specifies the timpstamp for delete operation.</param>
- /// <param name="onComplete">Specifies an "on complete" delegate callback.</param>
- /// <param name="stateObject">Specifies a user-defined object.</param>
- public static void DeleteAsync(
- string clientId,
- string clientSecret,
- string prefix,
- DateTime before,
- ServiceAgent<DeleteResult>.OnCompleteDelegate onComplete,
- object stateObject)
- {
- if (string.IsNullOrEmpty(clientId))
- {
- throw new ArgumentNullException("clientId");
- }
- if (string.IsNullOrEmpty(clientSecret))
- {
- throw new ArgumentNullException("clientSecret");
- }
- DeleteAsync(
- new AdmAuthClientIdentity(clientId, clientSecret, KeyValueService.ServiceScope),
- prefix,
- before,
- onComplete,
- stateObject);
- }
- /// <summary>
- /// Helper method to initiate the call that deletes KeyValue items by keys.
- /// </summary>
- /// <param name="hawaiiAppId">Specifies the Hawaii Application Id.</param>
- /// <param name="keys">Specifies the keys to search.</param>
- /// <param name="onComplete">Specifies an "on complete" delegate callback.</param>
- /// <param name="stateObject">Specifies a user-defined object.</param>
- public static void DeleteByKeysAsync(
- string hawaiiAppId,
- string[] keys,
- ServiceAgent<DeleteResult>.OnCompleteDelegate onComplete,
- object stateObject)
- {
- if (string.IsNullOrEmpty(hawaiiAppId))
- {
- throw new ArgumentNullException("hawaiiAppId");
- }
- DeleteByKeysAsync(
- new GuidAuthClientIdentity(hawaiiAppId),
- keys,
- onComplete,
- stateObject);
- }
- /// <summary>
- /// Helper method to initiate the call that deletes KeyValue items by keys.
- /// </summary>
- /// <param name="clientId">The adm client Id.</param>
- /// <param name="clientSecret">The adm client secret.</param>
- /// <param name="keys">Specifies the keys to search.</param>
- /// <param name="onComplete">Specifies an "on complete" delegate callback.</param>
- /// <param name="stateObject">Specifies a user-defined object.</param>
- public static void DeleteByKeysAsync(
- string clientId,
- string clientSecret,
- string[] keys,
- ServiceAgent<DeleteResult>.OnCompleteDelegate onComplete,
- object stateObject)
- {
- if (string.IsNullOrEmpty(clientId))
- {
- throw new ArgumentNullException("clientId");
- }
- if (string.IsNullOrEmpty(clientSecret))
- {
- throw new ArgumentNullException("clientSecret");
- }
- DeleteByKeysAsync(
- new AdmAuthClientIdentity(clientId, clientSecret, KeyValueService.ServiceScope),
- keys,
- onComplete,
- stateObject);
- }
- /// <summary>
- /// Helper method to initiate the call that gets KeyValue items by key.
- /// </summary>
- /// <param name="clientIdentity">The hawaii client identity.</param>
- /// <param name="key">Specifies a key to search.</param>
- /// <param name="onComplete">Specifies an "on complete" delegate callback.</param>
- /// <param name="stateObject">Specifies a user-defined object.</param>
- private static void GetByKeyAsync(
- ClientIdentity clientIdentity,
- string key,
- ServiceAgent<GetByKeyResult>.OnCompleteDelegate onComplete,
- object stateObject)
- {
- GetByKeyAgent agent = new GetByKeyAgent(
- KeyValueService.HostName,
- clientIdentity,
- key,
- stateObject);
- agent.ProcessRequest(onComplete);
- }
- /// <summary>
- /// Helper method to initiate the call that gets KeyValue items by keys.
- /// </summary>
- /// <param name="clientIdentity">The hawaii client identity.</param>
- /// <param name="keys">Specifies the keys to search.</param>
- /// <param name="onComplete">Specifies an "on complete" delegate callback.</param>
- /// <param name="stateObject">Specifies a user-defined object.</param>
- private static void GetByKeysAsync(
- ClientIdentity clientIdentity,
- string[] keys,
- ServiceAgent<GetByKeysResult>.OnCompleteDelegate onComplete,
- object stateObject)
- {
- GetByKeysAgent agent = new GetByKeysAgent(
- KeyValueService.HostName,
- clientIdentity,
- keys,
- stateObject);
- agent.ProcessRequest(onComplete);
- }
- /// <summary>
- /// Helper method to initiate the call that gets KeyValue items.
- /// </summary>
- /// <param name="clientIdentity">The hawaii client identity.</param>
- /// <param name="prefix">Specifies the search prefix keyword.</param>
- /// <param name="size">Specifies the size of result.</param>
- /// <param name="continuationToken">Specifies the continuation token.</param>
- /// <param name="onComplete">Specifies an "on complete" delegate callback.</param>
- /// <param name="stateObject">Specifies a user-defined object.</param>
- private static void GetAsync(
- ClientIdentity clientIdentity,
- string prefix,
- int size,
- string continuationToken,
- ServiceAgent<GetResult>.OnCompleteDelegate onComplete,
- object stateObject)
- {
- GetAgent agent = new GetAgent(
- KeyValueService.HostName,
- clientIdentity,
- prefix,
- size,
- continuationToken,
- stateObject);
- agent.ProcessRequest(onComplete);
- }
- /// <summary>
- /// Helper method to initiate the call that creates KeyValue item.
- /// </summary>
- /// <param name="clientIdentity">The hawaii client identity.</param>
- /// <param name="items">Specifies the KeyValue items to create.</param>
- /// <param name="onComplete">Specifies an "on complete" delegate callback.</param>
- /// <param name="stateObject">Specifies a user-defined object.</param>
- private static void CreateAsync(
- ClientIdentity clientIdentity,
- KeyValueItem[] items,
- ServiceAgent<SetResult>.OnCompleteDelegate onComplete,
- object stateObject)
- {
- CreateAgent agent = new CreateAgent(
- KeyValueService.HostName,
- clientIdentity,
- items,
- stateObject);
- agent.ProcessRequest(onComplete);
- }
- /// <summary>
- /// Helper method to initiate the call that sets KeyValue item.
- /// </summary>
- /// <param name="clientIdentity">The hawaii client identity.</param>
- /// <param name="items">Specifies the KeyValue items to create.</param>
- /// <param name="onComplete">Specifies an "on complete" delegate callback.</param>
- /// <param name="stateObject">Specifies a user-defined object.</param>
- private static void SetAsync(
- ClientIdentity clientIdentity,
- KeyValueItem[] items,
- ServiceAgent<SetResult>.OnCompleteDelegate onComplete,
- object stateObject)
- {
- SetAgent agent = new SetAgent(
- KeyValueService.HostName,
- clientIdentity,
- items,
- stateObject);
- agent.ProcessRequest(onComplete);
- }
- /// <summary>
- /// Helper method to initiate the call that deletes KeyValue items.
- /// </summary>
- /// <param name="clientIdentity">The hawaii client identity.</param>
- /// <param name="prefix">Specifies the search prefix keyword.</param>
- /// <param name="before">Specifies the timpstamp for delete operation.</param>
- /// <param name="onComplete">Specifies an "on complete" delegate callback.</param>
- /// <param name="stateObject">Specifies a user-defined object.</param>
- private static void DeleteAsync(
- ClientIdentity clientIdentity,
- string prefix,
- DateTime before,
- ServiceAgent<DeleteResult>.OnCompleteDelegate onComplete,
- object stateObject)
- {
- DeleteAgent agent = new DeleteAgent(
- KeyValueService.HostName,
- clientIdentity,
- prefix,
- before,
- stateObject);
- agent.ProcessRequest(onComplete);
- }
- /// <summary>
- /// Helper method to initiate the call that deletes KeyValue items by keys.
- /// </summary>
- /// <param name="clientIdentity">The hawaii client identity.</param>
- /// <param name="keys">Specifies the keys to search.</param>
- /// <param name="onComplete">Specifies an "on complete" delegate callback.</param>
- /// <param name="stateObject">Specifies a user-defined object.</param>
- private static void DeleteByKeysAsync(
- ClientIdentity clientIdentity,
- string[] keys,
- ServiceAgent<DeleteResult>.OnCompleteDelegate onComplete,
- object stateObject)
- {
- DeleteByKeysAgent agent = new DeleteByKeysAgent(
- KeyValueService.HostName,
- clientIdentity,
- keys,
- stateObject);
- agent.ProcessRequest(onComplete);
- }
- /// <summary>
- /// Returns the Host Name to be used when accessing the service. This will generally
- /// be the value specified in the DefaultHostName, but it can conditionally be set with
- /// the presence of a config file on first access.
- /// </summary>
- /// <returns>A string containing the host name of the service</returns>
- [SecuritySafeCritical]
- private static string GetHostName()
- {
- if (string.IsNullOrEmpty(KeyValueService.hostName))
- {
- KeyValueService.hostName = ClientLibraryUtils.LookupHostNameFromConfig(StagingConfigFileName, KeyValueService.DefaultHostName);
- }
- return KeyValueService.hostName;
- }
- }
- }
|