Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

4 Commits

Repository files navigation

Честный знак API Client

A thread-safe Java client for the Честный знак API with request rate limiting.

Features

  • Thread-safe implementation
  • Rate limiting for API requests
  • Support for both production and demo environments
  • Support for all document types and product groups
  • Proper error handling

Usage

Creating an API Client

// Create a client allowing 10 requests per minuteCrptApiapi = newCrptApi(TimeUnit.MINUTES, 10);
// Or specify the environment (PRODUCTION or DEMO)CrptApidemoApi = newCrptApi(TimeUnit.MINUTES, 10, CrptApi.Environment.DEMO);

Authentication

try {
// Create a certificate signer that signs data with your УКЭПCrptApi.CertificateSignersigner = data -> {
// Implement your УКЭП signing logic here// This is typically done using a cryptographic libraryreturnsignWithUKEP(data);
};
// Authenticate to get a tokenStringtoken = api.authenticate(signer);
} catch (Exceptione) {
// Handle authentication errors
}

Creating Documents

Using the Unified Document Creation API

// Prepare your document data in Base64 formatStringbase64EncodedDocument = "...";
Stringsignature = "..."; // Your УКЭП signaturetry {
// Create a document (e.g., introducing goods for milk products)CrptApi.CreateDocumentResponseresponse = api.createDocument(
CrptApi.DocumentFormat.MANUAL, // Format: MANUAL, XML, or CSVbase64EncodedDocument, // Base64 encoded documentsignature, // УКЭП signatureCrptApi.DocumentType.LP_INTRODUCE_GOODS, // Document typeCrptApi.ProductGroup.MILK// Product group
);
System.out.println("Document created with ID: " + response.getDocumentId());
} catch (InterruptedExceptione) {
Thread.currentThread().interrupt();
System.err.println("Request interrupted: " + e.getMessage());
} catch (TimeoutExceptione) {
System.err.println("Rate limit exceeded: " + e.getMessage());
} catch (IOExceptione) {
System.err.println("I/O error: " + e.getMessage());
} catch (CrptApi.ApiExceptione) {
System.err.println("API error " + e.getStatusCode() + ": " + e.getMessage());
} finally {
// Make sure to shut down the API client when doneapi.shutdown();
}

Using the Convenience Method for Introducing Goods

// Create a document using the Document object modelCrptApi.Documentdocument = newCrptApi.Document();
document.setDocId("doc123");
document.setDocType("LP_INTRODUCE_GOODS");
document.setOwnerInn("1234567890");
// Set other document properties// Create a productCrptApi.Productproduct = newCrptApi.Product();
product.setUitCode("uitCode123");
product.setProducerInn("1234567890");
// Set other product properties// Add product to documentdocument.setProducts(List.of(product));
try {
// Create an introduce goods document for the milk product groupCrptApi.CreateDocumentResponseresponse = api.createIntroduceGoodsDocument(
document, "digital_signature",
CrptApi.ProductGroup.MILK
);
System.out.println("Document created with ID: " + response.getDocumentId());
} catch (Exceptione) {
// Handle errors
} finally {
api.shutdown();
}

Building

This project uses Maven for dependency management. To build:

mvn clean package

Requirements

  • Java 11 or higher
  • Maven (for building)

About

Тестовое задание на позицию Java developer

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages