| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace DCL.Helpers.NFT.Markets.OpenSea_Internal |
| | 5 | | { |
| | 6 | | internal class SingleAssetRequestHandler : IRequestHandler |
| | 7 | | { |
| | 8 | | const bool VERBOSE = false; |
| | 9 | |
|
| 0 | 10 | | private readonly SchedulableRequestHandler schedulableHandler = new SchedulableRequestHandler(); |
| | 11 | | private readonly RequestAssetSingle request; |
| | 12 | | private readonly RequestController requestController; |
| | 13 | |
|
| | 14 | | private int retryCount = 0; |
| | 15 | |
|
| 0 | 16 | | SchedulableRequestHandler IRequestHandler.schedulableRequestHandler => schedulableHandler; |
| | 17 | |
|
| 0 | 18 | | public SingleAssetRequestHandler(RequestAssetSingle request, RequestController requestController) |
| | 19 | | { |
| | 20 | | if (VERBOSE) |
| | 21 | | Debug.Log($"SingleAssetRequestHandler: ({GetHashCode()}) {request.requestId} created"); |
| | 22 | |
|
| 0 | 23 | | this.request = request; |
| 0 | 24 | | this.requestController = requestController; |
| 0 | 25 | | schedulableHandler.SetReadyToBeScheduled(this); |
| 0 | 26 | | } |
| | 27 | |
|
| 0 | 28 | | string IRequestHandler.GetUrl() { return $"{Constants.SINGLE_ASSET_URL}/{request.contractAddress}/{request.token |
| | 29 | |
|
| | 30 | | void IRequestHandler.SetApiResponse(string responseJson, Action onSuccess, Action<string> onError) |
| | 31 | | { |
| 0 | 32 | | SingleAssetResponse response = null; |
| | 33 | |
|
| | 34 | | try |
| | 35 | | { |
| 0 | 36 | | response = Utils.FromJsonWithNulls<SingleAssetResponse>(responseJson); |
| 0 | 37 | | } |
| 0 | 38 | | catch (Exception e) |
| | 39 | | { |
| 0 | 40 | | onError?.Invoke(e.Message); |
| 0 | 41 | | } |
| | 42 | |
|
| 0 | 43 | | if (response == null) |
| 0 | 44 | | return; |
| | 45 | |
|
| | 46 | | if (VERBOSE) |
| | 47 | | Debug.Log($"SingleAssetRequestHandler: ({GetHashCode()}) {request.requestId} resolved"); |
| | 48 | |
|
| 0 | 49 | | request.Resolve(response); |
| 0 | 50 | | onSuccess?.Invoke(); |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | void IRequestHandler.SetApiResponseError(string error) |
| | 54 | | { |
| | 55 | | if (VERBOSE) |
| | 56 | | Debug.Log($"SingleAssetRequestHandler: ({GetHashCode()}) {request.requestId} rejected {error}"); |
| | 57 | |
|
| 0 | 58 | | request.Reject(error); |
| 0 | 59 | | } |
| | 60 | |
|
| 0 | 61 | | bool IRequestHandler.CanRetry() { return retryCount < Constants.REQUESTS_RETRY_ATTEMPS; } |
| | 62 | |
|
| | 63 | | void IRequestHandler.Retry() |
| | 64 | | { |
| | 65 | | if (VERBOSE) |
| | 66 | | Debug.Log($"SingleAssetRequestHandler: ({GetHashCode()}) {request.requestId} retry {retryCount}"); |
| | 67 | |
|
| 0 | 68 | | requestController.requestScheduler.EnqueueRequest(this); |
| 0 | 69 | | retryCount ++; |
| 0 | 70 | | } |
| | 71 | | } |
| | 72 | | } |