예제 #1
0
        public async Task <HttpResponseMessage> CreateApiVersion(string projectName, string apiVersionValue)
        {
            var project = await this
                          .GetProjectDataProvider()
                          .FindProject(
                organizationId: Project.GlobalProject,
                projectId: projectName)
                          .ConfigureAwait(continueOnCapturedContext: false);

            if (project == null)
            {
                throw new ErrorResponseMessageException(
                          httpStatus: HttpStatusCode.NotFound,
                          errorCode: ErrorResponseCode.ProjectNotFound,
                          errorMessage: string.Format("The project with name {0} was not found", projectName));
            }

            var apiVersionDefinition = await this.Request.Content
                                       .ReadAsAsync <ApiVersionDefinition>()
                                       .ConfigureAwait(continueOnCapturedContext: false);

            if (apiVersionDefinition == null)
            {
                throw new ErrorResponseMessageException(
                          httpStatus: HttpStatusCode.BadRequest,
                          errorCode: ErrorResponseCode.ApiVersionNull,
                          errorMessage: "The api version request cannot be null");
            }

            if (string.IsNullOrEmpty(apiVersionValue)) // TODO Validate string contents for valid url characters
            {
                throw new ErrorResponseMessageException(
                          httpStatus: HttpStatusCode.BadRequest,
                          errorCode: ErrorResponseCode.ApiVersionInvalid,
                          errorMessage: string.Format("The requested api version '{0}' is invalid.", apiVersionValue));
            }

            apiVersionDefinition.ApiVersion = apiVersionValue;

            var apiVersion = ApiVersion.CreateApiVersionFromDefinition(
                definition: apiVersionDefinition,
                organizationId: Project.GlobalProject,
                projectName: projectName);

            await this
            .GetApiVersionDataProvider()
            .SaveApiVersion(apiVersion)
            .ConfigureAwait(continueOnCapturedContext: false);

            // TODO: Copy most recent api version's swagger definition

            return(this.Request.CreateResponse(
                       statusCode: HttpStatusCode.Created,
                       value: apiVersion.ToDefinition(),
                       configuration: this.HttpConfiguration));
        }