protected override async Task PatchStubAsync(MaterialStub advMaterialStub, Amsv1MaterialData amsv1Data) { // logo var logoElement = advMaterialStub.GetElementByType(MaterialElementType.CompositeBitmapImage); string uploadUrl = logoElement.UploadUrl; var materialId = advMaterialStub.Id; string fileName = $"{amsv1Data.ImageName}.{amsv1Data.ImageExt}"; var uploadHash = await _okapiClient.UploadFileAsync(materialId, new Uri(uploadUrl), fileName, amsv1Data.ImageData); logoElement.Value.Raw = uploadHash.Raw; // crop area logoElement.Value.CropArea = CropAreaFactory.Create(amsv1Data); // background color var bgColorElement = advMaterialStub.GetElementByType(MaterialElementType.Color); bgColorElement.Value.Raw = amsv1Data.BackgroundColor; // custom images var customImages = new List <SizeSpecificImage>(); foreach (var c in amsv1Data.SizeSpecificImages) { var img = await CreateCustomImageAsync(materialId, new Uri(uploadUrl), c); customImages.Add(img); } logoElement.Value.SizeSpecificImages = customImages.ToArray(); advMaterialStub.Properties.Name = "logo_zmk"; }
public async Task <MaterialStub> CreateMaterialStubAsync(string type, string code, long firm, string language) { var uri = _baseUri + $"/api/{_apiVersion}/{type}/{code}/session?firm={firm}&languages={language}"; long materialsStubId = 0; try { using (var response = await _httpClient.PostAsync(uri, new StringContent(String.Empty, Encoding.UTF8, "application/json"))) { var content = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { _logger.Error("Unsuccessfull response with code {code} and content {content}", response.StatusCode, content); response.EnsureSuccessStatusCode(); } var stub = MaterialStub.FromJson(content).First(); materialsStubId = stub.Id; return(content != null ? stub : null); } } catch (Exception ex) { _logger.Error(ex, "Exception occured while creating AM template for firmId: {firmId}, stubId: {stubId}", firm, materialsStubId); throw; } }
private async Task <MaterialStub[]> GetMaterialAsync(long id) { var uri = _baseUri + $"/api/{_apiVersion}/am/{id}"; using (var response = await _retryPolicy.ExecuteAsync(() => _httpClient.GetAsync(uri))) { var content = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { _logger.Error("Unsuccessfull response with code {code} and content {content}; Material id: {id}", response.StatusCode, content, id); response.EnsureSuccessStatusCode(); } return(content != null?MaterialStub.FromJson(content) : null); } }
public async Task <MaterialStub> CreateNewMaterialAsync(long id, long firm, MaterialStub stub) { var uri = _baseUri + $"/api/{_apiVersion}/am/{id}?firm={firm}"; try { var reqContent = stub.ToJson(); using (var response = await _httpClient.PutAsync(uri, new StringContent(reqContent, Encoding.UTF8, "application/json"))) { var content = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { if (response.StatusCode == (HttpStatusCode)422) { _logger.Warning("[CREATE_MATERIAL] Advertisement material with id {advertisementId} does not satisfy some requirements and won't be imported. Errors: Response: {response}; Content: {content}", id, response, content); throw new UnprocessableEntityException($"Unprocessable entity ({response.StatusCode}) request status received.", content: content); } if (response.StatusCode == HttpStatusCode.Conflict) { _logger.Warning("Conflict occured while creating material with id {id}; Response: {response}; Content: {content}", id, response, content); _logger.Warning("[RECOVERING] Trying to get material {id} from okapi.", id); var am = await GetMaterialAsync(id); return(am.FirstOrDefault()); } _logger.Error("Unsuccessfull response with code {code} and content {content}", response.StatusCode, content); response.EnsureSuccessStatusCode(); } return(content != null?MaterialStub.SingleFromJson(content) : null); } } catch (Exception ex) { _logger.Error(ex, "[AM_CREATION_FAIL] Exception occured while creating new AM {amid}", id); throw; } }
protected override async Task PatchStubAsync(MaterialStub advMaterialStub, Amsv1MaterialData amsv1Data) { // original logo var logoElement = advMaterialStub.GetElementByType(MaterialElementType.CompositeBitmapImage); string uploadUrl = logoElement.UploadUrl; var materialId = advMaterialStub.Id; string fileName = $"{amsv1Data.ImageName}.{amsv1Data.ImageExt}"; var uploadHash = await _okapiClient.UploadFileAsync(materialId, new Uri(uploadUrl), fileName, amsv1Data.ImageData); logoElement.Value.Raw = uploadHash.Raw; // crop area logoElement.Value.CropArea = CropAreaFactory.Create(amsv1Data); // background color var bgColorElement = advMaterialStub.GetElementByType(MaterialElementType.Color); bgColorElement.Value.Raw = amsv1Data.BackgroundColor; advMaterialStub.Properties.Name = "logo_kb"; }
protected abstract Task PatchStubAsync(MaterialStub stub, Amsv1MaterialData amsv1Data);
public static Element GetElementByType(this MaterialStub stub, MaterialElementType elementType) { return(stub .Elements .FirstOrDefault(el => el.Type?.Equals(elementType.ToString(), StringComparison.OrdinalIgnoreCase) ?? false)); }
public static MaterialStub parseMaterial(CrpReader reader, bool saveFile, string saveFileName, long fileSize, bool verbose) { long fileContentBegin = reader.BaseStream.Position; MaterialStub retVal = new MaterialStub(); retVal.shaderName = reader.ReadString(); retVal.numProperties = reader.ReadInt32(); for (int i = 0; i < retVal.numProperties; i++) { int propertyType = reader.ReadInt32(); string propertyName = reader.ReadString(); switch (propertyType) { case 0: retVal.colors[propertyName] = reader.singlarObjParser["UnityEngine.Color"](); break; case 1: retVal.vectors[propertyName] = reader.singlarObjParser["UnityEngine.Vector4"](); break; case 2: retVal.floats[propertyName] = reader.ReadSingle(); break; case 3: bool isNull = reader.ReadBoolean(); if (!isNull) { retVal.textures[propertyName] = reader.ReadString(); } else { retVal.textures[propertyName] = ""; } break; } } if ((reader.BaseStream.Position - fileContentBegin) != fileSize) { int bytesToRead = (int)(fileSize - (reader.BaseStream.Position - fileContentBegin)); reader.ReadBytes(bytesToRead); } string fileName = saveFileName + ".json"; string json = JsonConvert.SerializeObject(retVal, Formatting.Indented); if (verbose) { Console.WriteLine("Read info file {0}", fileName); Console.WriteLine(json); } if (saveFile) { StreamWriter file = new StreamWriter(new FileStream(saveFileName + ".json", FileMode.Create)); file.Write(json); file.Close(); } return(retVal); }