Added RouteName and Request/Response handling to the code generator. More work is needed.
This commit is contained in:
@ -13,14 +13,17 @@ namespace MontoyaTech.Rest.Net.Example
|
||||
{
|
||||
public string BaseUrl;
|
||||
|
||||
private HttpClient HttpClient;
|
||||
public HttpClient HttpClient;
|
||||
|
||||
public TestApi Test;
|
||||
|
||||
public AuthApi Auth;
|
||||
|
||||
public Client(string baseUrl)
|
||||
{
|
||||
this.BaseUrl = baseUrl;
|
||||
this.Test = new TestApi(this);
|
||||
this.Auth = new AuthApi(this);
|
||||
this.HttpClient = new HttpClient();
|
||||
this.HttpClient.DefaultRequestHeaders.Add("Accept", "*/*");
|
||||
this.HttpClient.DefaultRequestHeaders.Add("Connection", "keep-alive");
|
||||
@ -38,26 +41,72 @@ namespace MontoyaTech.Rest.Net.Example
|
||||
|
||||
public string Status()
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/status");
|
||||
return default;
|
||||
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/status");
|
||||
|
||||
var response = this.Client.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == System.Net.HttpStatusCode.OK)
|
||||
return Newtonsoft.Json.JsonConvert.DeserializeObject<string>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
else
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
|
||||
public string Add(double a, double b)
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, $"{this.Client.BaseUrl}/add/{a}/{b}");
|
||||
return default;
|
||||
var message = new HttpRequestMessage(HttpMethod.Post, $"{this.Client.BaseUrl}/add/{a}/{b}");
|
||||
|
||||
var response = this.Client.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == System.Net.HttpStatusCode.OK)
|
||||
return Newtonsoft.Json.JsonConvert.DeserializeObject<string>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
else
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
}
|
||||
|
||||
public class AuthApi
|
||||
{
|
||||
public Client Client;
|
||||
|
||||
public AuthApi(Client client)
|
||||
{
|
||||
this.Client = client;
|
||||
}
|
||||
|
||||
public string Signup(User user)
|
||||
public bool UserExists(string name)
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, $"{this.Client.BaseUrl}/signup/{user}");
|
||||
return default;
|
||||
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/auth/{name}");
|
||||
|
||||
var response = this.Client.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == System.Net.HttpStatusCode.OK)
|
||||
return Newtonsoft.Json.JsonConvert.DeserializeObject<bool>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
else
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
|
||||
public User Json()
|
||||
public void Signup(User request)
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/json");
|
||||
return default;
|
||||
var message = new HttpRequestMessage(HttpMethod.Post, $"{this.Client.BaseUrl}/auth/signup");
|
||||
|
||||
message.Content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(request));
|
||||
|
||||
var response = this.Client.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == System.Net.HttpStatusCode.OK)
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
|
||||
public User Get()
|
||||
{
|
||||
var message = new HttpRequestMessage(HttpMethod.Get, $"{this.Client.BaseUrl}/auth");
|
||||
|
||||
var response = this.Client.HttpClient.Send(message);
|
||||
|
||||
if (response.StatusCode == System.Net.HttpStatusCode.OK)
|
||||
return Newtonsoft.Json.JsonConvert.DeserializeObject<User>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||
else
|
||||
throw new Exception("Unexpected Http Response StatusCode:" + response.StatusCode);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user