Application on TSD and communication with 1C: Enterprise 8.3 through HTTP-Service. Part 2 (API implementation on the 1C side)

1. The choice of exchange method. API Description.



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:







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.





From a Retrofit point of view, baseUrl will look something like this "http://192.168.0.1/unf/hs/inntsd/v1/"



where:







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.



All Articles