Beispiel #1
0
            public void DownloadImageAsync(long productID, string url, Action <long, Texture2D> doneCallbackAction = null)
            {
                if (s_MissingTexture == null)
                {
                    s_MissingTexture = (Texture2D)EditorGUIUtility.LoadRequired("Icons/UnityLogo.png");
                }

                var texture = AssetStoreCache.instance.LoadImage(productID, url);

                if (texture != null)
                {
                    doneCallbackAction?.Invoke(productID, texture);
                    return;
                }

                var httpRequest = m_AsyncHTTPClient.GetASyncHTTPClient(url);

                httpRequest.doneCallback = httpClient =>
                {
                    if (httpClient.IsSuccess() && httpClient.texture != null)
                    {
                        AssetStoreCache.instance.SaveImage(productID, url, httpClient.texture);
                        doneCallbackAction?.Invoke(productID, httpClient.texture);
                        return;
                    }

                    doneCallbackAction?.Invoke(productID, s_MissingTexture);
                };
                httpRequest.Begin();
            }
            public void GetProductIDList(int offset, int limit, Action <ProductList> doneCallbackAction)
            {
                var returnList = new ProductList
                {
                    count   = 0,
                    isValid = false,
                    list    = new List <long>()
                };

                AssetStoreOAuth.instance.FetchUserInfo(userInfo =>
                {
                    if (!userInfo.isValid)
                    {
                        returnList.errorMessage = userInfo.errorMessage;
                        doneCallbackAction?.Invoke(returnList);
                        return;
                    }

                    limit           = limit > 0 ? limit : kDefaultLimit;
                    var httpRequest = m_AsyncHTTPClient.GetASyncHTTPClient($"{m_Host}{kListUri}?offset={offset}&limit={limit}");
                    httpRequest.header["Authorization"] = "Bearer " + userInfo.accessToken.access_token;
                    httpRequest.doneCallback            = httpClient =>
                    {
                        var errorMessage = "Failed to parse JSON.";
                        if (httpClient.IsSuccess() && httpClient.responseCode == 200)
                        {
                            try
                            {
                                var res = Json.Deserialize(httpClient.text) as Dictionary <string, object>;
                                if (res != null)
                                {
                                    var total          = (long)res["total"];
                                    returnList.count   = total;
                                    returnList.isValid = true;

                                    if (total == 0)
                                    {
                                        doneCallbackAction?.Invoke(returnList);
                                        return;
                                    }

                                    var results = res["results"] as IList <object>;
                                    foreach (var result in results)
                                    {
                                        var item      = result as Dictionary <string, object>;
                                        var packageId = item["packageId"];
                                        returnList.list.Add((long)packageId);
                                    }

                                    doneCallbackAction?.Invoke(returnList);
                                    return;
                                }
                            }
                            catch (Exception e)
                            {
                                errorMessage = e.Message;
                            }
                        }
                        else
                        {
                            errorMessage = httpClient.text;
                        }

                        returnList.errorMessage = errorMessage;
                        doneCallbackAction?.Invoke(returnList);
                    };
                    httpRequest.Begin();
                });
            }
Beispiel #3
0
 private void GetAccessToken()
 {
     if (string.IsNullOrEmpty(m_UserInfo.authCode))
     {
         GetAuthCode();
         return;
     }
     if (m_UserInfo.accessToken != null && m_UserInfo.accessToken.IsValid())
     {
         GetTokenInfo();
         return;
     }
     if (m_AccessTokenRequest != null)
     {
         return;
         // a request is already running, no need to recall
     }
     m_AccessTokenRequest          = m_AsyncHTTPClient.GetASyncHTTPClient($"{m_Host}{kOAuthUri}", "POST");
     m_AccessTokenRequest.postData = $"grant_type=authorization_code&code={m_UserInfo.authCode}&client_id=packman&client_secret={m_Secret}&redirect_uri=packman://unity";
     m_AccessTokenRequest.header["Content-Type"] = "application/x-www-form-urlencoded";
     m_AccessTokenRequest.doneCallback           = httpClient =>
     {
         if (httpClient.IsSuccess())
         {
             var res = Json.Deserialize(httpClient.text) as Dictionary <string, object>;
             if (res != null)
             {
                 var accessTokenResponse = new AccessToken();
                 accessTokenResponse.access_token  = res["access_token"] as string;
                 accessTokenResponse.token_type    = res["token_type"] as string;
                 accessTokenResponse.expires_in    = res["expires_in"] as string;
                 accessTokenResponse.refresh_token = res["refresh_token"] as string;
                 accessTokenResponse.user          = res["user"] as string;
                 accessTokenResponse.display_name  = res["display_name"] as string;
                 m_UserInfo.accessToken            = accessTokenResponse;
                 if (m_UserInfo.accessToken.IsValid())
                 {
                     GetTokenInfo();
                 }
                 else
                 {
                     m_UserInfo.errorMessage = "Access token invalid";
                     OnDoneFetchUserInfo();
                 }
             }
             else
             {
                 m_UserInfo.errorMessage = "Failed to parse JSON.";
                 m_UserInfo.accessToken  = null;
                 OnDoneFetchUserInfo();
             }
         }
         else
         {
             m_UserInfo.errorMessage = httpClient.text;
             m_UserInfo.accessToken  = null;
             OnDoneFetchUserInfo();
         }
         m_AccessTokenRequest = null;
     };
     m_AccessTokenRequest.Begin();
 }