Giter Club home page Giter Club logo

apiconsumer's Introduction

Api Consumer by Armane

Using API Consumer with Java, Android & Kotlin

Implementation

The API Consumer is a tool that can be used perfectly to make http request in Java and kotlin applications. To use the API Consumer visit and follow the instructions.


##With Gradle
you can use this API with Gradle (Android Or Desktop or Web).

To get a Git project into your build:

  • Step 1. Add the JitPack repository to your build.gradle at the end of repositories:
	allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}
  • Step 2. Add the dependency
	dependencies {
	        implementation 'com.github.Abderrahmanearache:ApiConsumer:1.1'
	} 


##With Maven
you can use this API also with Maven Repository

To get a Git project into your Maven Project:

  • Step 1. Add the JitPack repository to your pom.xml repositories.
    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>
  • Step 2. Add the dependency
	<dependency>
	    <groupId>com.github.Abderrahmanearache</groupId>
	    <artifactId>ApiConsumer</artifactId>
	    <version>1.1</version>
	</dependency>

How it works

The API contains many features that help you get data from your APIs or external APIs.

So far you can get your data as a single string or json, so ApiConsumer provides 2 methods :

  • A StringRequest that you can use to get simple text,
  • A JsonRequest that allows you to get your data as a JSONObject to parse at the end.

>> Getting a simple text

to get a simple String data from an API, using a StringRequest :

 
        StringRequest sRequest = new StringRequest("http://armane.studio/apiconsumer/params")
                .onFinish(response -> System.out.println(response))
                .onError(System.err::println);
        sRequest.execute();

at the end, you get a StringResponse which contains a getResponse function returns a string value as below

       StringRequest sRequest = new StringRequest("http://armane.studio/apiconsumer/params")
              .onFinish(response -> {
                  System.out.println(response.getResponse());
              });

Methods

generally, the method is GET by default, unless you define the method in the parameters.

	StringRequest sRequest = new StringRequest("http://armane.studio/apiconsumer/params",Method.POST);

or by executing the request by doGet(),doPost() or doRequest() instead of execute()


Binding params

to send parameters to your API, using GET or POST:

  • use addParams with a String matrix :
        StringRequest sRequest = new StringRequest("http://armane.studio/apiconsumer/params", Method.POST);
        sRequest.addParams(new String[][]{
                {"name1", "value1"},
                {"name2", "value2"},
				...
		});
		
  • or use addParams with a HashMap<String,String> :
			
        HashMap<String, String> params = new HashMap<>();
        params.put("name1", "value1");
        params.put("name2", "value2");

        StringRequest sRequest = new StringRequest("http://armane.studio/apiconsumer/params", Method.POST);
        sRequest.addParams(params);
  • or using addJsonParam with an new or existing object directly
       StringRequest sRequest = new StringRequest("http://armane.studio/apiconsumer/params", Method.POST);
       sRequest.addJsonParam(new Object() {
           String name1 = "value1";
           String name2 = "value2";
           Object object = new Object() {
                       String name = "name";
                       String job = "job";   
              }; 
       });
       sRequest.doRequest();        

this method parse the object and stringify it as bellow :

{
   "name1":"value1",
   "name2":"value2",
   "object":{
      "name":"name",
      "job":"job"
   }
}

if you want use addJsonParam, make sure you use the doRequest method, the json data goes at input to your api, to read it in your servier side :
+- java servlet >> use request.getReader()
+- PHP >>use file_get_contents('php://input')


Result

The onFinish function is a callback function that can be executed when the request ends perfectly and returns a result, so we have to define what we needs to do at the end.

The onError function is a callback function that can be executed when an error has occurred somewhere during execution.

      StringRequest sRequest = new StringRequest("http://armane.studio/apiconsumer/params")
                .onFinish(response -> {
                    doWhatYouWantWith(response.getResponse());
                })
                .onFinish(error -> {
                    showError(error.gerMessage());
                });

>> Getting a Json Object

if you know that your API will provide you with a json format, it is recommended to use JsonRequest instead.

the JsonRequest has the same functions as StringRequest, the only diferent is that you make sure that you get a jsonObject at the end.

Exemple :

        HashMap<String, String> hashParam = new HashMap<>();
        hashParam.put("name1", "value1");
        hashParam.put("name2", "value2");

        JsonRequest jsonRequest = new JsonRequest("http://armane.studio/apiconsumer/params")
        jsonRequest.addParams(hashParam)
                //or
                .addParams(new String[][]{
                        {"name1", "value1"},
                        {"name2", "value2"},
                })
		//or
                .addJsonParam(new Object() {
                    String param1 = "value1";
                    String name2 = "value2";
                    Object object = new Object() {
                    		String name = "name";
                    		String job = "job";   
                   }; 
                }) 
                .onFinish(jsonResponse -> {
                    if (jsonResponse.isJson())
                        System.out.println(jsonResponse.getJsonObject().get("keyFromYourJsonResult"));
                })
                .onError(System.err::println);
        //to execute 
        jsonRequest.execute(); //or
        jsonRequest.doPost(); //or
        jsonRequest.doGet(); //or
        jsonRequest.doRequest(); //or 

you can use it with Kotlin language with Android Example :

        var req = JsonRequest("http://armane.studio/apiconsumer/params")
                .onFinish {
            		jsonResponse -> Log.d("json", jsonResponse.jsonObject.toString())
        		}
                .onError {
                    exception -> exception.printStackTrace()
                }
                .addParams(arrayOf(
                        arrayOf("key", "val"),
                        arrayOf("key2", "val2")
                ))
        req.doGet()

apiconsumer's People

Contributors

ar-mane avatar

Watchers

Zakaria HBA avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.