2. Implementation of the API on the side of 1C.
3. Application on TSD and communication with 1C: Enterprise 8.3 through HTTP-Service. Part 3 (BroadcastReceiver. Getting data)
There are plenty of manuals on how to create an HTTP service on the Internet. Therefore, I will immediately describe the implementation. Our service consists of three URL patterns:
- Directories - / v1 / catalogs / {Directory} / {Action}
- Documents - / v1 / documents / {Document} / {Action}
- Services - / v1 / services / {Action}
In order not to inflate the article, consider the example with the reference book "Nomenclature". 1C does not allow us to have different paths for HTTP methods. Therefore, the URL pattern will be (/ v1 / catalogs / wares / {Action}, for which two HTTP methods GET, POST are defined. In other patterns we will also use GET, POST.
- / v1 - will allow us to support several API options in the future. We will be able to add the URL template v_V2_Directories and, accordingly, the path to start with / v2
- / catalogs / wares / - catalogs integrates all directories. This is the static part of the way. wares complies with the Nomenclature Directory. 1C will put "wares" in the URL parameters . It should be noted that we come up with the ways ourselves. And then we think how to process them in 1C ourselves too
- {Action} - here we will pass what we want to receive from 1C. For example getByParam or getAll
From a Retrofit point of view, baseUrl will look something like this
"http://192.168.0.1/unf/hs/inntsd/v1/"
where:
- 192.168.0.1 - Server where the HTTP service is published
- / unf - 1C database name
- / hs - static part for HTTP services
- / inntsd is the root URL for the entire service. Call as we want
Consider the procedure for receiving a request.
Get() // . Get //Post = (, "GET"); //middleware. . = (); = (); (, ) = (); // {} = (); // {} = (); // URL. .URL =(); // . Firefox , Google Chrome = ; .("", ); .("", ); .("", ); .("", ); .("", ); ; () = ..("Content-Type"); = ; .("", ); // "Content-Type","content-type", "Content-type" = = ..("content-type"); ; = = ..("ontent-type"); ; = .("", ); ; ; ((), "multipart/form-data") > 0 = (); ((), "application/json") > 0 = JSON(); .("", ); ; ; // JSON . . JSON(); JSON = JSON; = .(); JSON.(); = JSON(JSON,,"sampleDate",JSON.ISO); .("", ); = ; .("", ); ; JSON.(); ;
There are many bad tips in this example. For example, Directory Name, Action needs to be converted to an enumeration or a new directory with matches. But how elegant it is to implement in the supplement, I have not decided yet.
Let's parse the getByParam request. Full address: / v1 / catalogs / wares / getByParam? Prop = byCode & comparison = similarly & searchString = 239
Find out which directory we are working with.
() . = "" = (); . = "" = () = (100, " " + .); ; ;
Get the request method. And we check that all parameters are attached to the method. Here it should be noted that I do not do a deep check. Since I am writing for both 1C and Android.
() . = "" = ..("prop"); = ..("comparison"); = ..("searchString"); ( = ) ( = ) ( = ) = (103, " / "); ; ; = (, , ); = (0, ""); .("payload", ); . = "" = (); = (0, ""); .("payload", ); = ("102", " " + .); ; ;
If everything is alright. We prepare the data for the answer.
(, , ) = ; = " | . , | . , | . , | . , | .. | | . | | . | {}"; = ""; () = ("byCode") = "."; () = ("byArticle") = "."; () = ("byName") = "."; ; () = ("equally") = + " = &"; () = ("similarly") = + " &"; ; = (, "{}", ); . = ; () = ("similarly") = "%" + + "%"; ; .("", ); = .().(); = .(); = ; .( ("code,article, name, fullName, unit", ., ., ., ., .)); ; = ; .("quantity", ); .("wares", ); ;
And finally we unite everything together. We put this in JSON and send the response.
= (0, ""); .("payload", ); ; (, ) = (); HTTP = HTTP(); HTTP.["Content-Type"] = "application/json; charset=utf-8"; // json UTF-8 // ToDo CORS //HTTP.["Access-Control-Allow-Origin"] = ; //HTTP.["Access-Control-Allow-Credentials"] = "true"; HTTP.(); HTTP; () JSON = JSON(, .,,,,,,,); = JSON; . = ; .(JSON); = JSON; . = JSON.; . = JSON.ISO; JSON(, , ); = .(); ;
And we give it all to the client.
= (200, ) ;
Regardless of what we got from 1C. We always try to answer correctly.
(, ) = ; = (); .("code", ); .("description", ); .("result", ); .("payload", ); ;
That's all. All other methods are implemented in a similar way. Ask questions. I will be happy to answer.