< Summary

Class:EventsAPIController
Assembly:ExploreV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Sections/PlacesAndEventsSection/SubSections/EventsSubSection/EventsSubSectionMenu/EventsAPIController.cs
Covered lines:0
Uncovered lines:21
Coverable lines:21
Total lines:83
Line coverage:0% (0 of 21)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GetAllEvents(...)0%2100%
RegisterAttendEvent(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Sections/PlacesAndEventsSection/SubSections/EventsSubSection/EventsSubSectionMenu/EventsAPIController.cs

#LineLine coverage
 1using DCL;
 2using System;
 3using System.Collections.Generic;
 4using UnityEngine;
 5using UnityEngine.Networking;
 6
 7public 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
 28public 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
 033    internal UserProfile ownUserProfile => UserProfile.GetOwnUserProfile();
 34
 35    public WebRequestAsyncOperation GetAllEvents(Action<List<EventFromAPIModel>> OnSuccess, Action<string> OnFail)
 36    {
 037        return DCL.Environment.i.platform.webRequest.Get(
 38            URL_GET_ALL_EVENTS,
 39            OnSuccess: (webRequestResult) =>
 40            {
 041                EventListFromAPIModel upcomingEventsResult = JsonUtility.FromJson<EventListFromAPIModel>(webRequestResul
 042                OnSuccess?.Invoke(upcomingEventsResult.data);
 043            },
 44            OnFail: (webRequestResult) =>
 45            {
 046                OnFail?.Invoke(webRequestResult.error);
 047            });
 48    }
 49
 50    public UnityWebRequestAsyncOperation RegisterAttendEvent(string eventId, bool isRegistered, Action OnSuccess, Action
 51    {
 052        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
 065        string json = JsonUtility.ToJson(data);
 066        var postWebRequest = new UnityWebRequest(URL_POST_MESSAGE, "POST");
 067        byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
 068        postWebRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
 069        postWebRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
 070        postWebRequest.SetRequestHeader("Content-Type", "application/json");
 71
 072        UnityWebRequestAsyncOperation asyncOp = postWebRequest.SendWebRequest();
 073        asyncOp.completed += (asyncOp) =>
 74        {
 075            if (postWebRequest.result == UnityWebRequest.Result.Success)
 076                OnSuccess?.Invoke();
 77            else
 078                OnFail?.Invoke(postWebRequest.downloadHandler.text);
 079        };
 80
 081        return asyncOp;
 82    }
 83}