C
C#2y ago
GenTwo

❔ Java => C#

Hi all, Would anyone know enough Java to convert this codeblock from Java to C#? ChatGPT failed on this convert lol
package <route>;

import lombok.Getter;
import org.json.JSONObject;
import okhttp3.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@Getter
public class License {

private final String licenseKey;
private final String product;
private final String version;

private int statusCode;
private String discordName;
private String discordID;
private String statusMsg;
private boolean valid;

public License(String licenseKey, String product, String version) {
this.licenseKey = licenseKey;
this.product = product;
this.version = version;

OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\n \"license\": \""+ licenseKey +"\",\n \"product\": \""+ product +"\",\n \"version\": \""+ version +"\"\n}");
Request request = new Request.Builder()
.url("http://<ip>:<port>/api/client")
.method("POST", body)
.addHeader("Authorization", "<api-key>")
.build();
Response response = client.newCall(request).execute();

String data = response.body().string();
JSONObject obj = new JSONObject(data);

if (!obj.has("status_msg") || !obj.has("status_id")) return;

statusCode = obj.getInt("status_code");
statusMsg = obj.getString("status_msg");

if (!obj.has("status_overview")) return;

discordName = obj.getString("discord_tag");
discordID = obj.getString("discord_id");

valid = true;
}
}
package <route>;

import lombok.Getter;
import org.json.JSONObject;
import okhttp3.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@Getter
public class License {

private final String licenseKey;
private final String product;
private final String version;

private int statusCode;
private String discordName;
private String discordID;
private String statusMsg;
private boolean valid;

public License(String licenseKey, String product, String version) {
this.licenseKey = licenseKey;
this.product = product;
this.version = version;

OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\n \"license\": \""+ licenseKey +"\",\n \"product\": \""+ product +"\",\n \"version\": \""+ version +"\"\n}");
Request request = new Request.Builder()
.url("http://<ip>:<port>/api/client")
.method("POST", body)
.addHeader("Authorization", "<api-key>")
.build();
Response response = client.newCall(request).execute();

String data = response.body().string();
JSONObject obj = new JSONObject(data);

if (!obj.has("status_msg") || !obj.has("status_id")) return;

statusCode = obj.getInt("status_code");
statusMsg = obj.getString("status_msg");

if (!obj.has("status_overview")) return;

discordName = obj.getString("discord_tag");
discordID = obj.getString("discord_id");

valid = true;
}
}
8 Replies
FusedQyou
FusedQyou2y ago
1. private final String licenseKey; -> private readonly string licenseKey; 2. String -> string. 3. OkHttpClient -> I think this would just be HttpClient. 4. Request request = new Request.Builder() -> Something along the lines of a HttpWebRequest. HttpClient supports build in ways but you add headers and stuff so doing it with HttpWebRequest is easier. 5. JSONObject obj = new JSONObject(data); -> Newtonsoft has JObject.Parse() for parsing json without an explicit type. Recommend you make an explicit type and use JsonConvert.DezerializeObject<Type>() though. Instead of Newtonsoft you can also use System.Text.Json btw. Idk the direct syntax for that but it's build into newer .NET versions.
ero
ero2y ago
1. it's not better to make that a property, that's correct as a field 3. I don't see any async code, not sure if Java even has that. That might need adjusting 5. JsonSerializer.Deserialize<T>()
FusedQyou
FusedQyou2y ago
3. Remove all this behaviour from the constructor and create a static async method that creates a License class. Then you can call the HttpClient asynchronously.
ero
ero2y ago
Use raw string literals and interpolation for that json Oh wait yeah, that's all in the ctor That's too much code to be there
Florian Voß
Florian Voß2y ago
not sure if Java even has that.
there is no async and await in java sadly. But there is the Future Api with a CompletableFuture type. Thats what comes closest to our loved Task
sibber
sibber2y ago
most of that shouldnt be in the ctor
FusedQyou
FusedQyou2y ago
I think we established that 😄
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