Skip to content

Commit 67f130d

Browse files
committedAug 3, 2022
Inclusão de exemplo de uso e tratamento de produto nulo
1 parent 6953a03 commit 67f130d

9 files changed

+455
-0
lines changed
 

‎Gs1BrDotNet/Exemplo/Exemplo.csproj

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\Gs1BrDotNet\Gs1BrDotNet.csproj" />
12+
</ItemGroup>
13+
14+
</Project>

‎Gs1BrDotNet/Exemplo/Program.cs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Gs1Br;
2+
3+
public class Program
4+
{
5+
public static void Main(string[] args)
6+
{
7+
Gs1brConfig config = new Gs1brConfig("2da3d381-43ae-4e0a-8cc4-8d583f7732ec", "23e15806-bb04-4c0c-97e8-f8beb885cd6a", "teste@teste.com", "senha");
8+
9+
AutenticacaoGs1Br auth = new AutenticacaoGs1Br(config);
10+
RetornoLogin retorno = auth.Autenticar();
11+
12+
ConsultaGtin consulta = new ConsultaGtin();
13+
RetornoConsultaGtinGs1Br retornoGtin = consulta.ConsultarGtin("07896412802546", retorno);
14+
15+
Product produto = retornoGtin.product;
16+
if (produto == null)
17+
{
18+
Console.WriteLine($@"{retornoGtin.status} - {retornoGtin.message}!");
19+
} else
20+
{
21+
Console.WriteLine($@"Produto: {produto.LerDescricao()}
22+
{produto.LerUrlFoto()}
23+
{produto.LerMarca()}
24+
{produto.LerClassificacaoGPC()}
25+
{produto.LerNCM()}
26+
{produto.LerCEST()}
27+
{produto.LerEmbalagem()}");
28+
}
29+
30+
Console.ReadKey();
31+
}
32+
}

‎Gs1BrDotNet/Gs1BrDotNet.sln

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.1.32407.343
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gs1BrDotNet", "Gs1BrDotNet\Gs1BrDotNet.csproj", "{7E01AAD1-6218-46E0-931F-16E5D5177252}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Exemplo", "Exemplo\Exemplo.csproj", "{3B864689-C242-47EA-AACC-A61BF97F9461}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{7E01AAD1-6218-46E0-931F-16E5D5177252}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{7E01AAD1-6218-46E0-931F-16E5D5177252}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{7E01AAD1-6218-46E0-931F-16E5D5177252}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{7E01AAD1-6218-46E0-931F-16E5D5177252}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{3B864689-C242-47EA-AACC-A61BF97F9461}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{3B864689-C242-47EA-AACC-A61BF97F9461}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{3B864689-C242-47EA-AACC-A61BF97F9461}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{3B864689-C242-47EA-AACC-A61BF97F9461}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {3EB0439A-F572-46C4-9F5A-35CB23814A8C}
30+
EndGlobalSection
31+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using Newtonsoft.Json;
2+
using RestSharp;
3+
using RestSharp.Authenticators;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace Gs1Br
11+
{
12+
public class AutenticacaoGs1Br
13+
{
14+
public AutenticacaoGs1Br(Gs1brConfig configGs1br)
15+
{
16+
config = configGs1br;
17+
}
18+
19+
public Gs1brConfig config { get; }
20+
21+
public RetornoLogin Autenticar()
22+
{
23+
//var gs1brConfig = Gs1brConfig.InstanciaConfig;
24+
25+
var client = new RestClient("https://api.gs1br.org/oauth/");
26+
client.Authenticator = new HttpBasicAuthenticator(config.ClientId, config.SecretId);
27+
28+
RestRequest request = new RestRequest("access-token", Method.Post);
29+
30+
request.AddHeader("Content-Type", "application/json");
31+
request.AddHeader("client_id", config.ClientId);
32+
33+
var body = new { grant_type = "password", username = config.Username, password = config.Password };
34+
35+
request.AddParameter("application/json", body, ParameterType.RequestBody);
36+
RestResponse response = client.Execute(request);
37+
if(response.StatusCode != System.Net.HttpStatusCode.Created && response.StatusCode != System.Net.HttpStatusCode.OK)
38+
throw new Exception(response.ErrorMessage);
39+
40+
string json = response.Content;
41+
42+
RetornoLogin retornoLogin = JsonConvert.DeserializeObject<RetornoLogin>(json);
43+
retornoLogin.clientId = config.ClientId;
44+
return retornoLogin;
45+
}
46+
}
47+
48+
49+
public class RetornoLogin
50+
{
51+
public RetornoLogin()
52+
{
53+
created = DateTime.Now;
54+
}
55+
56+
public bool Expired()
57+
{
58+
DateTime expires = created.AddMilliseconds(expires_in);
59+
return expires > DateTime.Now;
60+
}
61+
62+
public string clientId { get; set; }
63+
public string access_token { get; set; }
64+
public string refresh_token { get; set; }
65+
public string token_type { get; set; }
66+
public int expires_in { get; set; }
67+
private DateTime created { get; set; }
68+
}
69+
}
70+
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Newtonsoft.Json;
2+
using RestSharp;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Gs1Br
10+
{
11+
public class ConsultaGtin
12+
{
13+
public RetornoConsultaGtinGs1Br ConsultarGtin(string gtin, RetornoLogin login)
14+
{
15+
var client = new RestClient("https://api.gs1br.org/gs1/v2/");
16+
17+
RestRequest request = new RestRequest($"products/{gtin}", Method.Get);
18+
19+
request.AddHeader("Content-Type", "application/json");
20+
request.AddHeader("client_id", login.clientId);
21+
request.AddHeader("access_token", login.access_token);
22+
23+
request.AddParameter("application/json", ParameterType.RequestBody);
24+
RestResponse response = client.Execute(request);
25+
26+
if (response.StatusCode != System.Net.HttpStatusCode.Created && response.StatusCode != System.Net.HttpStatusCode.OK)
27+
throw new Exception($"Erro: {response.StatusCode} - {response.ErrorMessage}");
28+
29+
string json = response.Content;
30+
RetornoConsultaGtinGs1Br retornoConsulta = JsonConvert.DeserializeObject<RetornoConsultaGtinGs1Br>(json);
31+
return retornoConsulta;
32+
33+
}
34+
}
35+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.1</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<Description>Validation GTIN GS1Br Library</Description>
7+
<PackageIcon>biosac64.png</PackageIcon>
8+
<AssemblyVersion>2022.8.1</AssemblyVersion>
9+
<FileVersion>2022.8.1</FileVersion>
10+
<Version>2022.8.1</Version>
11+
<PackageProjectUrl>https://github.com/sacfiscal/gs1brdotnet</PackageProjectUrl>
12+
<RepositoryUrl>https://github.com/sacfiscal/gs1brdotnet</RepositoryUrl>
13+
<RepositoryType>git</RepositoryType>
14+
</PropertyGroup>
15+
16+
<ItemGroup>
17+
<None Include="D:\Users\Marco Polo\Pictures\SACFiscal\biosac64.png">
18+
<Pack>True</Pack>
19+
<PackagePath>\</PackagePath>
20+
</None>
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
25+
<PackageReference Include="RestSharp" Version="108.0.1" />
26+
</ItemGroup>
27+
28+
</Project>
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Gs1Br
6+
{
7+
public class Gs1brConfig
8+
{
9+
public Gs1brConfig(string gs1brGs1brClientId, string gs1brSecretId, string gs1brUsername, string gs1brPassword)
10+
{
11+
ClientId = gs1brGs1brClientId;
12+
SecretId = gs1brSecretId;
13+
Username = gs1brUsername;
14+
Password = gs1brPassword;
15+
}
16+
17+
public static void DefinirGs1brConfig(string gs1brGs1brClientId, string gs1brSecretId, string gs1brUsername, string gs1brPassword)
18+
{
19+
InstanciaConfig = new Gs1brConfig(gs1brGs1brClientId, gs1brSecretId, gs1brUsername, gs1brPassword);
20+
}
21+
public static Gs1brConfig InstanciaConfig { get; private set; }
22+
23+
public string ClientId { get; private set; }
24+
public string SecretId { get; private set; }
25+
public string Username { get; private set; }
26+
public string Password { get; private set; }
27+
}
28+
}
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Gs1Br
8+
{
9+
public static class Gs1brExtensions
10+
{
11+
public static string LerDescricao(this Product product)
12+
{
13+
Tradeitemdescriptioninformationlang descricao = product.tradeItemDescriptionInformationLang.FirstOrDefault(x => x.languageCode.Equals("pt-BR"));
14+
15+
if (descricao == null)
16+
return "";
17+
18+
return descricao.tradeItemDescription;
19+
}
20+
21+
public static string LerUrlFoto(this Product product)
22+
{
23+
Referencedfileinformation foto = product.referencedFileInformations.FirstOrDefault(x => x.languageCode.Equals("pt-BR"));
24+
25+
if (foto == null)
26+
return "";
27+
28+
return foto.uniformResourceIdentifier;
29+
}
30+
31+
public static string LerMarca(this Product product)
32+
{
33+
Brandnameinformationlang marca = product.brandNameInformationLang.FirstOrDefault(x => x.languageCode.Equals("pt-BR"));
34+
35+
if (marca == null)
36+
return "";
37+
38+
return marca.brandName;
39+
}
40+
41+
public static Tuple<string, decimal> LerEmbalagem(this Product product)
42+
{
43+
Tradeitemmeasurements embalagem = product.tradeItemMeasurements;
44+
45+
if (embalagem == null)
46+
return null;
47+
48+
if (embalagem.netContent.value == null)
49+
return null;
50+
51+
return new Tuple<string, decimal>(embalagem.netContent.measurementUnitCode, embalagem.netContent.value.Value);
52+
}
53+
54+
public static string LerNCM(this Product product)
55+
{
56+
Additionaltradeitemclassification ncm = product.tradeItemClassification.additionalTradeItemClassifications.FirstOrDefault(x => x.additionalTradeItemClassificationSystemCode.Equals("NCM"));
57+
58+
if (ncm == null)
59+
return "";
60+
61+
return ncm.additionalTradeItemClassificationCodeValue.Replace(".", "");
62+
}
63+
64+
public static string LerCEST(this Product product)
65+
{
66+
Additionaltradeitemclassification cest = product.tradeItemClassification.additionalTradeItemClassifications.FirstOrDefault(x => x.additionalTradeItemClassificationSystemCode.Equals("CEST"));
67+
68+
if (cest == null)
69+
return "";
70+
71+
return cest.additionalTradeItemClassificationCodeValue.Replace(".", ""); ;
72+
}
73+
74+
public static Tuple<string, string> LerClassificacaoGPC(this Product product)
75+
{
76+
Tradeitemclassification gpc = product.tradeItemClassification;
77+
78+
if (gpc == null)
79+
return null;
80+
81+
if (gpc.gpcCategoryName == null)
82+
return null;
83+
84+
return new Tuple<string, string>(gpc.gpcCategoryCode, gpc.gpcCategoryName);
85+
}
86+
}
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net.Http;
4+
using System.Text;
5+
using Newtonsoft.Json;
6+
using RestSharp;
7+
8+
namespace Gs1Br
9+
{
10+
public class RetornoConsultaGtinGs1Br
11+
{
12+
public Product product { get; set; }
13+
public string status { get; set; }
14+
public string message { get; set; }
15+
}
16+
17+
public class Product
18+
{
19+
public Gs1tradeitemidentificationkey gs1TradeItemIdentificationKey { get; set; }
20+
public Tradeitemdescriptioninformationlang[] tradeItemDescriptionInformationLang { get; set; }
21+
public object[] childTradeItems { get; set; }
22+
public Referencedfileinformation[] referencedFileInformations { get; set; }
23+
public string gtinStatusCode { get; set; }
24+
public Brandnameinformationlang[] brandNameInformationLang { get; set; }
25+
public object languageCode { get; set; }
26+
public Tradeitemweight tradeItemWeight { get; set; }
27+
public Tradeitemmeasurements tradeItemMeasurements { get; set; }
28+
public Tradeitemclassification tradeItemClassification { get; set; }
29+
}
30+
31+
public class Gs1tradeitemidentificationkey
32+
{
33+
public string gs1TradeItemIdentificationKeyCode { get; set; }
34+
public string gtin { get; set; }
35+
public string fixedLengthGtin { get; set; }
36+
}
37+
38+
public class Tradeitemweight
39+
{
40+
public Grossweight grossWeight { get; set; }
41+
public Netweight netWeight { get; set; }
42+
}
43+
44+
public class Grossweight
45+
{
46+
public string measurementUnitCode { get; set; }
47+
public float value { get; set; }
48+
public float originalValue { get; set; }
49+
}
50+
51+
public class Netweight
52+
{
53+
public object measurementUnitCode { get; set; }
54+
public object value { get; set; }
55+
}
56+
57+
public class Tradeitemmeasurements
58+
{
59+
public Netcontent netContent { get; set; }
60+
public Height height { get; set; }
61+
public Width width { get; set; }
62+
public Depth depth { get; set; }
63+
}
64+
65+
public class Netcontent
66+
{
67+
public string measurementUnitCode { get; set; }
68+
public int? value { get; set; }
69+
public int? originalValue { get; set; }
70+
}
71+
72+
public class Height
73+
{
74+
public object measurementUnitCode { get; set; }
75+
public object value { get; set; }
76+
public object originalValue { get; set; }
77+
}
78+
79+
public class Width
80+
{
81+
public object measurementUnitCode { get; set; }
82+
public object value { get; set; }
83+
public object originalValue { get; set; }
84+
}
85+
86+
public class Depth
87+
{
88+
public object measurementUnitCode { get; set; }
89+
public object value { get; set; }
90+
public object originalValue { get; set; }
91+
}
92+
93+
public class Tradeitemclassification
94+
{
95+
public string gpcCategoryCode { get; set; }
96+
public Additionaltradeitemclassification[] additionalTradeItemClassifications { get; set; }
97+
public string gpcCategoryName { get; set; }
98+
}
99+
100+
public class Additionaltradeitemclassification
101+
{
102+
public string additionalTradeItemClassificationSystemCode { get; set; }
103+
public string additionalTradeItemClassificationCodeValue { get; set; }
104+
}
105+
106+
public class Tradeitemdescriptioninformationlang
107+
{
108+
public string languageCode { get; set; }
109+
public string tradeItemDescription { get; set; }
110+
public bool _default { get; set; }
111+
}
112+
113+
public class Referencedfileinformation
114+
{
115+
public string languageCode { get; set; }
116+
public string contentDescription { get; set; }
117+
public string uniformResourceIdentifier { get; set; }
118+
public bool featuredFile { get; set; }
119+
public bool _default { get; set; }
120+
public string referencedFileTypeCode { get; set; }
121+
}
122+
123+
public class Brandnameinformationlang
124+
{
125+
public string languageCode { get; set; }
126+
public string brandName { get; set; }
127+
public bool _default { get; set; }
128+
}
129+
130+
}

0 commit comments

Comments
 (0)
Please sign in to comment.