Tizen.Maps Setup
[platform/core/csapi/tizenfx.git] / src / Tizen.Maps / Tizen.Maps / MapServiceRequest.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Threading.Tasks;
20
21 namespace Tizen.Maps
22 {
23     /// <summary>
24     /// Base class for map service request
25     /// </summary>
26     /// <typeparam name="T"></typeparam>
27     public abstract class MapServiceRequest<T>
28     {
29         protected TaskCompletionSource<IEnumerable<T>> _requestTask;
30         protected string errMessage;
31         protected int? _requestID;
32         protected ServiceRequestType _type;
33
34         internal Action startExecutionAction;
35         internal Interop.ErrorCode errorCode;
36         internal Interop.ServiceHandle _service;
37
38         /// <summary>
39         /// Creates map service request
40         /// </summary>
41         /// <param name="service">map service object</param>
42         /// <param name="type">Request type</param>
43         internal MapServiceRequest(MapService service, ServiceRequestType type)
44         {
45             _service = service.handle;
46             _type = type;
47         }
48
49         /// <summary>
50         /// Sends request to map service provider
51         /// </summary>
52         /// <returns>Response from map service provider</returns>
53         /// <exception cref="TaskCanceledException">Throws if request is canceled</exception>
54         /// <exception cref="InvalidOperationException">Throws if native operation failed</exception>
55         public async Task<IEnumerable<T>> GetResponseAsync()
56         {
57             if (_requestTask == null || _requestTask.Task.IsCanceled)
58             {
59                 _requestTask = new TaskCompletionSource<IEnumerable<T>>();
60                 startExecutionAction();
61                 await _requestTask.Task;
62             }
63             errorCode.WarnIfFailed(errMessage);
64             return await _requestTask.Task;
65         }
66
67         /// <summary>
68         /// Cancel this map service request
69         /// </summary>
70         public void Cancel()
71         {
72             if (_requestTask?.Task.IsCompleted == false)
73             {
74                 _requestTask?.SetCanceled();
75                 if (_requestID != null)
76                 {
77                     var err = Interop.Service.CancelRequest(_service, (int)_requestID);
78                     err.ThrowIfFailed(string.Format("Unable to cancel service request, Type: {0}, ID: {1}", _type, _requestID));
79                 }
80
81                 errorCode = Interop.ErrorCode.Canceled;
82             }
83         }
84     }
85 }