# retrofit **Repository Path**: start0715/retrofit ## Basic Information - **Project Name**: retrofit - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 25 - **Created**: 2022-03-25 - **Last Updated**: 2022-03-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## retrofit_ohos 一A HTTP client for openharmony ETS platform. ### Usage Instructions #### Create HTTP Request class Retrofit parses HTTP Request class and provides method based api calling. As shown below, Create Custom HTTP request class like "DataService" Extending "BaseService". Retrofit supports API declaration with Decorators, for example to make "GET" api call use @GET. Other api's supported are listed under "API Introduction" below. Use "retrofit_ohos" module to import retrofit apis. ``` javascript import {BaseService,ServiceBuilder,GET,POST,DELETE,PUT,Path,Body,BasePath,Response,Header,Query} from 'retrofit_ohos'; //BasePath - Append "/" to basepath. @BasePath("/") class DataService extends BaseService{ //GET Api call with Header and Query params @GET("get?test=arg123") async getRequest(@Header("Content-Type") contentType: string,@Query('d1') data1: string,@Query('d2') data2: number): Promise> { return >{} }; //POST Api call with Header and Body info @POST("post") async postRequest(@Header("Content-Type") contentType: string,@Body user: User): Promise> { return >{} }; //PUT Api call with Header and Body info @PUT("put") async putRequest(@Header("Content-Type") contentType: string,@Body user: User): Promise> { return >{} }; //GET Api call with Header, Query and Path Param @GET("{req}?test=arg123") async getRequest2(@Header("Content-Type") contentType: string,@Query('d1') data1: string,@Query('d2') data2: number,@Path("req") requestPath: string): Promise> { return >{} }; } ``` #### Initialize Retrofit service and call methods. Initialize the Retrofit BaseService, Set End point and Call required apis by its method. ``` javascript const dataService = new ServiceBuilder() .setEndpoint("https://restapiservice.com") //Base Url .build(DataService); dataService.getRequest("application/json","dat1",8).then((resp)=>{ //Get request with params console.log("getRequest Response =" + JSON.stringify(resp.result)); } ``` ### API Introduction ## BaseService.ServiceBuilder **setEndpoint(endpoint: string): ServiceBuilder** Set base url **setTimeout(timeout: number): ServiceBuilder** set Request timeout in ms **build(service: new (builder: ServiceBuilder) => T): T** Build Retrofit Base service ## BaseService **setEndpoint(endpoint: string): void** Set the base url **clone()** Clone the Base service request. ## Decorators **@GET** GET HTTP method Decorator **@POST** POST HTTP method Decorator **@PUT** PUT HTTP method Decorator **@DELETE** DELETE HTTP method Decorator **@HEAD** HEAD HTTP method Decorator **@OPTIONS** OPTIONS HTTP method Decorator **@BasePath** BasePath Decorator for appending base path **@Path** Path Decorator **@Body** Body Decorator for passing body **@Headers** Headers Decorator for setting Headers **@Header** Header Decorator for setting an header **@HeaderMap** HeaderMap Decorator for setting header as map object **@Queries** Queries Decorator for setting query list **@Query** Query Decorator for setting query **@QueryMap** QueryMap Decorator for setting query in map **@FormUrlEncoded** FormUrlEncoded Decorator for enabling formurlencoding **@Field** Field Decorator for setting a field for post method **@FieldMap** FieldMap Decorator for setting fields with Map object **@Timeout** Timeout Decorator for setting request timeout in millisecond ## Installation Instructions 1. As Retrofit uses okhttp library as http client, Copy okhttp_ohos library folder to root directory of app project. 2. Copy retrofit_ohos library folder to Project root folder along with okhttp_ohos. and add local dependencies to /package.json as below. ``` "dependencies": { "retrofit_ohos": "file:./retrofit_ohos" } ```