Esempio n. 1
0
        private void CreateSpace(string spaceId, string spaceName, string spaceDescription, Dictionary <string, object> spaceCustom, Dictionary <string, object> externalQueryParam, PNCallback <PNCreateSpaceResult> callback)
        {
            if (string.IsNullOrEmpty(spaceId) || string.IsNullOrEmpty(spaceId.Trim()) || spaceName == null)
            {
                throw new ArgumentException("Missing Id or Name");
            }

            if (string.IsNullOrEmpty(config.SubscribeKey) || string.IsNullOrEmpty(config.SubscribeKey.Trim()) || config.SubscribeKey.Length <= 0)
            {
                throw new MissingMemberException("Invalid subscribe key");
            }

            if (callback == null)
            {
                throw new ArgumentException("Missing userCallback");
            }


            IUrlRequestBuilder urlBuilder = new UrlRequestBuilder(config, jsonLibrary, unit, pubnubLog, pubnubTelemetryMgr);

            urlBuilder.PubnubInstanceId = (PubnubInstance != null) ? PubnubInstance.InstanceId : "";
            Uri request = urlBuilder.BuildCreateSpaceRequest(spaceCustom, externalQueryParam);

            RequestState <PNCreateSpaceResult> requestState = new RequestState <PNCreateSpaceResult>();

            requestState.ResponseType      = PNOperationType.PNCreateSpaceOperation;
            requestState.PubnubCallback    = callback;
            requestState.Reconnect         = false;
            requestState.EndPointOperation = this;

            string json = "";

            requestState.UsePostMethod = true;
            Dictionary <string, object> messageEnvelope = new Dictionary <string, object>();

            messageEnvelope.Add("id", spaceId);
            messageEnvelope.Add("name", spaceName);
            if (spaceDescription != null)
            {
                messageEnvelope.Add("description", spaceDescription);
            }
            if (spaceCustom != null)
            {
                messageEnvelope.Add("custom", spaceCustom);
            }
            string postMessage = jsonLibrary.SerializeToJsonString(messageEnvelope);

            json = UrlProcessRequest <PNCreateSpaceResult>(request, requestState, false, postMessage);

            if (!string.IsNullOrEmpty(json))
            {
                List <object> result = ProcessJsonResponse <PNCreateSpaceResult>(requestState, json);
                ProcessResponseCallbacks(result, requestState);
            }
        }