An open API service indexing awesome lists of open source software.

https://github.com/smith8h/sconnect

A Http client based library that use OkHttp3 for simply making requests to URLs and APIs, and get a response as Json or plain text.
https://github.com/smith8h/sconnect

android android-lib android-library connect http http-client http-request http-requests http-response https java json json-parser kotlin rest-api

Last synced: about 2 months ago
JSON representation

A Http client based library that use OkHttp3 for simply making requests to URLs and APIs, and get a response as Json or plain text.

Awesome Lists containing this project

README

          

# SConnect

![Builds and tests](https://github.com/smith8h/SConnect/actions/workflows/build.yml/badge.svg)
[![JitPack release](https://jitpack.io/v/smith8h/SConnect.svg)](https://jitpack.io/#smith8h/SConnect)
![Latest release](https://img.shields.io/github/v/release/smith8h/SConnect?include_prereleases&label=latest%20release)
![stable version](https://img.shields.io/badge/stable_version-v6.0-blue)
![stability-stable](https://img.shields.io/badge/stability-stable-green.svg)
![minimumSDK](https://img.shields.io/badge/minSDK-24-f39f37)
![Repository size](https://img.shields.io/github/repo-size/smith8h/SConnect)

**(SConnect)** A Http client based library that use *OkHttp3* for simply making requests to URLs and APIs, and get a response as Json or plain text.

## Content

- [**Setup**](#setup)
- [**Changelog**](https://github.com/smith8h/SConnect/blob/main/CHANGELOG.md)
- [**Documentation**](#documentation)
- [**Example Code**](#example-code)
- [**Donations :heart:**](#donations)

## Setup

> **Step 1.**
> Add the JitPack repository to your build file.
> Add it in your root build.gradle at the end of repositories:

```gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
...
}
}
```

> **Step 2.** Add the dependency:

```gradle
dependencies {
...
implementation 'com.github.smith8h:SConnect:v5.0'
...
}
```

> [!WARNING]
> Add these dependencies in case you facing some compile or runtime errors:

```gradle
dependencies {
...
implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.11'
implementation 'com.squareup.okio:okio:3.5.0'
implementation 'com.google.code.gson:gson:2.10.1'
...
}
```

## Documentation

To create a connection first pass a context using `with()` method:

```java
SConnect.with(context)
```

Then pass the callback interface to deal with the response using `callback()` method:

```java
SConnect.with(context).callback(new SConnectCallBack() {
@Override
public void onFailure(SResponse response, String tag) {}

@Override
public void onSuccess(SResponse response, String tag, Map responseHeaders) {
// use response, tag, responseHeaders
if (response.isJSON() && response.isMap()) {
Toast.makeText(context, response.getMap().getString("key"), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, response.toString(), Toast.LENGTH_SHORT).show();
}
}
})
```

After that, if you need to add headers, params to your connection. add them using methods:

```java
.addHeaders(Map)
.addHeader("key", value) // you can also use add header one by one.

.addParams(Map)
.addParam("key", value) // you can also use add header one by one.

// and then set the param type.
.paramsType(SConnect.PARAM) // or BODY

// if required for some connections
.mediaType("a string represent the media type applied to a connection")
```

then pass the url using `url()` method:

```java
.url("url")
```

**Optional method |** use `tag()` to set a tag to every connection (useful when you do a multiple connections at same time and need to recognize them).

```java
.tag("someTag")
```

finaly, use any of `get()`, `post()`, `put()`, `delete()`, `patch()`, `options()` or `head()` methods corresponding to your connection:

```java
.get()
// or: post() | put() | delete() | patch() | options() | head()
```

## Example Code

• connections doesn't need params/headers:

```java
SConnect.with(this)
.callback(callback)
.url(url)
.get(); // post | put | delete
// also pass tag if you need to recognize multiple requesrs which one is giving response
```

• connections need params/headers:

```java

Map params = new HashMap<>();
params.put("key", value);
...
Map headers = new HashMap<>();
params.put("key", value);
...

SConnect.with(this)
.callback(callback)
.addParams(params)
.paramsType(SConnect.PARAM) // or BODY
.addHeaders(headers)
.mediaType("json/application;Charset:UTF-8")
.url("http://example.url.com")
.tag("sample")
.get(); // post | put | delete...
```

Dealing with **Json response* using `SResponse` class
> if response is plain/text or HTML (when requesting websites) simply use `response.toString()` method.

```java
// get/check response as json (if get a api json response)
boolean isJSON = response.isJSON();
// check if response json is Map Object
boolean isMap = response.isMap();
// else if it's Array
boolean isArray = response.isArray();


// getting response if it is plain text or json in general
String text = response.toString();


// get response as map object
SResponse.Map object = response.getMap();
// now you can access all values in that object using
// return Object of any value
Object o = object.get("key");
// return int
int i = object.getInt("key");
// return String
String s = object.getString("key");
// return float
float f = object.getFloat("key");
// return boolean
boolean b = object.getBoolean("key");
// return Map object as above ↑
// if map object nested inside another map object
SResponse.Map = object.getMap("key");
// get keys
Set keys = object.keys();
// get values
List values = object.values();
// has key?
boolean hasKey = object.hasKey("key");
// has value? (accepts anything)
boolean hasValue = object.hasValue(Object);
// iterate through keys & values
object.forEach((key, value) -> {
// use key || value
});
// size
int size = object.size();
// is empty?
boolean isEmpty = object.isEmpty();
// to json string
String json = object.toString();
// to HashMap
HashMap map = object.toMap();


// the same but for arrays
// get response as array from response or from map object
// this if the response body is array
SResponse.Array array = response.getArray();
// and this if response body is object ↑ has an array as value inside it
SResponse.Array array = object.getArray("key");
// array class has same methods like map class
// get at index as object of any value
Object o = array.get(0);
// get string
String s = array.getString(0);
// get int
int i = array.getInt(0);
// get float
float f = array.getFloat(0);
// get boolean
boolean b = array.getBoolean(0);
// get Map object like above if map object nested inside list
SResponse.Map m = array.getMap(0);
// same if it has array inside array
SResponse.Array a = array.getArray(0);
// contains something?
boolean contains = array.contains(Object);
// iterate through items
array.forEach(item -> {
// use item
});
// size
int size = array.size();
// is empty
boolean isEmpty = array.isEmpty();
// to json string
String json = array.toString();
// to List
List list = array.toList();

```

## Contributors







## Donations

> If you would like to support this project's further development, the creator of this projects or the continuous maintenance of the project **feel free to donate**.
Your donation is highly appreciated. Thank you!

You can **choose what you want to donate**, all donations are awesome!

[![PayPal](https://img.shields.io/badge/PayPal-00457C?style=for-the-badge&logo=paypal&logoColor=white)](https://www.paypal.me/husseinshakir)
[![Buy me a coffee](https://img.shields.io/badge/Buy_Me_A_Coffee-FFDD00?style=for-the-badge&logo=buy-me-a-coffee&logoColor=black)](https://www.buymeacoffee.com/HusseinShakir)
[![Ko-fi](https://img.shields.io/badge/Ko--fi-F16061?style=for-the-badge&logo=ko-fi&logoColor=white)](https://ko-fi.com/husseinsmith)






With :heart: