C
C#15mo ago
Elmishh

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

Is it possible?
22 Replies
Thinker
Thinker15mo ago
If it has an API then you can use it in C#
Elmishh
Elmishh15mo ago
Ok so how
Thinker
Thinker15mo 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
Elmishh15mo ago
https://api.hypixel.net/skyblock/bazaar I think i wrote the link wrong It is correct
Thinker
Thinker15mo 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
Elmishh15mo ago
That looks so complex to me 0-0
Thinker
Thinker15mo 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
Elmishh15mo 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
Pixel15mo ago
make it public
Thinker
Thinker15mo ago
Use HttpClient, not WebClient
Elmishh
Elmishh15mo ago
how would i do that
Thinker
Thinker15mo ago
Like I showed above
Elmishh
Elmishh15mo ago
my friend gave me the api part of the code
Thinker
Thinker15mo 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
MODiX15mo 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
Thinker15mo ago
also yes you need to make Seeds public
Elmishh
Elmishh15mo 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
Elmishh15mo ago
how its supposed to look
Elmishh
Elmishh15mo ago
how it looks
Elmishh
Elmishh15mo ago
Elmishh
Elmishh15mo ago
on first use it works fine first use per download but then its broken
Accord
Accord15mo 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.