Monday, 1 July 2019

Parallel refresh requests of OAuth2 access token with Swift p2/OAuth2

I am using https://github.com/p2/OAuth2 for connecting to the backend of my app via OAuth2 which works quite well.

The problem I have is when the access token expires and multiple requests happen at the same time some of them fail.

Parallel requests can be triggered from different parts of the app. For example when the app is launched the current location is sent to the server and a list of events is downloaded.

What would be the best way to make sure that no second refresh token request is made while the first is still running?



from Parallel refresh requests of OAuth2 access token with Swift p2/OAuth2

Angular application cannot find files in sub directory

I want to deploy Angular application in Apache sub directory. I added this .htaccess configuration:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /admin/
  RewriteCond %{REQUEST_FILENAME} -s [OR]
  RewriteCond %{REQUEST_FILENAME} -l [OR]
  RewriteCond %{REQUEST_FILENAME} !-f [OR]
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteRule ^.*$ - [NC,L]
  RewriteRule ^(.*) index.html [NC,L]
</IfModule>

I used this command to compile the code:

ng build --prod --base-href /admin/

But when I open one page I get this error:

Error: Uncaught (in promise): e: {"headers":{"normalizedNames":{},"lazyUpdate":null},"status":404,"statusText":"Not Found","url":"http://185.185.185.185/admin/api/transaction_notes/unique_id/219","ok":false,"name":"HttpErrorResponse","message":"Http failure response for http://185.185.185.185/admin/api/transaction_notes/unique_id/219: 404 Not Found","error":"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<html><head>\n<title>404 Not Found</title>\n</head><body>\n<h1>Not Found</h1>\n<p>The requested URL /admin/api/transaction_notes/unique_id/219 was not found on this server.</p>\n<hr>\n<address>Apache/2.4.18 (Ubuntu) Server at 185.185.126.15 Port 80</address>\n</body></html>\n"}

Can you give me some advice how I can solve this isssue?



from Angular application cannot find files in sub directory

Check and recover a cached file with okhttp3

I am using okhttp3 to make requests to the server, not using retrofit. My idea is to make a call and save the response in cache for 15 minutes. if during those 15 minutes, the request is made again, recover the cache response, after 15 minutes, request it again from the server.

This is my code:

public class Handler_JSON_get {

    public Handler_JSON_get() {

    }

    public String makeServiceCall(String url, JSONObject data) {
        final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

        Cache cache = new Cache(new File(Environment.getExternalStorageDirectory().getPath() + "/Android/data/MY_PACKAGE/", "cache"), 10 * 1024 * 1024);

        OkHttpClient okHttpClient;

            Log.i("2134","Usando caché");
             okHttpClient = new OkHttpClient.Builder()
                    .addNetworkInterceptor(provideCacheInterceptor())
                    .readTimeout(45, TimeUnit.SECONDS)
                    .protocols(Arrays.asList(Protocol.HTTP_1_1))
                    .addInterceptor(provideCacheInterceptor())
                    .cache(cache)
                    .build();

        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();

        try {
            Response response = okHttpClient.newCall(request).execute();

            Log.i(TAG,"Response:"+response.code());

            return response.body().string();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    Interceptor provideCacheInterceptor() {
        return new Interceptor() {
            @Override
            public Response intercept(Interceptor.Chain chain) throws IOException {
                Response response = chain.proceed(chain.request());
                CacheControl cacheControl = new CacheControl.Builder()
                        .maxAge(15, TimeUnit.MINUTES)
                        .build();

                return response.newBuilder()
                        .header("cache", cacheControl.toString())
                        .build();
            }
        };
    }
}

The problem is that he always makes the call to the server and I have many doubts.

okhttp3 is responsible for seeing if there is a cached response in the storage?

The call saves a journal file, is this the correct file?

What am I missing or what am I doing wrong?

I have seen that there are quite a few questions about it, but I can not make those answers compatible with my code.

I would sincerely appreciate the parts that are necessary in my code to function properly, I start to go crazy.

Thanks in advance.



from Check and recover a cached file with okhttp3