| | 1 | | using DCL; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.Networking; |
| | 6 | |
|
| | 7 | | public interface IEventsAPIController |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Request all events from the server. |
| | 11 | | /// </summary> |
| | 12 | | /// <param name="OnSuccess">It will be triggered if the operation finishes successfully.</param> |
| | 13 | | /// <param name="OnFail">It will be triggered if the operation fails.</param> |
| | 14 | | /// <returns></returns> |
| | 15 | | WebRequestAsyncOperation GetAllEvents(Action<List<EventFromAPIModel>> OnSuccess, Action<string> OnFail); |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// Register/Unregister your user in a specific event. |
| | 19 | | /// </summary> |
| | 20 | | /// <param name="eventId">Event Id.</param> |
| | 21 | | /// <param name="isRegistered">True for registering.</param> |
| | 22 | | /// <param name="OnSuccess">It will be triggered if the operation finishes successfully.</param> |
| | 23 | | /// <param name="OnFail">It will be triggered if the operation fails.</param> |
| | 24 | | /// <returns></returns> |
| | 25 | | UnityWebRequestAsyncOperation RegisterAttendEvent(string eventId, bool isRegistered, Action OnSuccess, Action<string |
| | 26 | | } |
| | 27 | |
|
| | 28 | | public class EventsAPIController : IEventsAPIController |
| | 29 | | { |
| | 30 | | internal const string URL_GET_ALL_EVENTS = "https://events.decentraland.org/api/events"; |
| | 31 | | internal const string URL_POST_MESSAGE = "https://events.decentraland.org/api/message"; |
| | 32 | |
|
| 0 | 33 | | internal UserProfile ownUserProfile => UserProfile.GetOwnUserProfile(); |
| | 34 | |
|
| | 35 | | public WebRequestAsyncOperation GetAllEvents(Action<List<EventFromAPIModel>> OnSuccess, Action<string> OnFail) |
| | 36 | | { |
| 0 | 37 | | return DCL.Environment.i.platform.webRequest.Get( |
| | 38 | | URL_GET_ALL_EVENTS, |
| | 39 | | OnSuccess: (webRequestResult) => |
| | 40 | | { |
| 0 | 41 | | EventListFromAPIModel upcomingEventsResult = JsonUtility.FromJson<EventListFromAPIModel>(webRequestResul |
| 0 | 42 | | OnSuccess?.Invoke(upcomingEventsResult.data); |
| 0 | 43 | | }, |
| | 44 | | OnFail: (webRequestResult) => |
| | 45 | | { |
| 0 | 46 | | OnFail?.Invoke(webRequestResult.error); |
| 0 | 47 | | }); |
| | 48 | | } |
| | 49 | |
|
| | 50 | | public UnityWebRequestAsyncOperation RegisterAttendEvent(string eventId, bool isRegistered, Action OnSuccess, Action |
| | 51 | | { |
| 0 | 52 | | AttendEventRequestModel data = new AttendEventRequestModel |
| | 53 | | { |
| | 54 | | address = ownUserProfile.userId, |
| | 55 | | message = new AttendEventMessageModel |
| | 56 | | { |
| | 57 | | type = "attend", |
| | 58 | | timestamp = DateTime.Now.ToLongDateString(), |
| | 59 | | @event = eventId, |
| | 60 | | attend = isRegistered |
| | 61 | | }, |
| | 62 | | signature = "" |
| | 63 | | }; |
| | 64 | |
|
| 0 | 65 | | string json = JsonUtility.ToJson(data); |
| 0 | 66 | | var postWebRequest = new UnityWebRequest(URL_POST_MESSAGE, "POST"); |
| 0 | 67 | | byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json); |
| 0 | 68 | | postWebRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend); |
| 0 | 69 | | postWebRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer(); |
| 0 | 70 | | postWebRequest.SetRequestHeader("Content-Type", "application/json"); |
| | 71 | |
|
| 0 | 72 | | UnityWebRequestAsyncOperation asyncOp = postWebRequest.SendWebRequest(); |
| 0 | 73 | | asyncOp.completed += (asyncOp) => |
| | 74 | | { |
| 0 | 75 | | if (postWebRequest.result == UnityWebRequest.Result.Success) |
| 0 | 76 | | OnSuccess?.Invoke(); |
| | 77 | | else |
| 0 | 78 | | OnFail?.Invoke(postWebRequest.downloadHandler.text); |
| 0 | 79 | | }; |
| | 80 | |
|
| 0 | 81 | | return asyncOp; |
| | 82 | | } |
| | 83 | | } |