Created
September 19, 2013 06:43
-
-
Save DevJohnC/6619825 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // Author: John Carruthers (johnc@frag-labs.com) | |
| // | |
| // Copyright (C) 2013 John Carruthers | |
| // | |
| // Permission is hereby granted, free of charge, to any person obtaining | |
| // a copy of this software and associated documentation files (the | |
| // "Software"), to deal in the Software without restriction, including | |
| // without limitation the rights to use, copy, modify, merge, publish, | |
| // distribute, sublicense, and/or sell copies of the Software, and to | |
| // permit persons to whom the Software is furnished to do so, subject to | |
| // the following conditions: | |
| // | |
| // The above copyright notice and this permission notice shall be | |
| // included in all copies or substantial portions of the Software. | |
| // | |
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
| // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
| // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
| // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
| // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
| // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
| // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| // | |
| using System; | |
| using System.Threading.Tasks; | |
| using FragLabs.AdjutantOS.API.Devices; | |
| using FragLabs.AdjutantOS.API.Services; | |
| namespace FragLabs.AdjutantOS.API | |
| { | |
| /// <summary> | |
| /// Services invoker. | |
| /// </summary> | |
| public class ServiceInvoker | |
| { | |
| /// <summary> | |
| /// Call a service on the specified device with no request body. | |
| /// </summary> | |
| /// <param name="device"></param> | |
| /// <param name="uri"></param> | |
| public static void CallOneWay(IDevice device, string uri) | |
| { | |
| Call(device, uri); | |
| } | |
| /// <summary> | |
| /// Call a service on the specified device. | |
| /// </summary> | |
| /// <param name="device"></param> | |
| /// <param name="uri"></param> | |
| /// <param name="requestJson"></param> | |
| public static void CallOneWay(IDevice device, string uri, string requestJson) | |
| { | |
| Call(device, uri, requestJson); | |
| } | |
| /// <summary> | |
| /// Call a service on the specified device. | |
| /// </summary> | |
| /// <param name="device"></param> | |
| /// <param name="request"></param> | |
| public static void CallOneWay(IDevice device, IRequestDto request) | |
| { | |
| Call<object>(device, request); | |
| } | |
| /// <summary> | |
| /// Call a service on the specified device with no request body. | |
| /// </summary> | |
| /// <param name="device"></param> | |
| /// <param name="uri"></param> | |
| /// <returns></returns> | |
| public static string CallSync(IDevice device, string uri) | |
| { | |
| return Call(device, uri).Result; | |
| } | |
| /// <summary> | |
| /// Call a service on the specified device. | |
| /// </summary> | |
| /// <param name="device"></param> | |
| /// <param name="uri"></param> | |
| /// <param name="requestJson"></param> | |
| /// <returns></returns> | |
| public static string CallSync(IDevice device, string uri, string requestJson) | |
| { | |
| return Call(device, uri, requestJson).Result; | |
| } | |
| /// <summary> | |
| /// Call a service on a specified device. | |
| /// </summary> | |
| /// <typeparam name="T"></typeparam> | |
| /// <param name="device"></param> | |
| /// <param name="request"></param> | |
| /// <returns></returns> | |
| public static T CallSync<T>(IDevice device, IRequestDto request) | |
| { | |
| return Call<T>(device, request).Result; | |
| } | |
| /// <summary> | |
| /// Call a service on the specified device with no request body. | |
| /// </summary> | |
| /// <param name="device"></param> | |
| /// <param name="uri"></param> | |
| /// <returns></returns> | |
| public static Task<string> Call(IDevice device, string uri) | |
| { | |
| return Call(device, uri, "{}"); | |
| } | |
| /// <summary> | |
| /// Call a service on a specified device. | |
| /// </summary> | |
| /// <param name="device"></param> | |
| /// <param name="uri"></param> | |
| /// <param name="requestJson"></param> | |
| /// <returns></returns> | |
| public static Task<string> Call(IDevice device, string uri, string requestJson) | |
| { | |
| var tcs = new TaskCompletionSource<string>(); | |
| CallService(device as IServiceClient, uri, requestJson, tcs); | |
| return tcs.Task; | |
| } | |
| /// <summary> | |
| /// Call a service on the specified device. | |
| /// </summary> | |
| /// <typeparam name="T"></typeparam> | |
| /// <param name="device"></param> | |
| /// <param name="request"></param> | |
| /// <returns></returns> | |
| public static Task<T> Call<T>(IDevice device, IRequestDto request) | |
| { | |
| var uri = request.GetUri(); | |
| if (string.IsNullOrEmpty(uri)) | |
| throw new ArgumentException("Request URI cannot be null or empty"); | |
| var requestJson = Serializer.Serialize(request); | |
| var tcs = new TaskCompletionSource<T>(); | |
| CallService(device as IServiceClient, uri, requestJson, tcs); | |
| return tcs.Task; | |
| } | |
| /// <summary> | |
| /// Call service on client. | |
| /// </summary> | |
| /// <param name="client"></param> | |
| /// <param name="uri"></param> | |
| /// <param name="requestJson"></param> | |
| /// <returns></returns> | |
| private static void CallService(IServiceClient client, string uri, string requestJson, TaskCompletionSource<string> taskCompletionSource) | |
| { | |
| if (client == null) throw new ArgumentNullException("client"); | |
| if (uri == null) throw new ArgumentNullException("uri"); | |
| if (requestJson == null) throw new ArgumentNullException("requestJson"); | |
| if (taskCompletionSource == null) throw new ArgumentNullException("taskCompletionSource"); | |
| client.CallService(uri, requestJson, taskCompletionSource.SetResult); | |
| } | |
| /// <summary> | |
| /// Call service on client. | |
| /// </summary> | |
| /// <param name="client"></param> | |
| /// <param name="uri"></param> | |
| /// <param name="requestJson"></param> | |
| /// <returns></returns> | |
| private static void CallService<T>(IServiceClient client, string uri, string requestJson, TaskCompletionSource<T> taskCompletionSource) | |
| { | |
| if (client == null) throw new ArgumentNullException("client"); | |
| if (uri == null) throw new ArgumentNullException("uri"); | |
| if (requestJson == null) throw new ArgumentNullException("requestJson"); | |
| if (taskCompletionSource == null) throw new ArgumentNullException("taskCompletionSource"); | |
| client.CallService(uri, requestJson, responseJson => | |
| { | |
| var serviceResponse = Serializer.Deserialize<ServiceResponse>(responseJson); | |
| if (serviceResponse.ThrewException) | |
| throw new Exception(serviceResponse.ExceptionString); | |
| taskCompletionSource.SetResult(serviceResponse.ResponseJson == null | |
| ? default(T) | |
| : Serializer.Deserialize<T>(serviceResponse.ResponseJson)); | |
| }); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment