Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,9 @@ UpgradeLog*.htm
*.pidb
*.*sdf
*.ipch

# Vscode user-specific files
.vscode/

# appsetting envs
appsettings.*.json
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# OdooRpc.CoreCLR.Client

Simple [Odoo JSON-RPC](https://www.odoo.com/documentation/9.0/api_integration.html) Client for [.Net Core 1.0](https://www.microsoft.com/net/core).
Simple [Odoo JSON-RPC](https://www.odoo.com/documentation/9.0/api_integration.html) Client for [.Net Core 3.1](https://www.microsoft.com/net/core).

Inspired by https://github.com/saidimu/odoo and https://github.com/osiell/odoorpc.

Expand Down Expand Up @@ -37,9 +37,16 @@ You will need nuget.exe on your path and set the NuGet API Key before you can pu

```Shell
nuget.exe setApiKey 76d7xxxx-xxxx-xxxx-xxxx-eabb8b0cxxxx
nuget.exe push .\build\OdooRpc.CoreCLR.Client.0.0.1.nupkg
nuget.exe push .\build\OdooRpc.CoreCLR.Client.V3.2.2.0.nupkg
```

### Samples
All updated to work with dotnetcore 3.1

*TODO*
1. update the appsettings.json file (note: enter the host without protocol prefix, and set the use of ssl)
2. Alternative 1, create an appsettings.Development.json file and optionally other env settings files, which you don't want source controlled.
Then run the sample project with `DOTNETCORE_ENVIRONMENT=Development dotnet run`
3. Alernative 2, use env variables instead of appsetting values when starting the sample project. Example:
`OdooConnection__Host=my.odoohost.com OdooConnection__Password=mypassword dotnet run`

The above commands are for UNIX, if you are on windows, command-line env vars are set like `set ENV_VAR=value && dotnet run`
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>Simple Odoo JSON-RPC Client for .Net Core 1.0</Description>
<Description>Simple Odoo JSON-RPC Client for .Net Core 3.1</Description>
<Authors>vmlf01;trafaelsilva</Authors>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyName>OdooRpc.CoreCLR.Client.Samples</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>OdooRpc.CoreCLR.Client.Samples</PackageId>
<PackageTags>odoo;openerp;dotnet code;json-rpc;dotnet</PackageTags>
<PackageProjectUrl>https://github.com/vmlf01/OdooRpc.CoreCLR.Client</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/vmlf01/OdooRpc.CoreCLR.Client/blob/master/LICENSE</PackageLicenseUrl>
<PackageLicense>https://github.com/vmlf01/OdooRpc.CoreCLR.Client/blob/master/LICENSE</PackageLicense>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/vmlf01/OdooRpc.CoreCLR.Client</RepositoryUrl>
<RuntimeFrameworkVersion>1.0.4</RuntimeFrameworkVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\OdooRpc.CoreCLR.Client\OdooRpc.CoreCLR.Client.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.3" />
</ItemGroup>

</Project>
194 changes: 19 additions & 175 deletions src/OdooRpc.CoreCLR.Client.Samples/Program.cs
Original file line number Diff line number Diff line change
@@ -1,196 +1,40 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using OdooRpc.CoreCLR.Client.Interfaces;
using Microsoft.Extensions.Configuration;
using OdooRpc.CoreCLR.Client.Models;
using OdooRpc.CoreCLR.Client.Models.Parameters;

namespace OdooRpc.CoreCLR.Client.Samples
{
public class Program
{
public static void Main(string[] args)
public static async Task Main(string[] args)
{
Console.WriteLine("Starting...");
var configuration = LoadConfiguration();
var odooConnection = configuration.GetSection("OdooConnection").Get<OdooConnectionInfo>();

var p = new Program();
p.LoginToOdoo().Wait();
//p.GetDepartments().Wait();
//p.SearchDepartments().Wait();
//p.GetDepartmentsFields().Wait();
//p.GetAllDepartments().Wait();
//p.CreateDeleteDepartment().Wait();
p.GetMetadata().Wait();
Console.WriteLine("Done! Press a key to exit...");
Console.ReadKey();
}

private OdooConnectionInfo OdooConnection;
private IOdooRpcClient OdooRpcClient;

public Program()
{
LoadSettings();
}

private void LoadSettings()
{
try
{
var settings = JsonConvert.DeserializeObject<JObject>(File.ReadAllText("appsettings.json"));
this.OdooConnection = settings["OdooConnection"].ToObject<OdooConnectionInfo>();
}
catch (Exception ex)
{
Console.WriteLine("Error reading app settings: {0}", ex.Message);
}
}

public async Task LoginToOdoo()
{
try
{
this.OdooRpcClient = new OdooRpcClient(this.OdooConnection);

var odooVersion = await this.OdooRpcClient.GetOdooVersion();

Console.WriteLine("Odoo Version: {0} - {1}", odooVersion.ServerVersion, odooVersion.ProtocolVersion);

await this.OdooRpcClient.Authenticate();

if (this.OdooRpcClient.SessionInfo.IsLoggedIn)
{
Console.WriteLine("Login successful => User Id: {0}", this.OdooRpcClient.SessionInfo.UserId);
}
else
{
Console.WriteLine("Login failed");
}
}
catch (Exception ex)
{
Console.WriteLine("Error connecting to Odoo: {0}", ex.Message);
}
}

public async Task GetAllDepartments()
{
try
{
var fieldParams = new OdooFieldParameters();
fieldParams.Add("name");
fieldParams.Add("company_id");
fieldParams.Add("color");

var departments = await this.OdooRpcClient.GetAll<JObject[]>("hr.department", fieldParams, new OdooPaginationParameters().OrderByDescending("name"));

Console.WriteLine(departments.FirstOrDefault());
}
catch (Exception ex)
{
Console.WriteLine("Error getting departments from Odoo: {0}", ex.Message);
}
}

public async Task GetDepartments()
{
try
{
var reqParams = new OdooGetParameters("hr.department");
reqParams.Ids.Add(6);
//reqParams.Ids.Add(7);
// var client = new SampleClient(odooConnection);
// await client.LoginToOdoo();
// await client.GetMetadata();

var fieldParams = new OdooFieldParameters();
fieldParams.Add("name");
fieldParams.Add("company_id");

var departments = await this.OdooRpcClient.Get<JObject[]>(reqParams, fieldParams);

Console.WriteLine(departments.FirstOrDefault());
}
catch (Exception ex)
{
Console.WriteLine("Error getting departments from Odoo: {0}", ex.Message);
}
// test your calls here
Console.WriteLine("Done!");
}

public async Task SearchDepartments()
private static IConfiguration LoadConfiguration()
{
try
{
var reqParams = new OdooSearchParameters(
"hr.department",
new OdooDomainFilter().Filter("name", "like", "SIC")
);
var environment = Environment.GetEnvironmentVariable("DOTNETCORE_ENVIRONMENT") ?? "Development";
Console.WriteLine($"Loading config for environment: {environment}");

var count = await this.OdooRpcClient.SearchCount(reqParams);
var departments = await this.OdooRpcClient.Search<long[]>(reqParams, new OdooPaginationParameters(0, 1));
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile($"appsettings.json")
.AddJsonFile($"appsettings.{environment}.json", true)
.AddEnvironmentVariables();
IConfiguration configuration = builder.Build();

Console.WriteLine(departments.FirstOrDefault());
}
catch (Exception ex)
{
Console.WriteLine("Error getting departments from Odoo: {0}", ex.Message);
}
return configuration;
}

public async Task GetDepartmentsFields()
{
try
{
var reqParams = new OdooGetModelFieldsParameters(
"hr.department"
);

var fields = await this.OdooRpcClient.GetModelFields<dynamic>(reqParams);

fields.ToList().ForEach(f => Console.WriteLine(f));
}
catch (Exception ex)
{
Console.WriteLine("Error getting partners from Odoo: {0}", ex.Message);
}
}

public async Task CreateDeleteDepartment()
{
try
{
var id = await this.OdooRpcClient.Create<dynamic>("hr.department", new
{
name = "test"
});

Console.WriteLine(id);

await this.OdooRpcClient.Delete("hr.department", id);
}
catch (Exception ex)
{
Console.WriteLine("Error getting partners from Odoo: {0}", ex.Message);
}
}

public async Task GetMetadata()
{
try
{
var metaParams = new OdooMetadataParameters("res.groups", new System.Collections.Generic.List<long>() { 4 });

var resp = await this.OdooRpcClient.GetMetadata(metaParams);

Console.WriteLine(resp.FirstOrDefault().ExternalId);
}
catch (Exception ex)
{
Console.WriteLine("Error getting metadata from Odoo: {0}", ex.Message);
}
}

}
}
Loading