C
C#2y ago
Elmishh

❔ using hypixel's api in a C# console app

Is it possible?
22 Replies
Thinker
Thinker2y ago
If it has an API then you can use it in C#
Elmishh
ElmishhOP2y ago
Ok so how
Thinker
Thinker2y ago
Tutorial: Make HTTP requests in a .NET console app using C#
Learn how to make HTTP requests to a REST web service and deserialize JSON responses. This tutorial creates a .NET console and uses C#.
Elmishh
ElmishhOP2y ago
https://api.hypixel.net/skyblock/bazaar I think i wrote the link wrong It is correct
Thinker
Thinker2y ago
HttpClient client = new();
var response = await client.GetAsync(url);
HttpClient client = new();
var response = await client.GetAsync(url);
this is the gist of it looks correct So as a quick mockup
HttpClient client = new();
var response = await client.GetFromJsonAsync<RootResponse>("https://api.hypixel.net/skyblock/bazaar");

record RootResponse(bool Success, DateTime LastUpdated, Dictionary<string, Product> Products);
record Product(string ProductId, SellSummary SellSummary);
record SellSummary(int Amount, float PricePerUnit);
// etc.
HttpClient client = new();
var response = await client.GetFromJsonAsync<RootResponse>("https://api.hypixel.net/skyblock/bazaar");

record RootResponse(bool Success, DateTime LastUpdated, Dictionary<string, Product> Products);
record Product(string ProductId, SellSummary SellSummary);
record SellSummary(int Amount, float PricePerUnit);
// etc.
Elmishh
ElmishhOP2y ago
That looks so complex to me 0-0
Thinker
Thinker2y ago
It's really not You create an HttpClient and wait for it to respond to your url. The rest just defines the structure of the data you're expecting to get back. The article I sent covers how to do this
Elmishh
ElmishhOP2y ago
soooo
using System;
using System.Net;
using itemprices;
using Newtonsoft.Json.Linq;

namespace itemprices
{
class SEEDS
{
static void seeds()
{

using (WebClient client = new WebClient())
{
int fuelx2000 = 13333;
int required_organic_matter = 4000;
int item_organic_matter = 1;
string responseString1 = client.DownloadString("https://api.slothpixel.me/api/skyblock/bazaar/SEEDS");


JObject responseJson = JObject.Parse(responseString1);

float item_buy_Price = (float)responseJson["sell_summary"][0]["pricePerUnit"];

string responseString2 = client.DownloadString("https://api.slothpixel.me/api/skyblock/bazaar/COMPOST");


JObject responseJson2 = JObject.Parse(responseString2);


float Compost_Sell_Price = (float)responseJson2["buy_summary"][0]["pricePerUnit"];

float Enchanted_Seeds_profit = Compost_Sell_Price - ((item_buy_Price / item_organic_matter) * required_organic_matter) - fuelx2000;
Console.WriteLine(Enchanted_Seeds_profit);
}
}
}
}



class Program
{
public static void Main()
{
SEEDS.seeds();
}
}
using System;
using System.Net;
using itemprices;
using Newtonsoft.Json.Linq;

namespace itemprices
{
class SEEDS
{
static void seeds()
{

using (WebClient client = new WebClient())
{
int fuelx2000 = 13333;
int required_organic_matter = 4000;
int item_organic_matter = 1;
string responseString1 = client.DownloadString("https://api.slothpixel.me/api/skyblock/bazaar/SEEDS");


JObject responseJson = JObject.Parse(responseString1);

float item_buy_Price = (float)responseJson["sell_summary"][0]["pricePerUnit"];

string responseString2 = client.DownloadString("https://api.slothpixel.me/api/skyblock/bazaar/COMPOST");


JObject responseJson2 = JObject.Parse(responseString2);


float Compost_Sell_Price = (float)responseJson2["buy_summary"][0]["pricePerUnit"];

float Enchanted_Seeds_profit = Compost_Sell_Price - ((item_buy_Price / item_organic_matter) * required_organic_matter) - fuelx2000;
Console.WriteLine(Enchanted_Seeds_profit);
}
}
}
}



class Program
{
public static void Main()
{
SEEDS.seeds();
}
}
is my code and im getting 'SEEDS.seeds()' is inaccessible due to its protection level what should i do
Pixel
Pixel2y ago
make it public
Thinker
Thinker2y ago
Use HttpClient, not WebClient
Elmishh
ElmishhOP2y ago
how would i do that
Thinker
Thinker2y ago
Like I showed above
Elmishh
ElmishhOP2y ago
my friend gave me the api part of the code
Thinker
Thinker2y ago
HttpClient client = new();
var response = await client.GetAsync(url);
HttpClient client = new();
var response = await client.GetAsync(url);
And since your JSON has a well-defined structure, there is no reason to use JObject here, you can just turn the JSON into regular objects. You can use $jsongen to generate classes from your JSON which you can then deserialize the string into.
MODiX
MODiX2y ago
Use https://app.quicktype.io or https://json2csharp.com to generate classes from your JSON
Instantly parse JSON in any language | quicktype
Whether you're using C#, Swift, TypeScript, Go, C++ or other languages, quicktype generates models and helper code for quickly and safely reading JSON in your apps. Customize online with advanced options, or download a command-line tool.
Convert JSON to C# Classes Online - Json2CSharp Toolkit
Convert any JSON object to C# classes online. Json2CSharp is a free toolkit that will help you generate C# classes on the fly.
Thinker
Thinker2y ago
also yes you need to make Seeds public
Elmishh
ElmishhOP2y ago
yeah i figured it out well i published it (yeah the code is bad whatever) and its completely broken the .exe is NOT working it is but not correctly at all
Elmishh
ElmishhOP2y ago
how its supposed to look
Elmishh
ElmishhOP2y ago
how it looks
Elmishh
ElmishhOP2y ago
Elmishh
ElmishhOP2y ago
on first use it works fine first use per download but then its broken
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server