public EquipItemsResponse EquipItems(EquipItemsRequest equipRequest) { HttpRequestMessage request = new HttpRequestMessage(); request.RequestUri = new Uri("https://www.bungie.net/Platform/Destiny2/Actions/Items/EquipItems/"); request.Method = HttpMethod.Post; string contentString = JsonConvert.SerializeObject(equipRequest); request.Content = new StringContent(contentString, Encoding.UTF8, "application/json"); request.Headers.Add("X-API-KEY", config["Bungie:ApiKey"]); request.Headers.Add("Authorization", $"Bearer {equipRequest.AccessToken}"); HttpResponseMessage response = httpClient.SendAsync(request).Result; if (response.IsSuccessStatusCode) { string json = response.Content.ReadAsStringAsync().Result; EquipItemsResponse equipResponse = JsonConvert.DeserializeObject <EquipItemsResponse>(json); return(equipResponse); } else if (response.StatusCode == HttpStatusCode.Unauthorized) { throw new BungieApiException("Unauthorized: Access is denied due to invalid credentials."); } else { throw new BungieApiException("idk"); } }
public EmissaryResult EquipLoadout(ulong discordId, string loadoutName) { EmissaryUser user = emissaryDao.GetUserByDiscordId(discordId); if (user == null) { RequestAuthorizationEvent?.Invoke(discordId); return(EmissaryResult.FromError("i need access to your bungie account to do this. please check your DMs for instructions")); } long destinyCharacterId = GetMostRecentlyPlayedCharacterId(user.DestinyMembershipType, user.DestinyProfileId); loadoutName = loadoutName.Trim(); Loadout loadout = emissaryDao.GetLoadout(discordId, destinyCharacterId, loadoutName); IList <DestinyItem> itemsToEquip = loadout.Items; foreach (DestinyItem item in itemsToEquip.ToList()) { if (item.TierTypeName == "Exotic") { itemsToEquip.RemoveAt(itemsToEquip.IndexOf(item)); itemsToEquip.Add(item); } } IList <long> itemInstanceIds = itemsToEquip.Select(item => item.ItemInstanceId).ToList(); string bungieAccessToken = authorizationService.GetAccessToken(discordId); if (bungieAccessToken == null) { RequestAuthorizationEvent?.Invoke(discordId); return(EmissaryResult.FromError("i need access to your bungie account to do this. please check your DMs for instructions")); } EquipItemsRequest equipRequest = new EquipItemsRequest(bungieAccessToken, user.DestinyMembershipType, destinyCharacterId, itemInstanceIds); EquipItemsResponse equipResponse; try { equipResponse = bungieApiService.EquipItems(equipRequest); } catch (BungieApiException e) { string errorMessage = e.Message; if (errorMessage.Contains("Unauthorized")) { RequestAuthorizationEvent?.Invoke(discordId); errorMessage += " please check your DMs for instructions"; } return(EmissaryResult.FromError(errorMessage)); } // TODO equip exotics last. // otherwise we might get the DestinyItemUniqueEquipRestricted error, which is annoying // because i have to tell the user to try again or do a thread.sleep(0.1sec) which is a big waste. // instead, adjust the order of the equip request so that exotics are very last. that way the // purple legendaries are equipped first (replacing any exotics in that slot) // and we won't have an exotic equip limit error. EmissaryResult result; if (equipResponse.EquipResults.All(equipResult => equipResult.EquipStatus == BungiePlatformErrorCodes.Success)) { result = EmissaryResult.FromSuccess(JsonConvert.SerializeObject(loadout)); } else { string errorMessage = "some items could not be equipped."; foreach (EquipItemResult equipResult in equipResponse.EquipResults) { if (equipResult.EquipStatus != BungiePlatformErrorCodes.Success) { errorMessage += $"\n{GetErrorDescriptionHelpMessageForEquipFail(equipResult.EquipStatus)}"; } } result = EmissaryResult.FromError(errorMessage); } return(result); }