private IRestResponse PostCreateRequest(WebApi api, RestSharp.JsonObject query, List <BaseEntity> entityList) { // if entity does not contain files, perform a normal post if (!(entityList[0] is IFileContainingEntity)) { return(api.Post($"/api/graphql", query)); } // otherwise, perform a multipart/form-data post var headers = new Dictionary <string, string> { { "Content-Type", "multipart/form-data" } }; var files = new List <FileData>(); foreach (var entity in entityList) { if (entity is IFileContainingEntity fileContainingEntity) { files.AddRange(fileContainingEntity.GetFiles().Where(file => file != null)); } } var param = new Dictionary <string, object> { { "operationName", query["operationName"] }, { "variables", query["variables"] }, { "query", query["query"] } }; return(api.Post($"/api/graphql", param, headers, DataFormat.None, files)); }
/// <summary> /// Put request to an end point with the specified parameters and headers. /// </summary> /// <param name="url">The Endpoint</param> /// <param name="param">The Request Body/Parameters</param> /// <param name="headers">The Request Headers</param> /// <param name="dataFormat">XML or JSon Body</param> /// <returns>The Requests Response</returns> public IRestResponse Put( string url, RestSharp.JsonObject body = null, Dictionary <string, string> headers = null, DataFormat dataFormat = DataFormat.Json) { return(RequestEndpoint(Method.PUT, url, body, headers, dataFormat)); }
/// <summary> /// Post to an end point with the specified parameters and headers. /// </summary> /// <param name="url">The Endpoint</param> /// <param name="body">The Request Body/Parameters</param> /// <param name="headers">The Request Headers</param> /// <param name="dataFormat">XML or JSon Body</param> /// <param name="files">Files to attatch to the request</param> /// <returns>The Requests Response</returns> public IRestResponse Post( string url, RestSharp.JsonObject body = null, Dictionary <string, string> headers = null, DataFormat dataFormat = DataFormat.Json, IEnumerable <FileData> files = null) { return(RequestEndpoint(Method.POST, url, body, headers, dataFormat, files)); }
private void SetNewPassword(string token, string username, string password) { var uri = $"{_configure.BaseUrl}/api/account/reset-password"; var query = new RestSharp.JsonObject { ["username"] = username, ["token"] = token, ["password"] = password, }; RequestHelpers.SendPostRequest(uri, query, _output); }
private void RequestResetPassword(string username) { var uri = $"{_configure.BaseUrl}/api/account/reset-password-request"; var query = new RestSharp.JsonObject { ["username"] = username }; RequestHelpers.SendPostRequest(uri, query, _output); }
public void TestGraphqlEndPointsUnauthorized(string entityName) { var api = new WebApi(_configure, _output); var query = new RestSharp.JsonObject(); query.Add("query", "{ " + entityName + "{id}}"); var response = api.Post($"/api/graphql", query); // we should get a valid response back Assert.Equal(HttpStatusCode.OK, response.StatusCode); }
public static void SendPostRequest(string uri, RestSharp.JsonObject query, ITestOutputHelper output) { var client = new RestClient { BaseUrl = new Uri(uri) }; var request = new RestRequest { Method = Method.POST, RequestFormat = DataFormat.Json }; request.AddParameter("application/json", query, ParameterType.RequestBody); request.AddHeader("Content-Type", "application/json"); request.AddHeader("Accept", "*\\*"); var response = client.Execute(request); ApiOutputHelper.WriteRequestResponseOutput(request, response, output); Assert.Equal(HttpStatusCode.OK, response.StatusCode); }
public void TestGraphqlEndPoints(string entityName) { var api = new WebApi(_configure, _output); var query = new RestSharp.JsonObject(); query.Add("query", "{ " + entityName + "{id}}"); api.ConfigureAuthenticationHeaders(); var response = api.Post($"/api/graphql", query); //check the ids are valid var validIds = JObject.Parse(response.Content)["data"][entityName] .Select(o => o["id"].Value <string>()) .All(o => !string.IsNullOrWhiteSpace(o)); //valid ids returned and a valid response Assert.Equal(HttpStatusCode.OK, response.StatusCode); }
private static RestSharp.JsonObject ConstructQuery(BaseEntity entity, ArrayList myAL) { var modelType = entity.GetType(); var entityVar = new RestSharp.JsonObject { { $"{modelType.Name.LowerCaseFirst()}", myAL } }; var fieldQueryPart = ConstructFieldQueryPart(entity); var creationType = entity is UserBaseEntity ? "CreateInput" : "Input"; var queryPart = $"mutation create{modelType.Name} (${modelType.Name.LowerCaseFirst()}: [{modelType.Name}{creationType}]) {{ create{modelType.Name}({modelType.Name.LowerCaseFirst()}s: ${modelType.Name.LowerCaseFirst()}){{ {fieldQueryPart} }} }}"; var query = new RestSharp.JsonObject { { "operationName", $"create{modelType.Name}" }, { "variables", entityVar }, { "query", queryPart } }; return(query); }
/// <summary> /// Query builder for batch updating of an entity /// Takes a list of BaseEntities to update. /// Generates new attribute values for all entities to be updated to, /// and returns a GraphQL query to perform the update with. /// </summary> /// <param name="baseEntities">The list of BaseEntities to be updated</param> /// <returns>The graphql query as a json object</returns> public static RestSharp.JsonObject BatchUpdateEntityQueryBuilder(List <BaseEntity> baseEntities) { string entityName = baseEntities[0].EntityName; var attributeNames = baseEntities[0].Attributes.Select(x => x.Name).ToList(); var newAttributes = new EntityFactory(entityName).Construct().ToJson(); var valuesToUpdate = new RestSharp.JsonObject(); attributeNames.ForEach(x => valuesToUpdate.Add(x.LowerCaseFirst(), $"{newAttributes[x.LowerCaseFirst()]}")); // Build the entity variables part var entityVar = new RestSharp.JsonObject { { $"idsToUpdate", baseEntities.Select(x => x.Id).ToArray() }, { "valuesToUpdate", valuesToUpdate }, { "fieldsToUpdate", attributeNames.ToArray() }, }; // Build the graphQL Query part var queryPart = $@"mutation update{entityName}sConditional ( $valuesToUpdate: {entityName}Input, $fieldsToUpdate: [String], $idsToUpdate:[ID]) {{update{entityName}sConditional( ids: $idsToUpdate, valuesToUpdate: $valuesToUpdate, fieldsToUpdate: $fieldsToUpdate){{ value }} }}" ; // Constructing query string RestSharp.JsonObject query = new RestSharp.JsonObject { { "operationName", $"update{entityName}sConditional" }, { "variables", entityVar }, { "query", queryPart } }; return(query); }
/// <summary> /// Query builder for deleting an entity /// </summary> /// <param name="entityKeyGuid"></param> /// <returns>The graphql query as a json object</returns> public static RestSharp.JsonObject DeleteEntityQueryBuilder(List <BaseEntity> entityList) { var entityName = entityList[0].EntityName; var guids = new List <Guid>(); entityList.ForEach(x => guids.Add(x.Id)); var entityVar = new RestSharp.JsonObject(); entityVar.Add($"{entityName.LowerCaseFirst()}Ids", guids.ToArray()); var queryPart = $@"mutation delete (${entityList[0].EntityName.LowerCaseFirst()}Ids: [ID]) {{ delete{entityName}({entityName.LowerCaseFirst()}Ids: ${entityName.LowerCaseFirst()}Ids){{ id __typename }} }}" ; var query = new RestSharp.JsonObject { { "operationName", "delete" }, { "variables", entityVar }, { "query", queryPart } }; return(query); }