|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using System;
- using System.Net;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using Acumatica.RESTClient.Client;
- namespace uri_import.AClasses
- {
-
- public class RestService : IDisposable
- {
- private readonly HttpClient _httpClient;
- private readonly string _acumaticaBaseUrl;
-
- public RestService(string acumaticaBaseUrl)
- {
- _acumaticaBaseUrl = acumaticaBaseUrl;
- _httpClient = new HttpClient(new HttpClientHandler
- {
- UseCookies = true,
- CookieContainer = new System.Net.CookieContainer()
- })
- {
- BaseAddress = new Uri(acumaticaBaseUrl + "/entity/Default/20_200_001/"),
- DefaultRequestHeaders = { Accept = { MediaTypeWithQualityHeaderValue.Parse("text/json") } }
- };
-
- }
-
- public RestService(string acumaticaBaseUrl, string userName, string password, string tenant, string locale)
- {
- _acumaticaBaseUrl = acumaticaBaseUrl;
- _httpClient = new HttpClient(new HttpClientHandler
- {
- UseCookies = true,
- CookieContainer = new System.Net.CookieContainer()
- })
- {
- BaseAddress = new Uri(acumaticaBaseUrl + "/entity/Default/20_200_001/"),
- DefaultRequestHeaders = { Accept = { MediaTypeWithQualityHeaderValue.Parse("text/json") } }
- };
- _httpClient.PostAsJsonAsync(acumaticaBaseUrl + "/entity/auth/login", new
- {
- name = userName,
- password = password,
- tenant = tenant,
- locale = locale
- }).Result
- .EnsureSuccessStatusCode();
- }
-
- public string PutFile(string acumaticaBaseUrl, string entityName, string keys, string fileName, System.IO.Stream file)
- {
- string endPoint = string.Format("{0}/{1}/{2}/{3}/files/{4}", acumaticaBaseUrl, "/entity/Default/20.200.001/", entityName, keys, fileName);// +"/entity/Default/20.200.001/" + entityName + "/" + keys + "/files/" + fileName;
- var res = _httpClient.PutAsync(endPoint, new StreamContent(file)).Result.EnsureSuccessStatusCode();
- return res.Content.ReadAsStringAsync().Result;
- }
-
- void IDisposable.Dispose()
- {
- _httpClient.PostAsync(_acumaticaBaseUrl + "/entity/auth/logout",
- new ByteArrayContent(new byte[0])).Wait();
- _httpClient.Dispose();
- }
- }
- }
|