forked from tobik312/GUS-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUS.cs
125 lines (111 loc) · 4.94 KB
/
GUS.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Linq;
using System.Collections.Generic;
using GUS_lib.Utils;
using GUS_lib.Models;
namespace GUS_lib{
public class GUS : IGUSApi{
private string key{get;set;}
private string sid{get;set;}
private DateTime sidExp{get;set;}
private SOAP soapClient{get;set;} = new SOAP(
"https://wyszukiwarkaregon.stat.gov.pl/wsBIR/UslugaBIRzewnPubl.svc",
"http://CIS/BIR/PUBL/2014/07/IUslugaBIRzewnPubl/"
);
public static KomunikatDictionary KomunikatValue = new KomunikatDictionary();
public GUS(string key,bool sandbox = false){
this.key = key;
if(sandbox)
this.soapClient.apiUrl = "https://wyszukiwarkaregontest.stat.gov.pl/wsBIR/UslugaBIRzewnPubl.svc";
}
private static DateTime WarsawTime{
get{
DateTimeOffset warsawTime;
try{
warsawTime =
TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTimeOffset.UtcNow, "Europe/Warsaw");
}catch(TimeZoneNotFoundException){
warsawTime =
TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTimeOffset.UtcNow,"Central European Standard Time");
}
return warsawTime.DateTime;
}
}
/*Service access*/
public void Login(bool force=false){
DateTime warsawTime = GUS.WarsawTime;
if(warsawTime.Hour==3 && warsawTime.Minute==20){
soapClient.sid = null;
sid = null;
throw new GUSException("Czyszczenie puli sesji.Spróbuj ponownie za chwilę",0);
}
if(sidExp == null || DateTime.Now>sidExp || force){
sidExp = DateTime.Now.AddMinutes(58);
ZalogujResponse res = soapClient.getData<Zaloguj,ZalogujResponse>(
new Zaloguj(){
pKluczUzytkownika = this.key
}
);
if(res.ZalogujResult==null) throw new GUSException("Nieudana próba logowania.",0);
sid = res.ZalogujResult;
soapClient.sid = sid;
}
}
public bool Logout(){
if(sid!=null){
WylogujResponse res = soapClient.getData<Wyloguj,WylogujResponse>(
new Wyloguj(){
pIdentyfikatorSesji = this.sid
}
);
if(res.WylogujResult){
soapClient.sid = null;
sid = null;
}
return res.WylogujResult;
}
return true;
}
/*Service info*/
public GetValueResponse GetValue(GetValueType type){
return soapClient.getData<GetValue,GetValueResponse>(
new GetValue(){
pNazwaParametru = type
}
,"http://CIS/BIR/2014/07/IUslugaBIR/");
}
/*Search entity*/
public Podmiot SzukajPodmiot(string nip = null,string regon = null,string krs = null){
Login();
return soapClient.getData<DaneSzukajPodmioty,DaneSzukajPodmiotyResponse>(
new DaneSzukajPodmioty(){
pParametryWyszukiwania = new ParametryWyszukiwania(){
Regon = regon,
Nip = nip,
Krs = krs
}
}
).FirstOrDefault();
}
public List<Podmiot> SzukajPodmioty(string[] nipy = null,string[] regony9 = null,string[] regony14 = null,string[] krsy = null){
Login();
return soapClient.getData<DaneSzukajPodmioty,DaneSzukajPodmiotyResponse>(
new DaneSzukajPodmioty(){
pParametryWyszukiwania = new ParametryWyszukiwania(){
Regon9zn = regony9 == null ? "" : String.Join(",",regony9),
Regon14zn = regony14 == null ? "" : String.Join(",",regony14),
Nipy = nipy == null ? "" : String.Join(",",nipy),
Krsy = krsy == null ? "" : String.Join(",",krsy)
}
}
);
}
public Podmiot SzukajPodmiotNip(string nip) => SzukajPodmiot(nip);
public Podmiot SzukajPodmiotRegon(string regon) => SzukajPodmiot(null,regon);
public Podmiot SzukajPodmiotKrs(string krs) => SzukajPodmiot(null,null,krs);
public List<Podmiot> SzukajPodmiotyNip(params string[] nipy) => SzukajPodmioty(nipy);
public List<Podmiot> SzukajPodmiotyRegon9(params string[] regony) => SzukajPodmioty(null,regony);
public List<Podmiot> SzukajPodmiotyRegon14(params string[] regony) => SzukajPodmioty(null,null,regony);
public List<Podmiot> SzukajPodmiotyKrs(params string[] krsy) => SzukajPodmioty(null,null,null,krsy);
}
}