Skip to content

Instantly share code, notes, and snippets.

View prof18's full-sized avatar

Marco Gomiero prof18

View GitHub Profile
@prof18
prof18 / rest-server.py
Created December 18, 2016 22:46 — forked from miguelgrinberg/rest-server.py
The code from my article on building RESTful web services with Python and the Flask microframework. See the article here: http://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask
#!flask/bin/python
from flask import Flask, jsonify, abort, request, make_response, url_for
from flask.ext.httpauth import HTTPBasicAuth
app = Flask(__name__, static_url_path = "")
auth = HTTPBasicAuth()
@auth.get_password
def get_password(username):
if username == 'miguel':
public class Parser extends AsyncTask<String, Void, String> implements Observer {
private XMLParser xmlParser;
private static ArrayList<Article> articles = new ArrayList<>();
private OnTaskCompleted onComplete;
public Parser() {
xmlParser = new XMLParser();
xmlParser.addObserver(this);
}
Parser parser = new Parser();
parser.execute(urlString);
parser.onFinish(new Parser.OnTaskCompleted() {
//what to do when the parsing is done
@Override
public void onTaskCompleted(ArrayList<Article> list) {
//list is an Array List with all article's information
//set the adapter to recycler view
mAdapter = new ArticleAdapter(list, R.layout.row, MainActivity.this);
mRecyclerView.setAdapter(mAdapter);
class XMLFetcher(private val url: String) : Callable<String> {
@Throws(Exception::class)
override fun call(): String {
return CoreXMLFetcher.fetchXML(url)
}
}
object CoroutineEngine {
@Throws(Exception::class)
suspend fun fetchXML(url: String) =
withContext(Dispatchers.IO) {
return@withContext CoreXMLFetcher.fetchXML(url)
}
@Throws(Exception::class)
suspend fun parseXML(xml: Deferred<String>) =
withContext(Dispatchers.IO) {
@Throws(Exception::class)
suspend fun getArticles(url: String) =
withContext(Dispatchers.IO) {
val xml = async { CoroutineEngine.fetchXML(url) }
return@withContext CoroutineEngine.parseXML(xml)
}
fun execute(url: String) {
Executors.newSingleThreadExecutor().submit{
val service = Executors.newFixedThreadPool(2)
val f1 = service.submit<String>(XMLFetcher(url))
try {
val rssFeed = f1.get()
val f2 = service.submit(XMLParser(rssFeed))
onComplete.onTaskCompleted(f2.get())
} catch (e: Exception) {
onComplete.onError(e)
coroutineScope.launch(Dispatchers.Main) {
try {
val parser = Parser()
val articleList = parser.getArticles(url)
setArticleList(articleList)
} catch (e: Exception) {
e.printStackTrace()
_snackbar.value = "An error has occurred. Please retry"
setArticleList(mutableListOf())
}
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
class ${NAME}(val items: MutableList<${ITEM_CLASS}>) : RecyclerView.Adapter<${NAME}.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.${LAYOUT_RES_ID}, parent, false))
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.bintray'
group 'com.your.awesome.lib'
version '1.0.0'
publishing {
publications {
Production(MavenPublication) {
artifact("$buildDir/outputs/aar/awesomelibrary-release.aar") {